In this tutorial we will be setting up a LEMP stack , which includes Nginx, MySQL 8 or MariDB 10, PHP 7 on Ubuntu 18.04 LTS Bionic Beaver.
LEMP (Linux OS, Nginx (pronounced as “Engine-X”) web engine with MySQL for DB and php for dynamic processing).
This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has
sudo
access and that you precede each of the privileged commands withsudo
Install Nginx
To serve web pages to our visitors, we will be installing Nginx, a modern and efficient web server. For this we will be using apt package management suite to complete the installation.
Now we will update apt and install the packages required .
apt update
apt install nginx
Start the Nginx web server:
systemctl start nginx.service
If the ufw
firewall is running, we will need to allow the port 80 to take traffic. We can enable this port by following command.
ufw allow 80/tcp
ufw reload
Type in your server’s IP address or hostname or http://localhost
and you will see the following default output.
If we get an output anything similar to the above, then we have installed the Nginx successfully.
Install MySQL or MariaDB
There is an option in between installing MySQL or MariaDB, which can be seen as a drop in replacement for MySQL. In MariaDB there all the enterprise features present but without all the corporate structuring.It is managed by the MariaDB Foundation as opposed to being managed by Oracle which manages MySQL. That said, they are both good choices.
Don’t install both, only one needs to be installed.
Install MySQL
Since we are installing a web server, we need to have a database to manage data for our site. For this we are installing MySQL database.
Please follow the below steps to install MySQL DB.
apt install mysql-server mysql-client
Now we need to configure the Database to complete the installation
mysql_secure_installation
We will be asked to enter the password for the MySQL root account, since
we haven’t set this yet, so just hit ENTER . Then we will be asked to set that password. we should type Y to set a root password.
To rest of all the questions, the script asks, you should press Y, followed by the ENTER key at each prompt. During this process we will remove anonymous users and the test database, disable remote root logins, and load these new rules MySQL.
Install MariaDB
NOTE :This is an optional step, since MySQL is already installed. If you want to go with MariaDB instead of MySQL, then this for you.
Since MariaDB is also an option, as mentioned earlier, and this means it can be installed just like MySQL without any further steps. We will be installing MariaDB
apt install mariadb-server mariadb-client
Similar to MySQL server, a server needs to be setup as well.
mysql_secure_installation
Install PHP
Now to generate dynamic content, we need to install PHP. Nginx does not contain native PHP processing like other web servers, so we need to install fpm ( “fastCGI process manager” ). fpm will take all the PHP requests from Nginx web server.
Install the php-fpm
and php-mysql
packages as shown below.
apt install php7.0-fpm php-mysql
Now we have installed PHP and in the coming part, we will make Nginx to use it.
Configure Nginx To Use PHP
Now we need to make Nginx to communicate with PHP for dynamic content.
For this configuration, we will avoid the default directories of Nginx by creating
new configuration file and new root directory to hold our PHP files.We name the new files with our domain or host names as required.
Then create a new configuration file in Nginx’s sites-available
directory:
/etc/nginx/sites-available/codesposts.com.conf
This will open a blank file, here we will paste the basic structure for our configuration as shown below.
- /etc/nginx/sites-available/codesposts.com.conf
-
server { listen 80; listen [::]:80; root /var/www/codesposts.com; index index.php index.html index.htm; server_name codesposts.com; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; } }
This simple configuration listens on port 80 and serves files from the web root we just created. Nginx will respond to requests for the name provided after server_name
, and any files ending in .php
will be processed by the php-fpm
process before Nginx sends the results to the user.
Now save and close the file after this basic configuration.
Now we will link our new config file to Nginx’s sites-enabled
directory:
ln -s /etc/nginx/sites-available/codesposts.com.conf /etc/nginx/sites-enabled/
This above step informs Nginx to use the configuration next time it is reloaded. Now we will test our configuration for any syntax errors by the below command.
nginx -t
If errors are reported,we need to check our config file before continuing.
If no errors were reported, then reload Nginx to make the changes:
systemctl restart nginx
Test PHP Configuration
Now we need test our LEMP stack, to validate that Nginx can correctly hand .php
files by our PHP processor.
To do this we need to create a test PHP file in our document root. We will create a codesposts.php file in the document root.
/var/www/html/codesposts.php
Now paste the below code in the blank file we have opened. Which will show the info of our server.
- /var/www/codesposts.com/test.php
-
<?php phpinfo(); ?>
After pasting the above code to our codesposts.php file, save and close the file.
Now we can visit this web page in a web browser from the below shown URL.
http://<Your_serverIP>/codesposts.php
we should see a web page that has been generated by PHP with information about your server. If we get this page, then we can assume that we have successfully configured our LEMP stack.
Now for security reasons, we can delete this test.php file, so that no others can get the info about our servers.
Thanks for being with us, hope this help you gain knowledge and we are always ready to help you. For furthers queries or suggestions, kindly drop a comment below.