How to Install Laravel on Ubuntu 22.04 with Nginx
How To Install and Configure Laravel with Nginx on Ubuntu 22.04
Just follow the following steps to install and configure laravel with Nginx on ubuntu 22.04:
- Step 1 – Install Required PHP Modules
- Step 2 – Creating a Database for Laravel
- Step 3 – Install Composer in Ubuntu 22.04
- Step 4 – Install Laravel in Ubuntu 22.04
- Step 5 – Configure Laravel in Ubuntu 22.04
- Step 6 – Configure NGINX to Serve Laravel Application
- Step 7 – Accessing Laravel Application from a Web Browser
Step 1 – Install Required PHP Modules
First of all, install required php modules, so open command prompt and execute the following command to install required php modules:
$ sudo apt update $ sudo apt php-common php-json php-mbstring php-zip php-xml php-tokenizer
Step 2 – Creating a Database for Laravel
Create a MySQL database for Laravel application; so execute the following command on command line to create database for laravel app:
$ sudo mysql MariaDB [(none)]> CREATE DATABASE laraveldb; MariaDB [(none)]> GRANT ALL ON laraveldb.* to 'webmaster'@'localhost' IDENTIFIED BY 'tecmint'; MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> quit
Step 3 – Install Composer in Ubuntu 22.04
Then use 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
Step 4 – Install Laravel in Ubuntu 22.04
Once the composer installation has been done; execute the following command on command line to install laravel apps in ubuntu 22.04 system:
$ cd /var/www/html $ composer create-project --prefer-dist laravel/laravel example.com
Step 5 – Configure Laravel in Ubuntu 22.04
Now configure laravel apps using the following commands:
Set permissions on the Laravel directory using the following command:
$ sudo chown -R :www-data /var/www/html/example.com/storage/ $ sudo chown -R :www-data /var/www/html/example.com/bootstrap/cache/ $ sudo chmod -R 0777 /var/www/html/example.com/storage/
he 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
We also need to configure the Laravel database connection details in .env
as shown in the following screenshot.
$ sudo nano /var/www/html/example.com/.env
Step 6 – Configure NGINX to Serve Laravel Application
Create a server block for it within the NGINX configuration, under the /etc/nginx/sites-available/
directory:
$ sudo nano /etc/nginx/sites-available/example.com.conf
Also, set the fastcgi_pass
directive should point to the medium PHP-FPM is listening on for requests (for example fastcgi_pass unix:/run/php/php7.4-fpm.sock
):
server{ server_name www.example.com; root /var/www/html/example.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 the file and then enable the Laravel site configuration by creating a link from /etc/nginx/sites-available/example.com.conf
to the /etc/nginx/sites-enabled/
directory. Besides, remove the default server block configuration.
$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/ $ sudo rm /etc/nginx/sites-enabled/default
Next, check if the NGINX configuration syntax is correct by running the following command before restarting the service.
$ sudo nginx -t $ sudo systemctl restart nginx
Step 7 – Accessing Laravel Application from a Web Browser
Now open a web browser on the local computer and use the following address to navigate.
http://www.example.com/
Conclusion
Through this tutorial, we have learned how to install and configure laravel on ubuntu 22.04 with nginx.