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.
A server running Ubuntu.
A user with sudo privileges.
First, let's update the package manager index and install Nginx:
Next, we need to install PHP 8 and some dependencies. Run the following command:
Next, we need to configure MySQL for our Laravel project. Run the following command to install MySQL:
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:
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:
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:
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:
This will create a new Laravel project in the blog directory.
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:
Open the new configuration file in a text editor:
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):
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:
Then, disable the default server block by deleting the symbolic link:
Now, test the Nginx configuration to make sure there are no syntax errors:
If the configuration is valid, you will see the following output:
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:
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.