HostUp

Install NGINX with PHP 7.3 and MariaDB on Ubuntu

Step 1: Install Nginx

To install the current version of Nginx, simply follow the documentation on its offical website seen here or do the following commands:

Firstly we need to install ca-certificates so we can verify that we downloaded the correct file from a trusted source.

sudo apt install curl gnupg2 ca-certificates lsb-release

Then we need to add Nginx mainline package to our repositories

echo "deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \     | sudo tee /etc/apt/sources.list.d/nginx.list

Now verify that we downloaded the correct file

curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add - 

If it prints out OK! then you are good to go!

Now that we have downloaded & verified its authnicity lets install it!

sudo apt update

Install Nginx.

sudo apt install nginx

That’s it! You have now installed the latest release of Nginx. You should now start it!

sudo systemctl start nginx.service

and make it start on reboot.

sudo systemctl enable nginx.service

Visit your servers IP address in your webbrowser. You should now see something along these lines if it is working correctly.

welcome to nginx, default index.html
Default index.html in NGINX

Good to know:

Your files are located by default in NGINX in /usr/share/nginx/html.

Step 2: Install MariaDB

To install the server & client:

sudo apt-get install mariadb-server mariadb-client

Enable it on restart:

sudo systemctl enable mysql.service

Start MariaDB:

sudo systemctl start mysql.service

Setup MariaDB:

sudo mysql_secure_installation

You will now be prompted with a configuration setup file. Please read through it and configure it to your liking. It is recommended to disable logins that are not from localhost.

  • Enter current password for root (enter for none): press Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • 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

Step 3: Create your Database

After this you may create or import your database from another source.

Login into MySQL:

sudo mysql -u root -p

You can create your database by running the following commands. We will call it joomla although you are free to call it whatever you want.

Create a new user:

CREATE USER 'changeme'@'localhost' IDENTIFIED BY 'password_here';

Assign the user to the joomla database

GRANT ALL ON changeme.* TO 'changeme'@'localhost' IDENTIFIED BY 'password_here' WITH GRANT OPTION;

Now you have created a database with its own user and password. However if you want to import a database from another source you need to follow this additional step:

Importing SQL file

First lets go to the root directory

cd

After this you need to upload your database dump to here. I prefer using SFTP for this. Filezilla is my favorite SFTP software.

If your username is root on the VPS then go to /root directory in the SFTP. Once there upload your .sql file.

To import a database:

mysql -u username -p database_name < file.sql 

Step 4: Install PHP 7.3

To add the repositories for PHP:

sudo apt-get install software-properties-common

To add the 2nd repository:

sudo add-apt-repository ppa:ondrej/php

Run this to download your added repositories:

sudo apt update

We can now install PHP now that we have all of the required repositories.

sudo apt install php7.3-fpm php7.3-common php7.3-mysql php7.3-gmp php7.3-curl php7.3-intl php7.3-mbstring php7.3-xmlrpc php7.3-gd php7.3-xml php7.3-cli php7.3-zip

It is recommended to raise the memory limit and disable cgi.fix_pathinfo. Your PHP configuration is located in/etc/php/7.3/cli/php.ini.

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

Press CTRL + W and search for ;cgi.fix_pathinfo=1

Replace it with cgi.fix_pathinfo=0

Save & exit by pressing CTRL + X followed by Y

Step 6: Configure Nginx

Add nginx to www-data group

sudo usermod -a -G www-data nginx

Change owner of directory to www-data

sudo chown -R www-data /usr/share/nginx/html

Go into your default.conf file

sudo nano /etc/nginx/conf.d/default.conf

Replace your existing configuration file with the one below:

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;

   location / {
    if ($request_uri ~ ^/(.*)\.html$) {
        return 302 /$1;
        }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
        location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

Running multible websites

Alternatively if you are running mulitble websites with different content on them you need to do a little more. If you only have one website, or multible domains pointing to the same website/content you can skip this step. Otherwise, here is an example configuration of two websites running different content:

server {
    listen       80;
    server_name  website1.com www.website1.com;

    root   /usr/share/nginx/html/website1.com;
    index  index.php index.html index.htm;

   location / {
    if ($request_uri ~ ^/(.*)\.html$) {
        return 302 /$1;
        }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
        location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

server {
    listen       80;
    server_name  website2.com www.website2.com;

    root   /usr/share/nginx/html/website2.com;
    index  index.php index.html index.htm;

   location / {
    if ($request_uri ~ ^/(.*)\.html$) {
        return 302 /$1;
        }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
        location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

As you can see, both the server_name and the root directory are different. Replace “website1.com, website2.com” with your respective domain names.

You need to create a directory for them both as shown in the root directory.

mkdir /usr/share/nginx/html/website1.com
mkdir /usr/share/nginx/html/website2.com

If you want to add https to your website(s) then follow this tutorial:

David

Want Ubuntu tutorials that actually work? Then you have found the right person

Leave a Reply

avatar
  Subscribe  
Notify of

Most popular

Most discussed