How To Install Laravel With Nginx on Ubuntu 22.04 LTS
In this tutorial, we will show you how to install Laravel With Nginx on Ubuntu 22.04 LTS. For those of you who didn’t know, Laravel is a popular open-source PHP framework for developers looking to build modern web applications based on PHP. It aims to help developers build complex and straightforward applications by making frequently used application tasks (like caching and authentication) easier.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of the Laravel PHP framework With Nginx on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.
Prerequisites
- A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Laravel With Nginx on Ubuntu 22.04 LTS Jammy Jellyfish
Step 1. First, make sure that all your system packages are up-to-date by running the following apt
commands in the terminal.
sudo apt update sudo apt upgrade
Step 2. Install the LEMP stack server.
A Ubuntu 22.04 LEMP stack server is required. If you do not have LEMP installed, you can follow our guide here.
Step 3. Configuring MariaDB for Laravel.
By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation
script. You should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB.
mysql_secure_installation
Configure it like this:
- Set root password? [Y/n] y - Remove anonymous users? [Y/n] y - Disallow root login remotely? [Y/n] y - Remove test database and access to it? [Y/n] y - Reload privilege tables now? [Y/n] y
Next, we will need to log in to the MariaDB console and create a database for Laravel. Run the following command:
mysql -u root -p
This will prompt you for a password, so enter your MariaDB root password and hit Enter. Once you are logged in to your database server you need to create a database for Laravel installation:
mysql> CREATE DATABASE laravel; mysql> GRANT ALL ON laravel.* to 'laravel'@'localhost' IDENTIFIED BY 'your_strong_password'; mysql> FLUSH PRIVILEGES; mysql> quit
Step 4. Installing PHP Composer.
The Composer is required for installing Laravel dependencies. Run the following command to install composer (a dependency manager for PHP) on Ubuntu 22.04 system
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer sudo chmod +x /usr/local/bin/composer
Verify the Composer version:
composer --version
Step 5. Installing Laravel on Ubuntu 22.04.
By default, Laravel is not available on Ubuntu 22.04 base repository. Now run the following command below to create a Laravel project with the Composer:
cd /var/www/html composer create-project --prefer-dist laravel/laravel your-domain.com
Next, set permissions on the Laravel directory using the following command:
sudo chown -R :www-data /var/www/html/your-domain.com/storage/ sudo chown -R :www-data /var/www/html/your-domain.com/bootstrap/cache/ sudo chmod -R 0777 /var/www/html/your-domain.com/storage/
The default .env
contains a default application key but you need to generate a new one for your laravel deployment for security purposes:
sudo php artisan key:generate
Then, configure the Laravel database connection details in .env
as shown in the file:
sudo nano /var/www/html/your-domain.com/.env
Step 6. Configure Nginx For Laravel.
Now we create a vhost for Nginx configuration, under the /etc/nginx/sites-available/
directory:
sudo nano /etc/nginx/sites-available/your-domain.com.conf
Add the following file:
server{ server_name www.your-domain.com; root /var/www/html/your-domain.com/public; index index.php; charset utf-8; gzip on; gzip_types text/css application/javascript text/javascript application/x-javascript image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php { include fastcgi.conf; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/run/php/php7.4-fpm.sock; } location ~ /\.ht { deny all; } }
Save and close the file, then restart the Nginx service for the changes to take effect:
sudo ln -s /etc/nginx/sites-available/your-domain.com.conf /etc/nginx/sites-enabled/ sudo rm /etc/nginx/sites-enabled/default sudo nginx -t sudo systemctl restart nginx
Step 7. Configure Firewall.
By default, the UFW firewall is enabled on Ubuntu. Depending on your Laravel configuration file, open ports 80 and 443 to allow HTTP and HTTPS traffic:
sudo ufw allow 'Nginx FULL' sudo ufw enable sudo ufw status
Step 8. Accessing Laravel Web Interface.
Once successfully installed, open your web browser and access the Laravel web interface using the URL http://your-domain.com
. You will be redirected to the following page:
Congratulations! You have successfully installed Laravel. Thanks for using this tutorial for installing the Laravel PHP framework With Nginx on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the official Laravel website.