Laravel is a powerful PHP framework for building modern web applications. When hosting a Laravel application, proper configuration of the web server, in this case, Nginx, is crucial for optimal performance. In this guide, we’ll walk through the steps to configure Nginx to host a Laravel application with a database.
Prerequisites
Before starting, ensure you have:
- Nginx is installed on your server.
- PHP installed and configured with required extensions.
- Composer installed for managing Laravel dependencies.
- A database server (MySQL or MariaDB) ready for use.
Step 1: Install Laravel
Let’s first install Composer before we install Laravel:
sudo apt-get install composer
The above command assumes you are using a Debian-based distribution like Ubuntu. If you are using a different Linux distribution, the package manager and command might vary. Adjust the command accordingly based on your system.
Now, let’s navigate to your web root directory and install Laravel using Composer:
composer create-project --prefer-dist laravel/laravel mylaravelapp
Replace mylaravelapp
with your desired project name.
Step 2: Configure Laravel Environment
Navigate to your Laravel application directory and copy the .env.example
file to .env
:
cd mylaravelapp
cp .env.example .env
Open the .env
file and configure the database connection settings:
nano .env
Paste the following lines in the .env
file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mylaravelapp
DB_USERNAME=mylaraveluser
DB_PASSWORD=your_password
Save and exit the file.
Step 3: Configure Database
To create the database for your Laravel application, you need to have MySQL or MariaDB installed. In this example, we’re installing the MSQL database server:
sudo apt-get install mysql-server
Log in to your MySQL or MariaDB server with the following command:
mysql -u root -p
You will be prompted to enter the root password for MySQL or MariaDB.
Create a Database and User
Create a new database and a user for your Laravel application. Replace mylaravelapp_db
, mylaravelapp_user
, and your_password
with your desired values. Paste the command commands one by one on the terminal:
CREATE DATABASE mylaravelapp_db;
CREATE USER 'mylaravelapp_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON mylaravelapp_db.* TO 'mylaravelapp_user'@'localhost';
FLUSH PRIVILEGES;
These commands do the following:
CREATE DATABASE mylaravelapp_db;
: Creates a new database namedmylaravelapp_db
.
CREATE USER 'mylaravelapp_user'@'localhost' IDENTIFIED BY 'your_password';
: Creates a new user namedmylaravelapp_user
with the passwordyour_password
.
GRANT ALL PRIVILEGES ON mylaravelapp_db.* TO 'mylaravelapp_user'@'localhost';
: Grants all privileges on themylaravelapp_db
database to themylaravelapp_user
user.
FLUSH PRIVILEGES;
: Reloads the privileges to apply the changes.
Exit the MySQL or MariaDB prompt:
EXIT;
These steps should create a database and user for your Laravel application. Make sure to update the .env
file in your Laravel application directory with the database connection details:
nano /path/to/mylaravelapp/.env
Update the following lines:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mylaravelapp_db
DB_USERNAME=mylaravelapp_user
DB_PASSWORD=your_password
Save the changes and proceed with configuring your Laravel application.
Step 4: Set Permissions
Ensure the appropriate permissions are set for Laravel directories:
chmod -R 755 storage
chmod -R 755 bootstrap/cache
Step 5: Create Nginx Server Block
If you haven’t installed Nginx, run the following command:
sudo apt-get install nginx
You can check the Nginx status by using this command:
sudo systemctl status nginx
.
Install and configure PHP for Nginx
Since Laravel is a PHP framework, we need to also install the php-fpm module on the server
sudo apt-get install php php-fpm php-mysql
This command is necessary to install PHP, the PHP FastCGI Process Manager, and the MySQL extension for PHP.
We also need you to configure and set up PHP to work with the Nginx server. This can be achieved by editing the php-fpm configuration file, which you can access using this command:
sudo nano /etc/php/8.1/fpm/pool.d/www.conf
Now, let’s update the settings as follows:
user = www-data
group = www-data
listen = /run/php/php8.1-fpm.sock
These settings configure PHP to run under the same user and group as Nginx while listening on the correct socket.
Now, save the changes to the file and then restart PHP-FPM to apply the changes:
sudo systemctl restart php8.1-fpm
Create the Nginx Configuration file
Create an Nginx server block configuration file for your Laravel application:
sudo nano /etc/nginx/sites-available/mylaravelapp
Add the following configuration:
server {
listen 80;
server_name mylaravelapp.com www.mylaravelapp.com;
root /path/to/mylaravelapp/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Replace /path/to/mylaravelapp
with the actual path to your Laravel application directory.
Step 6: Enable Server Block
Create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/mylaravelapp /etc/nginx/sites-enabled/
Step 7: Test Configuration and Reload Nginx
Test the Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 8: Access Your Laravel Application
Open your web browser and navigate to http://mylaravelapp.com
. You should see the Laravel welcome page.
Conclusion
Configuring Nginx to host a Laravel application involves setting up a server block, configuring PHP-FPM, and ensuring proper permissions. Following these steps will enable you to efficiently host and deploy your Laravel applications, providing a scalable and performant web hosting solution.