The LEMP (Linux, NGINX-pronounced “Engine-X”, MySQL/MariaDB & PHP) is a group software package commonly used for web page or web application, NGINX is a web server, MySQL is a Database for storing data in the backend. PHP (Hypertext Preprocessor) is an Open Source server-side scripting language used for Web development.
Following below steps tutorial for basic installation and setup guides
Step 1: Update Ubuntu Package
To get the latest version of Ubuntu package use theapt
by typing the command below:
sudo apt update&&upgrade
Step 2: Install NGINX Web Server
Now we are going to install NGINX stable version by default repository Ubuntu, to do this type the command below:
sudo apt install nginx
Once NGINX already installed, to check and verify Nginx service status type the command below
sudo service nginx status
If it is running and working properly you will see the output NGINX active (running) like below
● 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 2018-05-09 20:42:29 UTC; 2min 39s ago
Docs: man:nginx(8)
Process: 27688 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 27681 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 27693 (nginx)
Tasks: 2 (limit: 1153)
CGroup: /system.slice/nginx.service
├─27693 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─27695 nginx: worker process
To exit Nginx service status just press q
on the keyboard
Step 3: Configuration the Firewall with UFW
UFW (Uncomplicated Firewall) is an Iptable interface to easily configure a firewall on your system. If it does not yet enable, it’s recommended to enable and setup the rule for Nginx
For the first, we have to add rule for SSH, let’s se the command line below
sudo ufw allow OpenSSH
Add the rule receive HTTP to Nginx
sudo ufw allow 'Nginx HTTP'
Then you will see on terminal
Rule added
Rule added (v6)'
Now enable ufw
for Firewall.
sudo ufw enable
If prompted just pres Y
to accept and continue and to check UFW status type the command below
sudo ufw status
You will see the output UFW active (running) like below
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
Step 4: Testing NGINX on the Browser
NGINX web server now is ready on Ubuntu, now you may go to your web browser and visit your domain or IP. If you have not configured domain name yet and don’t know your IP, use the following command find out
sudo ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
If everything is OK you will be presented on your browser with the default Nginx landing page as below
You may edit above default landing page. The file was located in the document root directory /var/www/html
. If you want to edit you could use nano editor and use the following command
sudo nano /var/www/html/index.nginx-debian.html
To save and close nano, press Ctrl/Cmd
+ X
and then press Y
and ENTER
to save changes and Reload Nginx service by using the following command
sudo service nginx reload
Go back to your browser and refresh….. see the changed…..!!!!
Step 5: Install MariaDB or MySQL
There two options using MariaDB or MySQL Database, please choose one that you prefer with. To install MariaDB or MySQL on Ubuntu server let’s following steps:
Installing MariaDB
MariaDB is a fork of MySQL, the database structure and indexes of MariaDB are the same as MySQL. This allows you to switch from MySQL to MariaDB without having to alter your applications since the data and data structures will not need to change.
To install MariaDB packages from the MariaDB repository use the following command
sudo apt-get install mariadb-server mariadb-client
Press Y
and ENTER
when prompted on installing process
MariaDB service will start automatically, the commands below can be used to stop, start and enable MariaDB service.
sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service
To check and verify MariaDB is working, use the command below
sudo systemctl status mariadb
The output MariaDB active (running) like below
● mariadb.service - MariaDB 10.3.8 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) since Sun 2018-07-29 19:36:30 UTC; 56s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 16417 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 31 (limit: 507)
CGroup: /system.slice/mariadb.service
└─16417 /usr/sbin/mysqld
Press q
on the keyboard to exit service status
To check MariaDB server version use the command below
mysql -V
If you already installed MariaDB server above, you should ignore below Installation MySQL and jump to step 6
Installing MySQL
MySQL is an open-source relational database management system (RDBMS). Just like all other relational databases, MySQL uses tables, constraints, triggers, roles, stored procedures and views as the core components
To install MySQL packages from the MySQL repository use the following command
sudo apt update && sudo apt install mysql-server
Press Y
and ENTER
when prompted on installing process
MySQL service will start automatically, the commands below can be used to stop, and start MySQL service.
sudo service mysql stop
sudo service mysql start
To check and verify MySQL is working, use the command below
sudo service mysql status
The output MySQL active (running) like below
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since since Wed 2018-05-09 21:10:24 UTC; 16s ago
Main PID: 30545 (mysqld)
Tasks: 27 (limit: 1153)
CGroup: /system.slice/mysql.service
└─30545 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
Press q
on the keyboard to exit service status
To check MySQL server version use the command below
mysql -V
Step 6: Configuration MySQL Security
To improve MySQL security it’s recommended to enable mysql_secure_installation
. These packed to create a root password and disallow remotely root access. To install packages use the following ways
sudo mysql_secure_installation
On installation process will prompt and to be asked the validate password
plugin, it can be used for password strength checking
- Enter current password for root (enter for none):
Just press the Enter
- Set root password?
[Y/n]: Y
- New password:
Enter password
- Re-enter new password:
Repeat password
- Remove anonymous users?
[Y/n]: Y
- Disallow root login remotely?
[Y/n]: Y
- Remove test database and access to it?
[Y/n]: Y
- Reload privilege tables now?
[Y/n]: Y
If you didn’t create a root password, you have to generate a strong password
For testing MySQL server and run the following command.
sudo mysqladmin -p -u root version
Enter the MySQL root password you created before and see the following output
mysqladmin Ver 8.42 Distrib 5.7.22, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Server version 5.7.22-0ubuntu18.04.1
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/run/mysqld/mysqld.sock
Uptime: 4 min 28 sec
Threads: 1 Questions: 15 Slow queries: 0 Opens: 113 Flush tables: 1 Open tables: 106 Queries per second avg: 0.055
Configured MySQL server on Ubuntu now is successful
Step 7: Install PHP-FPM
Let’s install PHP with Latest version package on Ubuntu. Below there are two commands will update the package and get the package to install. Once installation there are questions just pressing Y
and ENTER
to continue.
sudo apt update && sudo apt install php-fpm
Let’s install PHP 7.3 Modules package run the command below
sudo apt-get install php7.3-fpm php7.3-cli php7.3-mysql php7.3-gd php7.3-imagick php7.3-recode php7.3-tidy php7.3-xmlrpc
After installing now check PHP version.
php --version
The output PHP version (running) like below
PHP 7.3.0-2+ubuntu18.10.1+deb.sury.org+1 (cli) (built: Dec 17 2018 09:23:19) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.0-dev, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.0-2+ubuntu18.10.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
List the contents for the directory /var/run/php/
ls /var/run/php/
You should see a few entries here.
php7.2-fpm.pid php7.2-fpm.sock
Above we can see the socket is called php7.2-fpm.sock. Remember this as you may need fastcgi_pass
setting on the next Nginx configuration
Step 8: Configuration PHP on NGINX
The LEMP components installed and now you may need to make configuration and some changes on Nginx server block as virtual host. Default file is located on /etc/nginx/sites-available/default
Edit default file using nano editor
sudo nano /etc/nginx/sites-available/default
The output default file like below
server {
listen 80;
listen [::]:80;
root /var/www/html/;
index index.html index.htm index.nginx-debian.html;
server_name YOUR_DOMAIN_OR_IP_HERE;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
Find or you can use Cmd/Ctrl W
to search index.html
the sample above see on contrast red line and Add index.php
after index
index index.php index.html index.htm index.nginx-debian.html;
Change the value for fastcgi_pass
socket inside location ~ \.php$
braket see on contrast red without comment #
.
If you installed PHP version 7.2, the socket should be: /var/run/php/php7.2-fpm.sock
or if you not sure PHP socket on your server use the following command
ls /var/run/php/
Once you’ve made the necessary changes, save and close (Press CTRL
+ X
, then press y
and ENTER
to confirm save)
Now check and verify there are no syntax errors, type the following command.
sudo nginx -t
If nothing error the output will present as the following
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Changes made in the configuration file will not be applied until the command to reload configuration is sent to Nginx or it is restarted. To reload use the following command.
sudo service nginx reload
Step 9: Testing PHP on the Browser
To verify PHP-FPM is working on the server block, let’s a create a new PHP file called test.php
. The default file is served in the directory /var/www/html/
Create a file using nano editor use the following command line
sudo nano /var/www/html/info.php
Copy and paste the following code
<?php
phpinfo();
Press Ctrl/Cmd
+ X
, then press Y
and ENTER
to save and close
You can now view this page in your web browser by visiting your server’s domain name or public IP address followed by /test.php
: http://your_domain_or_IP/info.php
…. Enjoy…!!!
If this tutorial could help you, please rate above Star button rating and share to help others find it! Feel free to leave a comment below.