Introduction
The LEMP Stack is a powerful suite of applications used to develop and deploy dynamic web applications and pages. Traditionally, the LEMP stack consists of Nginx, MySQL, and PHP. However, due to the modular architecture of this stack, these components can be swapped out and replaced with others of equal functionality.
This tutorial will help you install the LEMP (Nginx, MariaDB, and PHP) on your Ubuntu 18.04 server.
If you have VPS hosting or dedicated server web hosting with Ubuntu 18.04 installed, let’s get started!
Step 1 – Installing Nginx
The first step when installing the LEMP Stack is to install the Nginx web server:
$ sudo apt-get update $ sudo apt install nginx
Once the installation is complete, run the command below to start Nginx:
$ sudo systemctl start nginx
Then, confirm the status of the just installed web server:
$ sudo systemctl status nginx
This will give you the output 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 Mon 2018-10-29 07:58:38 EDT; 57s ago Docs: man:nginx(8) Process: 1836 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 1823 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 1839 (nginx) Tasks: 2 (limit: 2322) CGroup: /system.slice/nginx.service ├─1839 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─1841 nginx: worker process
Adjusting The Firewall
If the UFW firewall is activated on your Ubuntu server, you should adjust it to permit the Nginx web server. You should enable the most prohibitive profile to enable the traffic that you want via UFW.
First, run the command below to see the apps that have a profile deployed on your server:
$ sudo ufw app list
This will give you the following output:
Available applications Apache Apache Full Apache Secure Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
From the output above, its clear OpenSSH and Nginx have deployed UFW profiles on your Ubuntu systems.to enable both HTTPs and HTTP traffic, execute the command below:
$ sudo ufw allow in"Nginx Full"
The verify the UFW status:
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Nginx HTTP ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6)
That is it! You can now run the Nginx server test page. Go to your favorite browser and search http://IP-Address/ or http://localhost/
If everything is OK you will get the following page:
Step 2 – Installing MariaDB
Once you install the web server, the next step is to install the database server. As aforementioned, we’ll install the MariaDB server which is the best replacement of the MySQL database.
To install the MariaDB database server from the official Ubuntu repository, execute the command below:
$ sudo apt install mariadb-server mariadb-client
However, the MariaDB found in these repositories might not be the most recent version. For this reason, to install the latest MariaDB database server, you must install it from the official MariaDB repository.
To do so, first run the command below to add the repository, then import the installation keys:
$ sudo apt-get install software-properties-common $ sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 $ sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://sgp1.mirrors.digitalocean.com/mariadb/repo/10.3/ubuntu bionic main'
Next, run the command below to update the package index to accept the repository, then install the database server; MariaDB:
$ sudo apt update $ sudo apt install mariadb-server
You can now execute the command below to confirm the version of MariaDB you have installed:
$ sudo systemctl status mysql
This will give you an output similar to the one below:
● mariadb.service - MariaDB 10.3.10 database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2018-10-2405:39:11 EDT; 2min 57s ago Docs:man:mysql (8) https://mariadb.com/kb/en/library/systemd/ Main PID:3428 (mysqld) Status:"Taking your SQL requests now..." Tasks:32 (limit:1152) CGroup:/system.slice/mariadb.service └─994/usr/sbin/mysqld
Securing MariaDB
To secure MariaDB run the command below:
$ mysql_secure_installation
You will be prompted to enter the current password for root. Since you have just installed MariaDB, Press ENTER to proceed. This will take you through a number of prompts with questions including:
Set root password? [Y/n] type Y and press ENTER. Enter and reenter your prefered password and press enter to implement it. 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
Once all the questions are answered, your MariaDB installation will be secure.
Step 3 – Installing PHP
Execute the command below to deploy PHP:
$ sudo apt-get install php-fpm php-mysql
Once PHP is installed, the next step is securing it. This can be done by editing the php.ini file. First, run the command below to open this file:
$ sudo nano /etc/php/7.2/fpm/php.ini
Find this line ;cgi.fix_pathinfo=1 and replace 1 with 0:
cgi.fix_pathinfo=0
Save the changes and quit the nano editor, then run the command below to restart PHP-FPM:
$ sudo systemctl restart php7.2-fpm
Next, issue the command below to check the status of PHP-FPM service:
$ sudo systemctl status php7.2-fpm
This will give you an output similar to the one below:
● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2018-10-2908:12:46 EDT; 7s ago Docs:man:php-fpm7.2(8) Main PID:29535 (php-fpm7.2) Status:"Process active: 0, Idle: 2, Request: 0, Slow: 0, Traffic: 0req/se " Tasks:3 (limit:1152) CGroup:/system.slice/php7.2-fpm.service ............
Configuring Nginx To Utilize PHP-FPM
First, run the command below to open Nginx Virtual host file:
$ sudo nano /etc/nginx/sites-available/default
Locate the server part and add the IP address or FQDN of your Ubuntu 18.04 server: Besides, check to see if the index.php line is added:
[...] server { listen 80 default_server; listen [::]:80 default_server; [...] 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 192.168.225.22; [...]
Next, locate #location ~ .php$ section and adjust the lines as highlighted below:
location ~ .php$ { include snippets/fastcgi-php.conf; # ## With php-fpm (or other unix sockets): fastcgi_pass unix:/run/php/php7.2-fpm.sock; ## With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /.ht { deny all; } }
Save the changes and quit the nano editor.
Now, run the command below to check the file hosted in the directory /run/php:
$ ls /run/php/
This will give you the output below:
php7.2-fpm.pidphp7.2-fpm.sock
The file stored in that directory is php7.2-fpm.pid php7.2-fpm.sock and not php7.0-fpm.sock as listed listed in the location ~ .php$ section. Always ensure the ideal name is mentioned in the location ~ .php$ directive.
Next, run the command below to scrutinize Nginx’s configuration file for possible syntax errors:
$ sudo nginx -t
If everything is okay, you should get the output below:
nginx: the configuration file /etc/nginx/nginx.confsyntaxis ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Now restart the Nginx service:
$ sudo systemctl restart nginx
To test your PHP configuration through the web browser, first, run the command below to create an info.php file in the document root folder.
$ sudo nano /var/www/html/info.php
Next, add the content below into this file:
<?php phpinfo(); ?>
Save the changes and quit the nano editor, the restart Nginx to implement these changes.
Now go to your favorite browser and search http://IP-address/info.php. If PHP is running correctly, you should get the result below:
Installing PHP Modules
Now that you have a fully functioning PHP service, install more PHP modules to extend its functionality. If you are not sure which PHP module to install, run the command below to view the available modules:
$ sudo apt-cachesearch php- | less
This will give you an output of all the PHP modules that can be installed on your Ubuntu 18.04 server. Scroll and up and down to pinpoint the module you want to install. The command used to install the PHP modules takes the following format:
$ sudo apt-get install "module name"
For instance to install php-gd, run the command:
$ sudo apt-get install php-gd
On the other hand, to install all the available PHP modules run the command:
$ sudo apt-get install php*
If you want to get more details regarding a PHP module run the command:
$ sudo apt-cache show "module name"
Once you install the additional PHP modules, run the command below to restart the Nginx server:
$ sudo systemctl restart nginx
Conclusion
That is it! You have installed the LEMP Stack on your Ubuntu 18.04 server. You can now implement your web applications and web pages using the newly installed LEMP web stack.
Check out these top 3 Linux hosting services
0