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:

$ systemctl status nginx

Output:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running)
     Docs: man:nginx(8)
  Process: 2133 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 2121 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 2135 (nginx)
    Tasks: 3 (limit: 2292)
   CGroup: /system.slice/nginx.service
           ├─2135 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─2139 nginx: worker process
           └─2140 nginx: worker process

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:

How to Install LEMP on Ubuntu 18.04

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 :

$ systemctl start mysql
$ systemctl enable mysql

We can check to see if the MySQL service is running:

$ service mysql status

If running, you will see a green Active status, like this:

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running)
 Main PID: 2538 (mysqld)
    Tasks: 27 (limit: 2292)
   CGroup: /system.slice/mysql.service
           └─2538 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

Once the installation is complete, issue the following command to secure your installation:

$ mysql_secure_installation

Then, answer every prompt with Yes to improve the security of your MySQL server.

if you find below error

(ERROR 1698 (28000): Access denied for user ‘root’@’localhost’)

Here is the solution for it :

$ 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:

$ php -v

Output:

PHP 7.2.15-0ubuntu0.18.04.1 (cli) (built: Feb  8 2019 14:54:22) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.15-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

Configure Nginx for PHP

The next step you that need to complete is to modify the Nginx configuration file. The command below does exactly that:

$ cd /etc/nginx/sites-available/
$ nano /etc/nginx/sites-available/your_domain.com.conf

Now that you have a new default file opened, paste the following content:

server {
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name your_domain.com;

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

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

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

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

$ ln -s /etc/nginx/sites-available/your_domain.com.conf /etc/nginx/sites-enabled/your_domain.com.conf

Now, check the config file to make sure that there are no syntax errors. Any errors could crash the web server on restart.

$ nginx -t

Output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If there are no errors, you can reload the Nginx config.

$ service nginx reload

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

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.

Happy Coding!

LEAVE A REPLY

Please enter your comment!
Please enter your name here