Laravel is a popular PHP framework to build web applications and websites in PHP. In this article, we will look at how to install Laravel with NGINX in Ubuntu. You can use these steps for other Debian systems also.
How to Install Laravel with NGINX
Here are the steps to install Laravel with NGINX in Ubuntu. Please note, you will need a user with root or sudo privileges for the following steps.
1. Install Prerequisites
Open terminal and run the following command to install prerequisites
$ $ sudo apt install -y php-mbstring php-xml php-fpm php-zip php-common php-fpm php-cli unzip curl nginx
Install Composer, a tool for dependency management in PHP.
$ sudo curl -s https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
Run the following command to verify composer installation and check its status.
$ composer diagnose
Install MySQL server. You can skip this step if MySQL is already installed on your system.
$ sudo apt install mysql-server
Enable & start MySQL Server.
$ sudo systemctl enable --now mysql.service
Set root password and other information for your MySQL Server with the following command.
$ sudo mysql_secure_installation
Also read : How to Check Cron Log in Linux
2. Install Laravel
Replace demo with the name of your project in all the following steps.
Create a Laravel project using Composer. Ignore any warning about not to run Composer as root user.
$ cd /var/www/html
$ sudo composer global require laravel/installer
$ sudo composer create-project --prefer-dist laravel/laravel demo
Grant access to your non-root user.
$sudo chmod -R 755 /var/www/html/demo
$ sudo chown -R demo_user:demo_user /var/www/html/demo
Install demo project
$ cd demo
$ composer install
Launch the Laravel application with the following command. We are running it on IP 192.10.12.23 and on port 8000. Replace the IP address and port number as per your requirement.
$ cd /var/www/html/example
$ php artisan serve --host=192.10.12.23 --port=8000
Open browser and go to the following URL to access your application.
Also read : How to Install CouchDB in Ubuntu
3. Configure NGINX
First, we will set the file permissions. Replace demo with your project name.
$ sudo chmod -R 755 /var/www/html/
demo$ sudo chown -R www-data:www-data /var/www/html/demo
Create NGINX configuration file for your project.
$ sudo vi /etc/nginx/sites-available/demo
.conf
Add the following lines to NGINX configuration file. Replace demo.com with your domain name. Replace /var/www/html/demo/public with the folder location of your project. Also we are using /var/run/php/php7.4-fpm.sock as our PHP-FPM. If you have installed a different PHP version, please update this path accordingly.
server { listen 80; server_name demo.com; root /var/www/html/demo/public; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; 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; } }
Also read : How to Enable HTTP2 in Tomcat
Enable NGINX configuration
$ sudo ln -s /etc/nginx/sites-available/demo.conf /etc/nginx/sites-enabled/
Remove default NGINX configuration
$ sudo rm /etc/nginx/sites-enabled/default
Restart NGINX server to apply changes
$ sudo systemctl restart nginx
Open browser and go to http://demo.com to test your website.
Also read : How to Configure X-Frame-Options in NGINX
4. Configure MySQL
At this point, your website is accessible but we have not configured your database yet. Log into MySQL as root user.
$ sudo mysql -p -u root
Create database named laraveldemo
mysql> CREATE DATABASE `laraveldemo` CHARACTER SET utf8 COLLATE utf8_general_ci;
Create database user laraveldemouser. Replace password below with one as per your requirement.
mysql> CREATE USER 'laraveldemouser'@'%' IDENTIFIED BY 'password';
Grant permissions
mysql> use laraveldemo;
mysql> GRANT ALL ON `laraveldemo.*` TO 'laraveldemouser'@'%';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
Add database connection details in Laravel project’s .env file.
$ sudo vi /var/www/html/demo/.env
Set database connection details. Replace database, username and password with your values.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laraveldemo
DB_USERNAME=laraveldemouser
DB_PASSWORD=password
Save and exit the file. That’s it. Now Laravel application is configured to work with NGINX and MySQL in Ubuntu Linux.
Also read : How to Serve Static Files in NodeJS using NGINX