Follow through this guide to learn how to Setup LEMP Stack (Nginx, MariaDB, PHP 7.2) on Ubuntu 18.04. In our previous article, we covered how to setup LAMP Stack on Ubuntu 18.04. Well, we are once again here to guide you on how to setup LEMP Stack on Ubuntu 18.04.
Setup LEMP Stack on Ubuntu 18.04
Install Nginx Web Server
Nginx, commonly pronounced as Engine-X, is available on Ubuntu repositories by default. Assuming your package index is up-to-date, run the command below to install Nginx.
apt install nginx -y
Start and Enable Nginx to run on system reboot
Note that Nginx is set to run after installation by default on Ubuntu 18.04. You can verify the same by running the command;
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 Mon 2018-11-05 21:35:24 EAT; 1min 45s ago
Docs: man:nginx(8)
Process: 1974 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 1961 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0
Now that Nginx is running, run the command below to enable it start it on system reboot;
systemctl enable nginx
If UFW is running, enable Nginx through it. You can run the command below to list applications profiles so you can enable specific profile for Nginx.
ufw app list | grep -i nginx
Nginx Full
Nginx HTTP
Nginx HTTPS
In this case, you can allow any of the Nginx profiles above depending on your current traffic needs, For example, to allow traffic on port 80 (HTTP), just run the command;
ufw allow "Nginx HTTP"
ufw reload
You can now test your web server by navigating to your web browser and entering your server’s IP address. If you see this page below, then Nginx is running well.
Install MariaDB RDBMS
MariaDB server is an open-source fork of MySQL relational database management system. Run the command below to install MariaDB server.
apt install mariadb-server -y
Just like Nginx, MariaDB is set to run by default after installation. Again, you can run the command, systemctl status mariadb
. You can also set it up to run on system boot by running, systemctl enable mariadb
.
You may want to use MySQL servrer instead of MariaDB. If so, run the command below to install MySQL server.
apt install mysql-server
Once the installation is done, you can run the security script below to perform initial database server security configurations.
mysql_secure_installation
...output snipped...
Enter current password for root (enter for none): ENTER
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: ENTER YOUR PASSWORD
Re-enter new password: CONFIRM YOUR 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] y
... 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] y
... 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] y
- 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] y
... 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!
Note, you can press Enter to accept the default Yes.
Install PHP
PHP, Hypertext Preprocessor, is a server-side scripting language designed for Web development, but also used as a general-purpose programming language. Run the following command to install PHP and PHP modules for Apache and MySQL.
apt install php php-fpm php-mysql -y
In case you want to install more PHP modules, you can just search the module you want from Ubuntu repos and install it as shown below.
apt-cache search php- | more
libnet-libidn-perl - Perl bindings for GNU Libidn
php-all-dev - package depending on all supported PHP development packages
php-cgi - server-side, HTML-embedded scripting language (CGI binary) (default)
php-cli - command-line interpreter for the PHP scripting language (default)
php-common - Common files for PHP packages
php-curl - CURL module for PHP [default]
php-dev - Files for PHP module development (default)
php-gd - GD module for PHP [default]
php-gmp - GMP module for PHP [default]
php-ldap - LDAP module for PHP [default]
--More--
So if you want to install GD module for example, run the command below;
apt install php-gd -y
Note that Ubuntu 18.04 ships with the latest version of PHP, PHP 7.2.
Configuring Nginx to Process PHP Pages
Once the installation is done, you need to configure Nginx to be able to process PHP pages.
To achieve this, edit the Nginx default server block configuration file, /etc/nginx/sites-available/default
;
- locate the web server root directory and add index.php to the list of files to be processed
...
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
..
- locate the location block defining PHP script to pass to FastCGI server and make the changes such that the configuration look like;
# pass PHP scripts to FastCGI server
#
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;
}
Once you are done editing, save the configuration and quit.
Restart Nginx
In order for the changes to take effect restart Nginx. But before you can do that, verify that the configuration has no syntax errors.
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If everything is okay, go ahead and do restart Nginx
systemctl restart nginx
Test that PHP is actually working by creating a test php file for example, index.php under Nginx web root document folder, /var/www/html and put the following content.
vim /var/www/html/index.php
<?php
phpinfo();
?>
After that, save the file and quit. Restart Apache service to effect the changes.
systemctl restart nginx
Now, to test PHP processing, navigate to your browser and enter the following URL in your address bar.
http://your-server-IP/index.php
You should be able to see a page similar to the one shown below.
You can check the version of the PHP installed with the following command;
php -v
PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
Now that everything seems fine, remove the PHP test configuration file to avoid creating an attack surface.
rm -rf /var/www/html/index.php
Congratulations! You have successfully installed and configured a LEMP stack on Ubuntu 18.04 (LTS).