In this tutorial, we will show you how to install LEMP on Ubuntu 18.04 LTS. For those of you who didn’t know, The LEMP stack is a powerful combination of open-source software components designed to build and host dynamic websites and web applications. It consists of Linux as the operating system, Nginx (pronounced “engine-x”) as the web server, MariaDB as the database management system, and PHP as the scripting language for processing dynamic content.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation LEMP stack (Linux, Nginx, MariaDB, and PHP) on an Ubuntu 18.04 Bionic Beaver server.
Prerequisites
- A server running one of the following operating systems: Ubuntu 18.04, and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install LEMP on Ubuntu 18.04 LTS Bionic Beaver
Step 1. Make sure that all your system packages are up-to-date by running the following apt-get
commands in the terminal.
sudo apt-get update sudo apt-get upgrade
Step 2. Installing Nginx on Ubuntu 18.04 LTS.
The first step in setting up the LEMP stack is to install Nginx, the web server component. Follow these steps:
sudo apt install nginx
Once installed, start the Nginx service using the following command:
sudo systemctl start nginx
Now if you have your UFW firewall running, you will need to allow connections to Nginx:
sudo ufw allow 'Nginx HTTP'
You can verify that Nginx is really running by opening your favorite web browser and entering the URL http://your-domain.com
if it is installed, then you will see this:
To get Nginx to work with PHP correctly, we need to make changes to the Nginx configuration file. In this guide, we will be using a simple Nginx config file:
sudo nano /etc/nginx/sites-available/default
Copy the following into your text editor:
server { listen 80; server_name your_domain_name.com; root /usr/share/nginx/html; index index.php index.html; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php7.2-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Once you have finished editing the file restart Nginx with:
sudo nginx -t sudo systemctl restart nginx
Step 4. Installing MariaDB on Ubuntu 18.04 LTS.
Next, we’ll install MariaDB, the database management system component of the LEMP stack:
sudo apt install mariadb-server
Once complete, you can verify MariaDB is installed by running the below command:
sudo systemctl status mariadb
By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation
script. you should read below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB:
mysql_secure_installation
Configure it like this:
- Set root password? [Y/n] y - 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
To log into MariaDB, use the following command (note that it’s the same command you would use to log into a MariaDB database):
mysql -u root -p
Step 5. Installing PHP and Configure PHP-FPM Settings.
Unlike Apache, Nginx does not contain native PHP processing. For that, we have to install PHP-FPM (FastCGI Process Manager):
sudo apt install php-fpm php-mysql
Once installed, check the PHP version:
php --version
Now open the PHP-FPM default file to edit the following content:
### nano /etc/php/7.2/fpm/php.ini cgi.fix_pathinfo=0 date.timezone = Africa/Douala
Save the file and restart php-fpm:
systemctl restart php7.2-fpm
To test PHP, create a test file named info.php with the content below. Save the file, then browse to it to see if PHP is working:
nano /usr/share/nginx/html/info.php
Copy the following into your text editor:
<?php phpinfo(); ?>
Try to access it at http://your_server_ip/info.php
. If the PHP info page is rendered in your browser then everything looks good and you are ready to proceed further.
Congratulations! You have successfully installed the LEMP stack. Thanks for using this tutorial for installing LAMP (Linux, Nginx, MySQL, and PHP) in the Ubuntu 18.04 LTS system. For additional help or useful information, we recommend you check the official Nginx, MySQL, and PHP websites.