Beginner’s guide for creating LEMP server
In our previous tutorials, we discussed Nginx server & its installation. Since this is a web server, it can replace apache in LAMP stack (read about LAMP stack HERE). So synonym for combination of Linux, Nginx, Mariadb/mysql & php (php-fpm) is known as LEMP.
In this article, we will be creating LEMP server aka LEMP stack & we will be starting with installation of nginx web server,
Creating LEMP Server
Step 1 Installing NGINX
To install nginx web server, we will need to install & enable epel repository first as the nginx is not available in the default CentOS/RHEL repositories. To install EPEL repository, run
|$ rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-10.noarch.rpm
Once the repository has been enabled, install nginx by running
|$ yum install nginx
Now start its services & enable it at the boot time.
|$ systemctl start nginx
$ systemctl enable nginx
To test the nginx, open web browser & enter your system IP address into the address bar. We will then be greeted with a Welcome test page.
|http://IP address of machine
(Recommended read– Installing Nginx server & configuring virtual hosts)
Note: Since both apache & nginx are web servers & uses the same ports, we can only use one of two web servers at a time. So if there is previous installation of apache on the server either remove it or stop its services before installing nginx.
Step2 Installing Mariadb
Starting with RHEL/CentOS 7, Mariadb has replaced Mysql as the default database for the distribution. MariaDB is open-source forked out version of Mysql which has been developed by same team that created Mysql.
To install Mariadb, run
|$ yum install mariadb mariadb-server
Once the installation is done, start the services,
|$ systemctl start mariadb
$ systemctl enable mariadb
Next, we will secure the Mariadb installation . To do so, run
Complete the setup as per your setup needs, an example for the setup is shared below.
|/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we’ll need the current
password for the root user. If you’ve just installed MariaDB, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
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: ROOTPASSWORD
Re-enter new password: ROOTPASSWORD
Password updated successfully!
Reloading privilege tables..
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
Remove anonymous users? [Y/n] Y
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
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…
– Removing privileges on test database…
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
All done! If you’ve completed all of the above steps, yourMariaDB
installation should now be secure.
Thanks for using MariaDB!
Step 3- Installing PHP
Though python or perl is also used in LEMP stack, we will be using PHP which is used widely in the stack. To install php, run
|$ yum install php php-mysql php-fpm
First thing we should after installing php is to set ‘cgi.fix_pathinfo’ parameter to zero, as this is security threat and makes PHP vulnerable. This parameter tells to executes the closest file , in event a php file is missing. To disable it, goto ‘/etc/php.ini’ & search for the parameter, then uncomment it by removing ‘;’ & set value to zero,
|$ vi /etc/php.ini
Next we will goto ‘/etc/php-fpm.d/www.conf’ & change the user & group to ‘nginx’
user = nginx
Restart the services to implement the changes.
|$ systemctl restart php-fpm
Step 4 – Testing PHP installation
Laslty we will test our php setup by creating a test php page . To do so, create a file named ‘info.php’ in the default nginx document directory i.e. ‘/usr/share/nginx/html’ & add the following content
|$ vi /usr/share/nginx/html/info.php
Save the file & restart the nginx services for changes to be implemented. After restarting the nginx services, open web browser & enter IPaddress of the system followed by /info.php
It will open a page with PHP details like versions, release date etc.
That’s it guys, this was our article on creating LEMP server. Please feel free to mention your queries in the comment box below.