Hosting Laravel on nginx server and some common errors

Last Updated on 08 Sep 2021 by Ankur Gupta
10 mins read

Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller architectural pattern and based on Symfony. (Just a usual Wikipedia defination)

We'll be setting up a Laravel website using the nginx web server in a virtual machine.

Pre-requisites

I assume that you've already set up an Ubuntu Machine, installed nginx and cloned your website, which in our case we assume is named "semikolan" in the directory /var/www/semikolan. If you haven't, you can follow this tutorial. 

Installing PHP and composer

Now that we've cloned the website, we now need to install PHP and composer in our machine to serve Laravel. We can simply run these commands to do so:

sudo apt install php-fpm php-mysql php-mbstring php-xml php-bcmath

We can now also install composer using

sudo apt-get install composer

We now also need to install the necessary components required for laravel by our website, to do so, make sure that we are in our website's directory which in my case is /var/www/semikolan, you can navigate to the directory using:

cd /var/www/semikolan

We can now run composer install using:

sudo composer install

Setting up database

If you don't have a database and need to host your database in your machine only, you can install a mySQL server using:

sudo apt install mysql-server

Now start the server using

sudo mysql

We can now create a database, which can be done using (here the name we're using is semidb, it can be anything of your choice),

CREATE DATABASE semidb;

We also need to have a user in our database and give them the necessary permissions, which can be done by:

CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'dbpassword';
GRANT ALL PRIVILEGES ON semidb.* TO 'dbuser'@'localhost';

If you have multiple databases, you can keep creating more databases and then grant privilege on all databases using:

GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'localhost';

Now that we've created a database, you can know import our existing tables using phpmyadmin, adminer or directly transferring them. If your PHP app supports migrations, you can just run php artisan migrate after setting up your .env file mentioned below.

If you don't have any existing database, you can just create new tables according to your needs directly via mysql, phpmyadmin or adminer etc. 

You can then quit the mysql terminal using:

quit

To import a .sql file in your database, you can also use:

mysql -u dbuser -p semidb < file.sql

f you already have a mySQL server set up, you can ignore all of these above steps.

Changing .env files

Now that you have your database set up, you can now use these configurations in your .env files, make sure you are in your website directory and edit them using:

sudo nano .env

Here is how the files look in our case:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=semidb
DB_USERNAME=dbuser
DB_PASSWORD=dbpassword

You can also change other configurations according to your need, if you have a different hosted database, make sure to use that host in DB_HOST section. 

We should also make our directories editable by the application, we can do that using

sudo chmod -R -v 777 *

However the above method is insecure and you should only make the required directory editable. In mose cases, this is sufficient:

sudo chown -R www-data.www-data /var/www/semikolan/storage
sudo chown -R www-data.www-data /var/www/semikolan/bootstrap/cache

Changing nginx Configuration to serve laravel:

Now we'll change nginx configuration, to do so, we first create a new file using 

sudo nano /etc/nginx/sites-available/semikolan

Now we can add the default laravel configuration, which in this case is:

server {
    listen 80;
    server_name localhost;
    root /var/www/semikolan;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Make sure to edit the root path according to your directory.

You can now enable the file using:

sudo ln -s /etc/nginx/sites-available/semikolan /etc/nginx/sites-enabled/semikolan

You can now unlink the default configuration using:

sudo unlink /etc/nginx/sites-enabled/default

That should do the work and you should see your site hosted at your Ubuntu IP address.

You can now point this IP to your domain's DNS record and generate a SSL certificate. You can follow this tutorial to do so.

Category: Laravel | DevOps

Relavent Tags: Laravel, DevOps, Nginx, Web Hosting