Ubuntu 18.04 Install NGINX PHP MySQL on Google Cloud Compute Engine

Ubuntu 18.04 Install NGINX PHP MySQL on Google Cloud Compute Engine

In this article, we will learn How to install NGINX, PHP, and MySQL on Ubuntu 18.04? 
In this article and the next few articles, we learn How to set up a NGINX web server. Following are the key points which we cover 

  1. Install Nginx, PHP, MySQL, Secure MySQL. 
  2. Install and secure phpMyAdmin. 
  3. Nginx custom error page. 
  4. Configure a Domain, Create NGINX Server Block. 
  5. Install SSL Certificates 
  6. Enable GZIP compression 
  7. MySQL Remote access from Ubuntu 18.04 

To cover the following points, first, we need to create a new Compute Engine VM instance on Google Cloud Compute Engine. 

Step 1 Create a new Ubuntu 18.04 VM instance on Google Cloud Compute Engine. 

To create a new Google Cloud Compute Engine VM instance first we need to visit cloud.google.com. I am assuming you already have a valid cloud.google.com account. If you have a valid account, click on Go to Console. Then select Compute Engine > VM Instance. 
On VM Instance Page creates a new VM Instance 
For me, I used the following settings 

Instance Name: Any name 
Region and Zone: Based on your audience 
CPU: 1 Micro CPU 
Memory: 0.6 GB 
Boot Disk: Ubuntu 18.04 
Disk Size: 10 GB 
Allow HTTP and HTTPS traffic.

Create a new Ubuntu 18.04 VM Instance

Our server is up and running, let's visit the server by visiting the server IP. 

Currently, our server doesn't know how to Handle the HTTP request. To resolve this issue we need to first install NGINX. To install NGINX we need to open Ubuntu terminal in the browser.

Open SSH in Browser

Ubuntu terminal in Browser

Step 2 Install NGINX

Next, we need to install NGINX. To install NGINX on Ubuntu 18.04 execute following commands on the terminal to update the repository followed by NGINX installation. 


sudo apt update

sudo apt install nginx

You can check NGINX status by executing command "sudo systemctl status nginx" in the terminal.

If NGINX is running you can saw a default NGINX web page by visiting the server address.

Step 3 Install and Configure PHP

Next, we need to install and configure PHP. To install PHP execute following command in the terminal. The following command will install and PHP and MySQL components for PHP.


sudo apt install php-fpm php-mysql

PHP installation is completed successfully. Let's test it by creating a PHP file in the default NGINX html directory.

NGINX default directory available at "/var/www/html". In the directory, we have a default html file with name "index.nginx-debian.html".

Remove the file by executing "sudo rm /var/www/html/index.nginx-debian.html" command in the terminal. After deleting the default html file we need to create a new index.php file. To create a new index.php file, execute command "sudo nano /var/www/html/index.php" in the terminal.

Next, in the nano editor, add "<?php echo 'Hello, vLemonn';" code. To save the file, press Ctrl+O from your keyboard. To close the file, press Ctrl+X from your keyboard.

Our first PHP file is created successfully. To test visit http://<ServerIP>/index.php. On visiting the URL browser start downloading the PHP File. Why?

Because currently, our NGINX doesn't know how to process the PHP file. To fix the issue, we need to edit the NGINX server configuration.

To fix the issue for NGINX, not processing the PHP file we need to edit the default Nginx website config file. The default config file is available at "/etc/nginx/sites-enabled/default". To edit the default, we need to use the nano editor. The following command will open the default file in the nano editor.


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

You need to uncomment the Highlighted lines. Ensure the PHP version and path is correct.

PHP FPM Version and path for us.

Next, we need to test the NGINX configuration. The following command is used to test the NGINX configuration. 

sudo nginx -t

If the output of the command is OK, then restart the NGINX services and again visit at http://<Server IP>/index.php

sudo service nginx restart

Step 4 Install MySQL

We are done with NGINX and PHP installation. Next, we need a database server to store our data. For this, we will install and configure MySQL on our Ubuntu 18.04 Server. To install MySQL execute the following command in the terminal.


sudo apt install mysql-server-5.7

Step 5 Configure and Secure MySQL Installation

After MySQL installation, it is recommended to configure and secure the MySQL installation
To configure and secure MySQL installation, execute the command “sudo mysql_secure_installation” in the terminal and follow the instructions.

The first configuration step is to enable VALIDATE PASSWORD PLUGIN. It can be used to test password strength and improve security. For the production environment, it needs to be enabled. Type Y to enable it and hit enter.
If you selected Y then you need to select the password policy. For this, we have three options. I am recommended to select at least medium for Password Policy. Following are the available options.

There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
Regardless whether you selected Y or N for the above configuration step, next you need to provide the MySQL root user password. Enter the password and confirm the password. If you enable the VALIDATE PASSWORD PLUGIN then your password must be based on the selected password validation rule. Also, it will calculate the password strength. 100 means it is a good password. Confirm your password to go to the next configuration Step.

Next Question "Remove anonymous users?".

What are anonymous users?

An anonymous user or account is available in MySQL. It allows anyone to gain access into the MySQL Server without having an account. Anonymous user accounts are available for testing purpose. We don't want such account on our production environment. Type Y and hit enter for this question.

Disallow root login remotely?

For the MySQL root, the user has all the privileges on the database server. If anyone can guess the correct password for the root user, he or she can do anything on the database server. To make your production MySQL database server secure, you must need to disable the root login for the remote users. 

Remove test database and access to it? 

By default, MySQL comes with a database named 'test1 that anyone can access. This is also intended only for testing and should be removed before moving into a production environment.

Reload privilege tables now? 

To ensure all the changes made so far will take effect immediately we need to reload the privilege tables. It is the last step in the MySQL initial configuration for the production environment.

Step 6 Create a MySQL User for other applications
Our LEMP installation is completed successfully. In MySQL 5.7 and newer by default, the root user is set to authenticate using "auth_socket" plugin rather than with a password.  It improves security and usability, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) to access the MySQL Server. 

To allow easy access to the MySQL database server, we will create a new admin user with mysql_native_password. We will use the new user account for all the external applications.

To create the new user account open MySQL shell by executing the following command in the terminal.


sudo mysql

In MySQL shell execute the following SQL query. The first query will create the new user. The second query will grant all access to the new user.


CREATE USER 'mayanksanghvi'@'localhost' IDENTIFIED BY '[email protected]$$w0rd';


We are done with the initial server setup. We will learn more about the following topics in our next article.

Leave A Comment

Please login to post your valuable comments.

Join the newsletter

Get the latest vLemonn news first