Install Laravel 5.7, PHP 7.3 and NGINX on Ubuntu

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, avr: 5.00)

Laravel is a prominent member of a new generation of web frameworks. It is a free, open-source PHP web framework, created by Taylor Otwell back in 2011 and intended for the development of web applications following the MVC model.

Prerequisites

Before starting with this guide, it’s better you need to have domains or subdomain name pointing to your public server IP. on the following tutorial we will use lab.axfon.com. If you do not have public IP or domain you may also follow this tutorial using local server.


Step 1: Install NGINX as Web Server

Now we are going to install NGINX stable version by default repository Ubuntu, to do this type the command below:

sudo apt update && sudo apt install nginx

Once NGINX already installed, to check and verify Nginx service status type the command below

sudo service nginx status

If it is running and working properly you will see the output NGINX active (running) like below

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-01-03 21:00:12 UTC; 5h 3min ago
     Docs: man:nginx(8)
  Process: 3083 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, stat
  Process: 3068 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exi
 Main PID: 3087 (nginx)
    Tasks: 2 (limit: 665)
   Memory: 4.4M
   CGroup: /system.slice/nginx.service
           ├─3087 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─3088 nginx: worker process
Jan 03 21:00:12 axfon-lab systemd[1]: Starting A high performance web server and a reverse pr
Jan 03 21:00:12 axfon-lab systemd[1]: nginx.service: Failed to parse PID from file /run/nginx
Jan 03 21:00:12 axfon-lab systemd[1]: Started A high performance web server and a reverse pro

To exit Nginx service status just press q on the keyboard


Step 2: Configuration the Firewall with UFW

UFW (Uncomplicated Firewall) is an Iptable interface to easily configure a firewall on your system. If it does not yet enable, it’s recommended to enable and setup the rule for Nginx

For the first, we have to add rule for SSH, let’s se the command line below

sudo ufw allow OpenSSH

Add the rule receive HTTP to Nginx

sudo ufw allow 'Nginx HTTP'

Then you will see on terminal

Rule added
Rule added (v6)'

Now enable ufw for Firewall.

sudo ufw enable

If prompted just pres Y to accept and continue and to check UFW status type the command below

sudo ufw status

You will see the output UFW active (running) like below

Status: active
To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

NGINX web server now is ready on Ubuntu, now you may go to your web browser and visit your domain or IP. If you have not configured domain name yet and don’t know your IP, use the following command find out

sudo ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

If everything is OK you will be presented on your browser with the default NGINX landing page as below


Step 3: Install PHP 7.3 FPM and Related Modules

When this tutorial made the PHP 7.3 is not yet available in Ubuntu default repositories, for installing it we have to add manually ondrej/php PPA by running the following command

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

After adding the PPA we need to update Ubuntu, run the following command

sudo apt-get update

Once the PPA repository has been added and updated, now we are going to install PHP 7.3

sudo apt-get install php7.3-fpm php7.3-cli php7.3-mysql php7.3-gd php7.3-imagick php7.3-recode php7.3-tidy php7.3-xmlrpc php7.3-common php7.3-curl php7.3-mbstring php7.3-xml php7.3-bcmath php7.3-bz2 php7.3-intl php7.3-json php7.3-readline php7.3-zip

To verified PHP 7.3 was installed correctly on the web server, you may check the PHP version

sudo php -v

You will be presented on the terminal screen as below

PHP 7.3.0-2+ubuntu18.10.1+deb.sury.org+1 (cli) (built: Dec 17 2018 09:23:19) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.0-dev, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.0-2+ubuntu18.10.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

The default location of the PHP socket that Nginx List the contents for the directory /var/run/php/

ls /var/run/php/

You will see the output like below, note this below we are going to use for the next server block configuration

php7.3-fpm.pid php7.3-fpm.sock

Step 4: Configure PHP file (Optional)

The php.ini file is a default configuration file that read on PHP starts up. If you want to change the PHP settings on Ubuntu, Follow the below steps to modify the PHP configuration file php.ini using the command line on Ubuntu.

sudo nano /etc/php/7.3/fpm/php.ini

To edit the configuration file your may find or you can use Cmd/Ctrl W to search and replace it as the following

error_reporting = E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR
max_input_time = 30
error_log = /var/log/php/error.log
file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Los_Angeles

Press Ctrl/Cmd + X and then press Y and ENTER to save changes


Step 5: Create Directory and Grant Permission

Set permission ownership for the current user to be able to modify any files in these directories. The $(whoami) variable will take the value of the user you are currently logged in.

sudo chown -R $(whoami):$(whoami) /var/www/html/

Grant Directory Permission

sudo chmod -R 755 /var/www

Step 6: Install Composer

To install composer globally, download the Composer installer using curl and directly move into directory /usr/local/bin

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Now your composer installed successfully to /usr/local/bin/composer

All settings correct for using Composer
Downloading...
Composer (version 1.8.0) successfully installed to: /usr/local/bin/composer                  
Use it: php /usr/local/bin/composer

Step 7: Install Laravel 5.7

Install Laravel into the default directory /var/www/html Run the Composer create-project command to install Laravel in the directory lab-axfon (Change with your own prefer name)

cd /var/www/html
composer create-project --prefer-dist laravel/laravel lab-axfon

After running the commands above, a new project directory will be created… Run the commands below to set the correct permissions for that directory

sudo chown -R www-data:www-data /var/www/html/lab-axfon/
sudo chmod -R 755 /var/www/html/lab-axfon/

Step 8: Configuration Server Blocks

Nginx Server Blocks allows to run or host one or multi-domain on a single server. Nginx contains a default server block in /etc/nginx/sites-available/default.

sudo nano /etc/nginx/sites-available/default

Let’s see below configuration, on the red line you may change server_name with your IP address or your domain and add index.php after index

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/lab-axfon/public;
    index index.php index.html index.htm;
    server_name lab.axfon.com;

    location / {
         try_files $uri $uri/ /index.php?$args;        
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

Press Ctrl/Cmd + X and then press Y and ENTER to save changes

Changes made in the NGINX configuration file will not be applied until the command to Restart and Before testing on the Browser we have to verify and make sure no error.

sudo service nginx restart

Step 9: Testing Laravel on the Browser

You can now view this page in your web browser by visiting your server’s domain name or public IP address http://your_domain_or_IP/


If this tutorial could help you, please rate above Star button rating and share to help others find it! Feel free to leave a comment below.

Recommended For You

Axfon

About the Author: Axfon

Axfon.com is a site Online publisher for collection of guides and tutorials about Internet Technology