Laravel

How to install Laravel with NGINX on Ubuntu

installing laravel with nginx on ubuntu

Laravel is the most popular PHP MVC framework. If you have chosen a VPS or cloud hosting to deploy your laravel website, this post will help you install Laravel with NGINX on Ubuntu.

PHP 7.4 Ubuntu 20.04 Laravel 8.x

PHP and PHP-FPM

PHP-FPM is an advance level FastCGI process manager. More information on PHP-FPM is here. To install laravel with NGINX on Ubuntu, you must have PHP-FPM installed.

Open Ubuntu terminal and run following commands:

This should install latest version of PHP and PHP-FPM. You can verify the same by using php -version. The status of PHP-FPM can be checked with systemctl status php7.4-fpm

Installing common PHP modules and Laravel dependencies

Let’s find some commonly used PHP modules. I found information at this link useful. Let’s install all these common modules.

Now, we need to make sure that all the PHP modules needed for Laravel (8.x) to work are also installed.

Verify that all the modules are installed by running php -m

Setting up Composer on Ubuntu

As NPM is for NodeJS and PIP is for Python, PHP has it’s dependency manager in the shape of Composer. Install Composer on Ubuntu with following simple steps:

  1. sudo apt install curl
  2. curl -sS https://getcomposer.org/installer | php
  3. sudo mv composer.phar /usr/local/bin/composer

Add Laravel installer through composer

Let’s add laravel installer through composer.

Laravel installer is now available to use, but possibly Ubuntu shell doesn’t know the directory to execute laravel installer from.

To make Ubuntu recognize laravel installer, add composer’s system-wide vendor bin directory to the PATH variable.

It is important to note here that the location of composer’s vendor bin directory is different for each operating system (My one is Ubuntu 20.04)

After setting the PATH variable, we can now run laravel new first-laravel-app. Here, first-laravel-app is the name of the project and could be anything of your choice.

cd into first-laravel-app and run php artisan serve.

We can now see the laravel development server running at http://127.0.0.1:8000/. Go ahead and check if all looks good. The purpose of this post is not to show how a development server will run laravel app but how to install laravel with NGINX on Ubuntu.

Install NGINX on Ubuntu

NGINX is a powerful web server with load balancing support. NGINX serves PHP through PHP-FPM. As we have already setup PHP and PHP-FPM, let’s go ahead and install NGINX on Ubuntu.

Ideally, all the websites on the server will have it’s own NGINX configuration in /etc/nginx/conf.d directory.

As we have a fresh install of NGINX on Ubuntu the conf.d directory is empty.

To serve laravel app through NGINX, let’s create a simple NGINX conf file for laravel app inside /etc/nginx/conf.d. You can name this file anything but it should end with .conf extension. NGINX will automatically pick every file stored in /etc/nginx/conf.d directory. This is because of following line placed in /etc/nginx/nginx.conf

I have picked the simple laravel nginx conf file from laravel’s official deployment page.

The only line you need to change is root /home/zohaib/websites/first-laravel-app/public;

I am on localhost and therefore have server_name directive set to 127.0.0.1. Usually, the server_name directive should have your original domain name. The server_name directive tells nginx to use the current nginx file if the requested server name matches the server_name directive. server_name directive could look like below for live environment:

You can validate nginx configurations with sudo nginx -t. Make sure that NGINX and PHP-FPM services are running with sudo systemctl status nginx and sudo systemctl status php7.4-fpm respectively.

Now, open 127.0.0.1 in your browser and you should see Laravel welcome page. If you see any permissions error on storage directory, change the root directory permissions as below:

Conclusion

We have seen how to install Laravel with NGINX on Ubuntu. The process should be straight forward for you. You might run into some permissions and PATH settings issues. In such cases, you should find appropriate settings for your variant of Ubuntu.

I have also written a beginner installation guide on windows here.

If you have liked this post, Kindly consider sharing it and subscribing to my blog.

Tagged , , ,

About Zohaib Shah

Passionate software engineer with expertise in Django, Laravel and NodeJS. Working with different SaaS based products and API connected apps. Software Architect of UK's leading online travel agency. Get in touch
View all posts by Zohaib Shah →

2 thoughts on “How to install Laravel with NGINX on Ubuntu

  1. Thanks! This is pretty clear and helped a lot!
    Just an addition, I faced few simple issues that you may want to add to this guide:
    1- By default apache 2 is enabled in Ubuntu 21, therefore you get a port conflict. This can be solved by running sudo systemctl disable apache2 in order to disable apache (even on a system restart). You may need to restart nginx.
    2- If you are testing this online with no domain you can serve the app with an IP by running php artisan serve --host=123.123.123.123 --port=8000 where the host option is your public IP.

    I’ve tested this on Digital Ocean and works like a charm.
    Thanks again!

Leave a Reply

Your email address will not be published.