Search Our Database

Install LEMP on CentOS 7

Last updated on |
by

LEMP is an acronym for Linux, Nginx (Engine x), MySQL, and PHP. It is needed to run web servers due to it containing various software required for other web content management software, such as WordPress and also Joomla.

LEMP is similar to LAMP, where LAMP uses Apache instead of Nginx as open source web servers. Apache is usually chosen due to its widespread support, while Nginx is chosen due to its effectiveness under load. There’s an article on setting up LAMP on CentOS 6 here if you prefer to use Apache instead of Nginx.

One big difference between Apache and Nginx is the actual way that they handle connections and traffic. This provides perhaps the most significant difference in a way that they respond to different traffic conditions.

Step 1: Install Repositories and Dependencies

First SSH to your server, then install the required repositories by typing the below command:

yum install epel-release

Also, download this file, upload it to your server using FTP such as FileZilla. Put it inside /opt and run the following command:

rpm -Uvh /opt/libunwind-1.1-94.3.x86_64.rpm

 

Step 2: Install MySQL (MariaDB)

CentOS 7’s repository comes packaged with MariaDB instead of MySQL by default, thus we use MariaDB in place of MySQL

1) Run the following command to install MariaDB onto your system:

yum install mariadb-server mariadb

2) Then, type the following commands to start and enable MariaDB on boot:

systemctl start mariadb.service
systemctl enable mariadb.service

3) After that, we add in some security measures to our database using the following command:

mysql_secure_installation

Since we just installed MariaDB there shouldn’t be a root password, press enter then select if you want to add in a password for MariaDB root account. After that just put “y” for the rest of the prompts.

lamp2

4) Now access MariaDB by logging in as root using the following command:

mysql -u root -p

Enter the password you have just set (if any) and press Enter.

lamp3

Now you can perform various actions on your database, but we will not discuss that here. Type “exit” and Enter to exit MariaDB.

 

Step 3: Install Nginx

1) Install Nginx web server using yum install:

yum install nginx

2) After installation is completed, start Nginx service using :

systemctl start nginx.service
systemctl enable nginx.service

3) Before testing if Nginx is running, we need to turn off firewall settings. In CentOS 7, iptables are replaced by firewalls. Use the following command to turn it off:

systemctl stop firewalld.service

4) Now we need to configure Nginx to process PHP pages, type the following:

vi /etc/nginx/nginx.conf

lemp1

Edit the parts in red brackets:

  1. Set server_name to your server’s IP.
  2. Set all root path to /usr/share/nginx/html, this directory will become your web’s root folder.
  3. Uncomment error 404 section.
  4. Uncomment this section and change root path to /usr/share/nginx/html
  5. Uncomment the “location ~ \.php” section, change the fastcgi_pass to “127.0.0.1:9000” (without quote marks) and also the fastcgi_param path to $document_root$fastcgi_script_name.

5) By typing in your server’s IP in your browser URL, you can confirm if Nginx is successfully installed. Below is what it should look like.

lamp5

 

Step 4: Install PHP

1) Run the following command to install PHP onto your server:

yum install php-fpm php-mysql

2) Then we need to create an info.php file for checking PHP status on your server. Create info.php using the following line:

vi /usr/share/nginx/html/info.php

Then add the following block of lines inside the file:

<?php
phpinfo();
?>

3) Save and exit the file. Then, configure php-fpm by typing:

vi /etc/php-fpm.d/www.conf

Then, search for these 2 lines and replace the “apache” of user and group to “Nginx”.

lamp6

4) Save and exit. We now need to start the php-fpm service and also restart Nginx service:

systemctl start php-fpm.service
systemctl restart nginx.service

5) After all that, type “http://your-ip-address/info.php” (without quote marks) into browser URL to view your PHP status page.

lempphp

You have successfully installed LEMP onto your server. Now you may proceed to run your own website using WordPress or other similar applications.