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 it’s widespread support, while nginx is chosen due to it’s 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 the 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:
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.
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.
Now you can perform various actions on your database, but we will not discuss about 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 complete, 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 firewalld. 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:
Edit the parts in red brackets:
- Set server_name to your server’s IP.
- Set all root path to /usr/share/nginx/html, this directory will become your web’s root folder.
- Uncomment error 404 section.
- Uncomment this section and change root path to /usr/share/nginx/html
- 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.
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:
Then add the following block of lines inside the file:
<?php phpinfo(); ?>
3) Save and exit the file. Then, configure php-fpm by typing:
Then, search for these 2 lines and replace the “apache” of user and group to “nginx”.
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.
You have successfully installed LEMP onto your server. Now you may proceed to run your own website using WordPress or other similar applications.