How to Install LEMP on Ubuntu 19.04 and 18.04

The LEMP stack is a web development platform for hosting static and dynamic websites. It is a combination of free and open-source software including Linux, Nginx, MySQL, and PHP. It is an alternative to the highly popular LAMP stack, the only difference being having Nginx as a web server instead of Apache.

Install Nginx

To install Nginx on your Ubuntu 18.04 server, you need to execute the following command:

$ apt-get install nginx

After the installation is completed, start Nginx and enable it to start automatically after a reboot with these two commands:

$ systemctl start nginx
$ systemctl enable nginx

To check and verify whether Nginx is currently running on your server, execute the following command:

Also, you can verify that Nginx is running by opening a web browser and visiting your server IP address (http://server_ip). You should get the Nginx welcome page – it should look similar to the one below:

Install MySQL

Next, we need to install the MySQL server. The following command will install the latest MySQL 5.7 server from the official Ubuntu repositories:

$ apt install mysql-server

When the installation is complete, run the following commands to start and enable the MySQL service :

$ sudo mysql -u root # I had to use "sudo" since is new installation
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ service mysql restart

Install PHP

The last step of our LEMP stack setup is the PHP installation. Currently, the default PHP version available from the official Ubuntu repository is PHP 7.2.

Unlike Apache, Nginx does not contain native PHP processing. For that, we have to install PHP-FPM (FastCGI Process Manager). FPM is an alternative PHP FastCGI implementation with some additional features useful for sites with high load.

$ apt-get install php-fpm php-mysql

To verify if PHP has been properly installed, you can run the following command:

Test PHP

To see if PHP is working correctly on Ubuntu 18.04, let’s a create a new PHP file called info.php in the document root directory. By default, this is located in /var/www/html/.

$ nano /var/www/html/info.php

Paste the following into the file:

<?php
phpinfo();
?>

Restart Nginx for the changes to take effect:

$ systemctl restart nginx

Now, open your preferred web browser and navigate to http://your_server_ip_address/info.php . You will be welcomed by a web page similar to the one below:

How to Install LEMP on Ubuntu 18.04

That’s it – you have successfully installed the LEMP stack on your Ubuntu 18.04 VPS.

Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install the LEMP stack for you. They are available 24×7 and will take care of your request immediately.