How to Install Nginx, MariaDB and PHP7 on Ubuntu 16.04 LTS

 In Servers Admin, Servers Tutorials

Step 1: Update Ubuntu 16.04 LTS

Before we install any software, it’s always a good idea to update repository and software packages. SSH into your Ubuntu 16.04 server and enter the below commands.

sudo apt update

sudo apt upgrade

Step 2: Install Nginx Web Server

Nginx is a high performance web server. It also can be used as a reverse proxy. Enter this command to install Nginx Web server.

sudo apt install nginx
After it’s installed, we can enable Nginx to auto start when Ubuntu is booted by running the following command.

sudo systemctl enable nginx

Then start Nginx with this command:
sudo systemctl start nginx

Now check out its status.
systemctl status nginx

Output:

systemctl-status-nginx

enabled” indicates that auto start at boot time is enabled and we can see that Nginx is running.

Now in your browser’s address bar, type the public IP address of Ubuntu 16.04 LTS server. You should see the “Welcome to nginx!” Web page which means Nginx Web server is running correctly.

welcome-to-nginx

Finally, we need to make www-data (Nginx user) as the owner of web root directory.

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

Step 3: Install MariaDB

MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who concerned that Oracle might turn MySQL into a closed-source product. Many Linux distributions (Arch Linux, Fedora etc), companies and organizations (Google, Wikipedia) have migrated to MariaDB. So we’re going to install MariaDB instead of MySQL.

sudo apt install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically started. Use systemctl to check its status.
systemctl status mysql
Output:

maria-db

If it’s not running, start it with this command:
sudo systemctl start mysql

To enable MariaDB to automatically start when Ubuntu 16.04 is rebooted:
sudo systemctl enable mysql

Now run the post installation security script.
sudo mysql_secure_installation

When it asks you to enter MariaDB root password, press enter because you have not set the root password yet. Then enter y to set the root password for MariaDB server.

setup-maria-db-1

Next you can just press Enter to answer all the remaining questions. This will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security.

setup-maria-db

Step 4: Install PHP7

Enter the following command to install PHP7 and PHP7 extensions.

sudo apt install php7.0-fpm php7.0-mbstring php7.0-xml php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl

Now start php7.0-fpm.
sudo systemctl start php7.0-fpm

Check status:
systemctl status php7.0-fpm

Sample output:

install-php-7

Step 5: Create a Default Nginx Server Block File

Remove the default symlink in sites-enabled directory.
sudo rm /etc/nginx/sites-enabled/default
Then create a new default server block file under /etc/nginx/conf.d/ directory.

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

Paste the following text into the file. Replace 12.34.56.78 with your actual server IP.

server {
listen 80;
listen [::]:80;
server_name 12.34.56.78;
root /usr/share/nginx/html/;
index index.php index.html index.htm index.nginx-debian.html;

location / {
try_files $uri $uri/ =404;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root /usr/share/nginx/html;
}

location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
}

location ~ /\.ht {
deny all;
}
}

Save and close the file. Then test nginx configuration and reload it.
sudo nginx -t sudo systemctl reload nginx

Step 6: Test PHP

To test the cli version of PHP7, we just need to enter this command:
php --version

Sample output:

php-version

To test PHP-FPM, first create a test.php file in the Web root directory.

sudo nano /usr/share/nginx/html/test.php

Paste the following PHP code into the file.
<?php phpinfo(); ?>

Save and close the file. Now in the browser address bar, enter server-ip-address/test.php. Replace server-ip-address with your actual IP. You should see your server’s PHP information. This means PHP processing is fine.

php-info

How to Install PHP7.2

PHP7.2 is the latest stable version of PHP, released on November 30, 2017 and it has minor performance boost over PHP7.0. We can add the PPA from Ondrej Sury to install PHP7.2 on Ubuntu 17.10. That guy is also the maintainer of Certbot PPA.

sudo apt install software-properties-common

sudo add-apt-repository ppa:ondrej/php -y 

sudo apt update

Then we can install PHP7.2 and common extensions by using the following command.

sudo apt install php7.2 php7.2-fpm php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl

Now start PHP7.2-FPM.

sudo systemctl start php7.2-fpm

Enable auto-start at system boot time.

sudo systemctl enable php7.2-fpm

Check its status:

systemctl status php7.2-fpm

STEP 7:Using PHP7.2-FPM with Nginx

To make Nginx use PHP7.2-FPM instead of PHP7.0-FPM, we need to edit the Nginx server block file.

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

Find the following line.

fastcgi_pass unix:/run/php/php7.0-fpm.sock;

Change php7.0-fpm to php7.2-fpm.

fastcgi_pass unix:/run/php/php7.2-fpm.sock;

Save and close the file. Then reload Nginx for the change to take effect.

sudo systemctl reload nginx

If you refresh the server-ip-address/info.php page, you will see Nginx is now using PHP7.2-FPM.

php7-2-info

For your server’s security, you should delete test.php file now.
rm /usr/share/nginx/html/test.php

Congratulations you have successfully installed Nginx, MariaDB and PHP-7 & PHP-7.2 on Ubuntu 16.04 (AWS EC2).

You may also want to instal phpmyadmin on server for managing MariaDB databases, please click on the link below and you will find the tutorial for it .

How to Install phpMyAdmin with Nginx, MariaDB, PHP7 on Ubuntu 16.04

 

How to install WordPress on Ubuntu 16.04.

How to Install WordPress on Ubuntu 16.04 LTS with Nginx, MariaDB, PHP7.2

You may also be interested to know in how to add free SSL in your website .

Install Let’s Encrypt on Ubuntu 16.04 with Nginx

Recent Posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Start typing and press Enter to search

Let's work together

I'd love to work with you! Please fill out my project form to tell me more about your project/work. I reply to all enquiries within 48 hours.

Availability

  • June - Booked
  • July - Booked
  • August - Limited

Rates

  • My day rate is £300.
  • I can provide a fixed price for your work.
  • Projects start at around £2,000.

Header Enquiry Form

  • This form collects your name, email address and telephone number so that I can respond to the enquiry(ies) that you submit via this website. Please check my privacy policy for the full story on how I use, protect and manage your submitted data.