How to install LEMP in Ubuntu

To install LEMP in Ubuntu

LEMP is a group of software, which is used to serve dynamic web pages and web applications. The acronym of LEMP describes a Linux operating system, Nginx as web server, MySQL for backend data storage and the dynamic processing is handled by PHP. Installation of LEMP in Ubuntu is explained in this article.


To install the Nginx

Run the below command to install the Nginx.

root@linuxhelp1:~# apt-get install nginx -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  nginx-common nginx-core
Suggested packages:
  fcgiwrap nginx-doc
The following NEW packages will be installed:
  nginx nginx-common nginx-core
0 upgraded, 3 newly installed, 0 to remove and 293 not upgraded.
Need to get 458 kB of archives.
After this operation, 1,482 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx-common all 1.10.0-0ubuntu0.16.04.3 [26.9 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx-core amd64 1.10.0-0ubuntu0.16.04.3 [428 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx all 1.10.0-0ubuntu0.16.04.3 [3,496 B]
Fetched 458 kB in 2s (227 kB/s)
.
.
.
Setting up nginx-common (1.10.0-0ubuntu0.16.04.3) ...
Setting up nginx-core (1.10.0-0ubuntu0.16.04.3) ...
Setting up nginx (1.10.0-0ubuntu0.16.04.3) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...

Then start and check the status of the Nginx.

root@linuxhelp1:~# systemctl start nginx
root@linuxhelp1:~# systemctl status nginx
 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) since Wed 2016-10-26 06:00:52 IST  54s ago
 Main PID: 47617 (nginx)
   CGroup: /system.slice/nginx.service
           ??47617 nginx: master process /usr/sbin/nginx -g daemon on  master_process on
           ??47618 nginx: worker process                           

Oct 26 06:00:52 linuxhelp1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 26 06:00:52 linuxhelp1 systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
Oct 26 06:00:52 linuxhelp1 systemd[1]: Started A high performance web server and a reverse proxy server.
Oct 26 06:01:28 linuxhelp1 systemd[1]: Started A high performance web server and a reverse proxy server.

Now open the web browser and navigate to http://ip-address/ or http://localhost/ as shown below,


To configure Nginx

Open the default vhost file /etc/nginx/sites-available/default to configure the Ngnix.

root@linuxhelp1:~# vim /etc/nginx/sites-available/default

In the default vhost file, add the index.php as given below.

root /var/www/html 

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html 

        server_name _ 

At the same time, uncomment and modify the following lines as shown below.

location ~ .php$ {
                try_files $uri =404 
        #       include snippets/fastcgi-php.conf 
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000 
        #       # With php7.0-fpm:
                fastcgi_pass 127.0.0.1:9000 
                fastcgi_index index.php 
                 include fastcgi.conf 
        }

Save and exit the file to accomplish the above process.

Then run the below command to check the Nginx configuration file.

root@linuxhelp1:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now its time to restart the Nginx service.

root@linuxhelp1:~# systemctl restart nginx


To install Mariadb

Utilize the following command to install the Mariadb.

root@linuxhelp1:~# apt-get install mariadb-server -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient20 libreadline5 libterm-readkey-perl
  mariadb-client-10.0 mariadb-client-core-10.0 mariadb-common mariadb-server-10.0 mariadb-server-core-10.0 mysql-common
Suggested packages:
  libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl mailx mariadb-test tinyca
The following NEW packages will be installed:
  libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient20 libreadline5 libterm-readkey-perl
  mariadb-client-10.0 mariadb-client-core-10.0 mariadb-common mariadb-server mariadb-server-10.0 mariadb-server-core-10.0
  mysql-common
0 upgraded, 14 newly installed, 0 to remove and 293 not upgraded.
Need to get 16.5 MB of archives.
After this operation, 142 MB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-common all 5.7.16-0ubuntu0.16.04.1 [15.0 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 mariadb-common all 10.0.27-0ubuntu0.16.04.1 [16.2 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libdbi-perl amd64 1.634-1build1 [743 kB]
.
.
.
Setting up libhtml-template-perl (2.95-2) ...
Setting up mariadb-server (10.0.27-0ubuntu0.16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...


To install MySQL Secure

Install the MySQL secure and set the root password by using the below command.

root@linuxhelp1:~# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we' ll need the current
password for the root user.  If you' ve just installed MariaDB, and
you haven' t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from ' localhost' .  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

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

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you' ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Check the status of the Mariadb service.

root@linuxhelp1:~# service mysql status
 mysql.service - LSB: Start and stop the mysql database server daemon
   Loaded: loaded (/etc/init.d/mysql  bad  vendor preset: enabled)
   Active: active (running) since Wed 2016-10-26 06:10:55 IST  3min 1s ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/mysql.service
           ??52333 /bin/bash /usr/bin/mysqld_safe
           ??52334 logger -p daemon err -t /etc/init.d/mysql -i
           ??52478 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --skip-lo
           ??52479 logger -t mysqld -p daemon error

Oct 26 06:10:56 linuxhelp1 /etc/mysql/debian-start[52530]: mysql
Oct 26 06:10:56 linuxhelp1 /etc/mysql/debian-start[52530]: mysql.column_stats                                 OK
Oct 26 06:10:56 linuxhelp1 /etc/mysql/debian-start[52530]: mysql.columns_priv                                 OK
Oct 26 06:10:56 linuxhelp1 /etc/mysql/debian-start[52530]: mysql.db                                           OK
Oct 26 06:10:56 linuxhelp1 /etc/mysql/debian-start[52530]: mysql.event                                        OK
Oct 26 06:10:56 linuxhelp1 /etc/mysql/debian-start[52530]: mysql.func                                         OK
Oct 26 06:10:56 linuxhelp1 /etc/mysql/debian-start[52530]: mysql.gtid_slave_pos                               OK
Oct 26 06:10:56 linuxhelp1 /etc/mysql/debian-start[52530]: mysql.help_category                                OK


To install PHP

Run the following command to install the PHP for web development.

root@linuxhelp1:~# apt-get install php7.0 php7.0-fpm php7.0-mysql -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  php-common php7.0-cli php7.0-common php7.0-json php7.0-opcache php7.0-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  php-common php7.0 php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-mysql php7.0-opcache php7.0-readline
0 upgraded, 9 newly installed, 0 to remove and 293 not upgraded.
Need to get 3,630 kB of archives.
After this operation, 14.5 MB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 php-common all 1:35ubuntu6 [10.8 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-common amd64 7.0.8-0ubuntu0.16.04.3 [822 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-json amd64 7.0.8-0ubuntu0.16.04.3 [16.8 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-opcache amd64 7.0.8-0ubuntu0.16.04.3 [75.9 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-readline amd64 7.0.8-0ubuntu0.16.04.3 [12.8 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-cli amd64 7.0.8-0ubuntu0.16.04.3 [1,279 kB]
.
.
.
Creating config file /etc/php/7.0/cli/php.ini with new version
Setting up php7.0-fpm (7.0.8-0ubuntu0.16.04.3) ...

Creating config file /etc/php/7.0/fpm/php.ini with new version
Setting up php7.0 (7.0.8-0ubuntu0.16.04.3) ...
Setting up php7.0-mysql (7.0.8-0ubuntu0.16.04.3) ...

Creating config file /etc/php/7.0/mods-available/mysqlnd.ini with new version

Creating config file /etc/php/7.0/mods-available/mysqli.ini with new version

Creating config file /etc/php/7.0/mods-available/pdo_mysql.ini with new version
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for php7.0-fpm (7.0.8-0ubuntu0.16.04.3) ...

Then open the php.ini file to uncomment and change the value for the following line as shown below.

root@linuxhelp1:~# vim /etc/php/7.0/fpm/php.ini

cgi.fix_pathinfo=0

Once the installation gets completed, restart the php-fpm service and test the satus of the service.

root@linuxhelp1:~# systemctl restart php7.0-fpm
root@linuxhelp1:~# systemctl status php7.0-fpm
 php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.0-fpm.service  enabled  vendor preset: enabled)
   Active: active (running) since Wed 2016-10-26 06:17:20 IST  25s ago
  Process: 60236 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS)
 Main PID: 60245 (php-fpm7.0)
   Status: " Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" 
   CGroup: /system.slice/php7.0-fpm.service
           ??60245 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)                      
           ??60248 php-fpm: pool www                                                            
           ??60249 php-fpm: pool www                                                            

Oct 26 06:17:20 linuxhelp1 systemd[1]: Starting The PHP 7.0 FastCGI Process Manager...
Oct 26 06:17:20 linuxhelp1 systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

Create a sample “ testphp.php” file in nginx document root folder.

root@linuxhelp1:~# vim /var/www/html/test.php

After adding the following lines, save and exit the file.

< ?php
 phpinfo() 
?> 

Open the browser and navigate to http://server-ip-address/testphp.php.

PHP-FPM listens on the socket /var/run/php5-fpm.sock by default. Here you need to make changes in PHP-FPM as shown below,

root@linuxhelp1:~# vim /etc/php/7.0/fpm/pool.d/www.conf

Find the line ' listen = /var/run/php5-fpm.sock' and modify it to ' listen = 127.0.0.1:9000' .

Once the above process gets completed, restart the php7.0-fpm service.

root@linuxhelp1:~# systemctl restart php7.0-fpm

Now open the Nginx configuration file by using the below command.

root@linuxhelp1:~# vim /etc/nginx/sites-available/default

Find the line ' fastcgi_pass unix:/var/run/php5-fpm.sock ' and modify it as shown below.

location ~ .php$ {
                try_files $uri =404 
        #       include snippets/fastcgi-php.conf 
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000 
        #       # With php7.0-fpm:
                fastcgi_pass 127.0.0.1:9000 
                fastcgi_index index.php 
                 include fastcgi.conf 
        }

Finally restart the nginx service.

root@linuxhelp1:~# systemctl restart nginx

FAQ
Q
What is PHP FPM used for?
A
PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.
Q
What does reverse proxy mean?
A
In computer networks, a reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client, appearing as if they originated from the proxy server itself.
Q
Where is Nginx config?
A
The way nginx and its modules work is determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf , /etc/nginx , or /usr/local/etc/nginx .
Q
What Nginx can do?
A
It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.
Q
Why LEMP instead of LNMP?
A
Think of how in English, the article an is used instead of a for hour even though it begins with a consonant. The importance is the sound of the first letter rather than its written represent