Install and Configure Powerful LEMP Stack on Ubuntu Cloud Server

Learn how to install and configure a powerful Linux, Nginx, MySQL and PHP (LEMP Stack) on Ubuntu Cloud Server via SSH command line. Also added with bonus performance tips!

One of the best uses of Ubuntu cloud server is to host a web application or run online publishing software like WordPress on LEMP Stack.

As I’ll mainly focus on deploying WordPress CMS, the below tutorial will help in setup and configuring a powerful LEMP stack on Ubuntu Cloud Server.

Before getting started, just make sure that you’re login SSH and SFTP client. As the following guide will be too technical and will be only using the SSH and SFTP to install and config LEMP Stack remotely.

LEMP Stack on Ubuntu Cloud Server

Just in case if you’re not aware, this is the third article and video demo of #CloudServer Series. We previously posted on how to login SSH and SFTP client in Ubuntu Server.

Let start off with learning about LEMP stack —

What is the LEMP Stack?

DigitalOcean defines it as:

The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications. This is an acronym that describes a Linux operating system, with an Nginx web server. The backend data is stored in the MySQL database and the dynamic processing is handled by PHP.

Linux Operating System:

Ubuntu is a Linux distribution based operating system. Apart from Ubuntu Linux, there are a couple of other famous OS under Linux umbrella – Fedora, Arch Linux, Linux Mint, Debian, CentOS, etc.

Being open sources, Linux is one of the highly preferred Server Operating System.

Nginx Web Server:

Nginx is an HTTP and reverse proxy server along with mail proxy and TCP/UDP proxy. It also supports front-end just like Apache. However, compared to Apache it’s really fast and lightweight in serving the static content.

Nginx is highly scalable and utilizes less resource compare to Apache, making it #1 choice for server administrators and web application developers.

MySQL Database Server:

MySQL Server is the most popular open source relational database management system based on structured query language by Oracle.

MySQL is mainly used for storing and retrieving the data in the SQL tables. Prominently used by the web application and online publishing tools including WordPress.

PHP Processor:

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

PHP is the server-side scripting language and a powerful tool for making dynamic and interactive web content.

How to install Nginx on Ubuntu Cloud?

Nginx is the Web Server that helps in displaying the content on the requestors web browser. Hence we need to make sure that Nginx is properly setup and configure.

Let’s start off with updating all the upgradable packages.

sudo apt-get update
sudo apt-get upgrade -y

Install the latest version of Nginx on Ubuntu using the following command:

sudo apt-get install nginx

The above command will install Nginx web server and all supporting packages.

Install Nginx on Ubuntu for LEMP Stack

Now run below command to restart nginx for the first time:

sudo service nginx restart

After restarting, you can visit your IP address in the web browser to verify whether the Nginx is running. You will be able to see the message like “Welcome to nginx!”

Welcome to nginx! message on IP Address

We will be configuring the Nginx to user PHP processor further in this guide, till then proceed to MySQL database installation.

How to install MySQL database?

In order to run any database required web application like WordPress CMS, we need a database server on the server. There are a couple of database server (like MariaDB, MySQL) available on the internet, however, my personal preference is always MySQL.

By default, if you install the MySQL with command line without naming the version, it will install the MySQL version 5.7.

However, I believe MySQL 8.0 is quite faster compared to the legacy version in terms of speed and database query processing. Hence, we will be using MySQL 8.0 which is supported by all major web programs including the WordPress CMS.

Let’s add the MySQL 8.0 into our apt repository running the below command:

wget –c https://dev.mysql.com/get/mysql-apt-config_0.8.10-1_all.deb

MySQL 8.0 downloaded in Ubuntu Server

Now configure the MySQL package using dpkg command:

sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb

This command will ask you to select & verify the MySQL Server. If the current value is set to mysql-8.0. Then just navigate to Ok command and hit [Enter].

Configure MySQL Server to mysql-8.0 version using dpkg command

Now the default MySQL installation package is changed from version 5.7 to 8.0.

Run the below command to update the apt library and proceed with the MySQL 8.0 installation.

sudo apt-get update
sudo apt-get install mysql-server -y

Through the installation process, you’ll be asked to set up the MySQL root password. For security, make sure that you do not set the same Ubuntu root user password as MySQL root password.

MySQL Server ROOT Password setup

Next, you will be asked for MySQL Authentication configuration. Read through the complete setup and use arrow keys for navigation. Select <Ok> on first window and proceed to next.

In the second configuration window, you’ll be asked for the default authentication plugin method.

Select the second option i.e. Use Legacy Authentication Method (Retain MySQL 5.x Compatibility) and hit [Enter] button.

Use Legacy Authentication Method (Retain MySQL 5.x Compatibility)

Moving on to the security setup, the MySQL installation is insecure by default, however, thankfully a small command line will help to improve the overall security.

Now, let’s secure the MySQL installation by running the following security script command:

sudo mysql_secure_installation
  • First, you’ll be asked to enter the MySQL root password which we created while installation — enter the root password and proceed
  • Next, it will ask whether to use VALIDATE PASSWORD PLUGIN — assuming that you’ve already set a strong MySQL root password, enter no and proceed
  • Next, will be asking whether to Change the password for MySQL root user —  enter no since we already have a strong password for the root user
  • Next, will ask whether to Remove anonymous users? – enter yes
  • Next, Disallow root login remotely? — enter yes
  • Next, Remove test database and access to it? — enter yes
  • Next, Reload privilege tables now? — enter yes
  • Finally, it will show All done! message

MySQL Server Secure Installation Script Configuration

The MySQL 8.0 server installation is now secured. Check the status of MySQL service, and enable if it’s not running by executing below commands in SSH.

sudo systemctl status mysql
sudo systemctl enable mysql

In order to login your MySQL server again, just type below command and enter your MySQL root password.

sudo mysql -u root -p

To quit from MySQL, type below command and hit [Enter] button:

exit;

We’ve now successfully installed the MySQL 8.0 on Ubuntu Cloud Server via SSH command line. Let’s proceed to the PHP installation process.

How to install PHP for Processing?

We’ve already installed and set up Nginx Web Server and MySQL Database Server, however, we need a processor that connects these two independent servers to generate the dynamic content. That’s where the PHP comes into play.

Unlike other Web Server, Nginx does not have a native processor and it requires very heavy and fast processor. Hence, we will be installing the php-fpm that stands for PHP-FastCGI Process Manager. This will help in passing the PHP request for processing.

We will be installing the latest available PHP 7.3 version.

It is said that every new version the PHP module will be 10% faster than the preceding version. i.e. PHP 7.3 is 10% faster than PHP 7.2 and so on.

So let’s get into adding the PHP to the aptrepository, and then install the latest PHP in Ubuntu.

Update and upgrade the repository, and required packages using the following command:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install software-properties-common python-software-properties

Now, add the Ondřej Surý’s PPA repository using below command:

sudo add-apt-repository ppa:ondrej/php

Adding Ondřej Surý’s PPA repository PHP latest version

Run the update command again to refresh the repository.

sudo apt-get update

Now finally, let’s install the PHP7.3 server and necessary module packages running below command:

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

Installing PHP and supporting modules

Once completed, run the below command to verify the PHP7.3 installation:

php -v

Lastly, secure the PHP installation by modifying the configuration file.

Open php.ini file in nano editor using the command:

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

Look for the configuration line cgi.fix_pathinfo which will be commented with ; and the default set to value 1.

;cgi.fix_pathinfo=1

Uncomment the line and change value to 0, the final configuration setup should look like below:

cgi.fix_pathinfo=0

cgi.fix_pathinfo value modify in php.ini configuration file

Close the nano editor in SSH by hitting [Ctrl+c] on keyboard and save the changes.

Restart the PHp7.3-FPM using below command line.

sudo service php7.3-fpm restart

The PHP processor is now successfully installed and secured on Ubuntu Cloud Server.

Configure Nginx to use the PHP Processor

We have installed all the necessary server components. The only pending setup is telling Nginx to use PHP7.3-FPM Processor for generating dynamic content.

We need to edit the server{} blocks in Nginx Configuration files (just like Apache virtual host file).

Open the Nginx default configuration file in nano editor.

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

OR you can also use the SFTP client FileZilla to edit the config file easily.

Navigate to the Nginx folder path below, and open the default configuration file in your favorite editor like VS Code.

/etc/nginx/sites-available/

The file will look like below after excluding the # commented lines.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name _;
    location / {
        try_files $uri $uri/ =404;
    }
}

We need to make changes to this configuration file to use PHP Processor.

  1. Add index.php as the first value in index directive
  2. Modify the server_name to listen to our IP address; in my case it’s 159.203.71.45
  3. Uncomment the location block which is defined to use the fastcgi-php.conf snippet and also socket associated with php7.0-fpm and modify the value to php7.3-fpm
  4. Finally, uncomment the location block using [/code].ht[/code] for blocking access to .htaccess file in website root folder

Hence, the final output of the configuration file excluding the comment lines should look like:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name server_domain_or_IP;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    }

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

Save and close the nano editor in SSH OR the local editor in SFTP and upload the file back to server.

Upload the edited file back to the Ubuntu server using FileZilla

Now test the Nginx configuration using the below command:

sudo nginx -t

If there are any errors, troubleshoot and close before restarting the Nginx Web server.

When you’re ready to restart and commit the Nginx changes, run command:

sudo server nginx restart

Test and reload the Nginx Server Configuration Changes

That’s it! Your LEMP stack is ready with prominent security and highest possible configuration.

Watch the Video Tutorial of LEMP Stack

This article is purely technical and even minor mistake can end up misconfiguring the LEMP Stack. Hence, watch this video for the complete step by step guide to install powerful LEMP Stack on Ubuntu Cloud Server in DigitalOcean.

Hope you liked the video, please subscribe for more videos and stay updated.

What’s next in #CloudServer Series?

As we have already completed with setting up a strong and powerful LEMP Stack, we will be next installing the WordPress CMS on LEMP Stack.

WordPress is a great blogging platform for beginners to advanced level users. restoreBin Blog is also powered by WordPress Blogging CMS.

Stay tuned, and keep watching this space to learn more.

If you've any thoughts on Install and Configure Powerful LEMP Stack on Ubuntu Cloud Server, then feel free to drop in below comment box. Also, please subscribe to our restoreBin YouTube channel for amazing videos tips. Cheers!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *