Installing And Configuring Laravel With Nginx On Ubuntu.

Laravel is a popular PHP framework for web application development. In this tutorial, we will show you how to install and configure Laravel with Nginx on an Ubuntu server using PHP.

Prerequisites

  • A server running Ubuntu.

  • A user with sudo privileges.

Step 1: Install Nginx

First, let's update the package manager index and install Nginx:

sudo apt update
sudo apt install nginx

Step 2: Install PHP and Dependencies

Next, we need to install PHP 8 and some dependencies. Run the following command:

sudo apt install php8.0-fpm php8.0-common php8.0-mysql php8.0-xml php8.0-xmlrpc php8.0-curl php8.0-gd php8.0-imagick php8.0-cli php8.0-dev php8.0-imap php8.0-mbstring php8.0-opcache php8.0-soap php8.0-zip

Step 3: Configure MySQL

Next, we need to configure MySQL for our Laravel project. Run the following command to install MySQL:

sudo apt install mysql-server


During the installation, you will be prompted to set a root password for MySQL. Choose a secure password and make a note of it, as you will need it later.


After installing MySQL, log in to the MySQL shell:

mysql -u root -p


Enter the root password that you set earlier to log in.


Next, create a new MySQL user and database for your Laravel project. Run the following commands, replacing DB_USERNAME and DB_PASSWORD with the username and password that you want to use:

CREATE USER 'DB_USERNAME'@'localhost' IDENTIFIED BY 'DB_PASSWORD';
GRANT ALL PRIVILEGES ON * . * TO 'DB_USERNAME'@'localhost';
FLUSH PRIVILEGES;
CREATE DATABASE blog;

Step 4: Install Composer

Now, let's install Composer, which is a dependency manager for PHP. We will use Composer to install Laravel. Run the following command to download and install Composer:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Step 5: Install Laravel

With Composer installed, we can now install Laravel. Create a new directory for your Laravel project and navigate to it. Then, run the following command to install Laravel:

composer create-project --prefer-dist laravel/laravel blog


This will create a new Laravel project in the blog directory.

Step 6: Configure Nginx

With MySQL configured, we can now configure Nginx to serve our Laravel project. Create a new Nginx server block by copying the default configuration file:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/blog


Open the new configuration file in a text editor:

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


Update the server block with the following content, replacing YOUR_SERVER_IP with your server's IP address and YOUR_DOMAIN_NAME with your domain name (if you have one):

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/blog/public;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name YOUR_SERVER_IP YOUR_DOMAIN_NAME;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }
}


In this configuration, we are telling Nginx to listen for incoming connections on port 80 and to serve the content from the public directory of our Laravel project. We are also specifying the PHP FastCGI process manager (FPM) socket and setting the index directive to look for index.php as the default index file.


The location block specifies how Nginx should handle requests for PHP files. It includes the fastcgi-php.conf snippet and specifies the PHP FPM socket to use for processing requests.


Finally, the location block with the .ht directive denies access to any files that begin with .ht.


Save the file and close the text editor.


Next, enable the new server block by creating a symbolic link from the sites-available directory to the sites-enabled directory:

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


Then, disable the default server block by deleting the symbolic link:

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


Now, test the Nginx configuration to make sure there are no syntax errors:

sudo nginx -t


If the configuration is valid, you will see the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


If there are any syntax errors, Nginx will display an error message with details about the problem.


If the configuration is valid, restart Nginx to apply the changes:

sudo systemctl restart nginx


Now, open your web browser and visit your server's IP address or domain name. You should see the Laravel welcome page.


Congratulations! You have successfully installed and configured Laravel with Nginx on your Ubuntu server.