How to Install LEMP Stack on Ubuntu 18.04

Install LEMP Stack on Ubuntu 18.04

In this article, we will have explained the necessary steps to install and configure the LEMP Stack on Ubuntu 18.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of Linux, Apache, MySQL, and PHP. Due to its modular nature, the components can easily be swapped out. With LEMP, Apache is replaced with the lightweight yet powerful Nginx.

Install LEMP Stack on Ubuntu

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

Step 2. Install Nginx.

Install NGINX from the package repository:

Once installed, start Nginx and enable it to start automatically after a reboot with these two commands:

You can verify that the Nginx web server is running by opening a web browser and visiting your server IP address (http://server_ip_address). You should get the Nginx welcome page.

Step 3. Install MariaDB.

Now to install mysql-server package run below command:

After completing installation MariaDB will start automatically. Check MariaDB version by typing:

Then, issue the mysql_secure_installation command to improve the security of the MariaDB installation:

Install LEMP Stack on Ubuntu 18.04

Step 4. Install PHP.

Ubuntu 18.04 LTS ships with the latest and greatest PHP version 7.2. To install PHP and several other most common PHP modules type:

Once the PHP packages are installed, restart the Nginx service with:

Step 5. Configure Nginx for PHP.

The next step you that need to complete is to modify the Nginx configuration file:

Paste the following content:

To enable the server configuration that we just created, run the following command:

Restart PHP and reload the NGINX configuration:

Step 6. Configure Firewall.

It is recommended that you enable the ufw firewall and add a rule for Nginx:

Step 7. Testing PHP

Now you should create info.php file to test php to do so type following:

Then open the following link in your web browser and we will be able to see all the information about PHP and its other configurations: http://your-domain/phpinfo.php (replacing your IP address with the one above).

That’s all you need to do to install LEMP Stack on Ubuntu 18.04. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.