November 12, 2019
4min Read
Edgaras G.
In this tutorial, you will learn how to install LEMP stack on a CentOS 7 server. It is one of the most popular groups of software that you can use to build your website. Let’s get right into it.
LEMP is a stack of programs that work collectively to serve dynamic websites or web applications. It consists of Linux (the server’s operating system), ENginX/Nginx (web server application), MySQL (database management system), and PHP (scripting language).
LEMP is a popular alternative to LAMP, which uses Apache instead of Nginx and focuses on serving static web pages. There are also WAMP and MAMP that replace Linux with Windows and macOS.
Before we begin the tutorial on how to install LEMP, make sure that your server or VPS is running on CentOS 7. If you’re using a Hostinger VPS plan, install CentOS from the Servers tab of hPanel.
Now that Linux is installed, we are going to guide you through the installations of Nginx, MySQL, and PHP.
This tutorial requires you to have root access and use an SSH client like PuTTY (Windows) or terminal shell (Linux, macOS) to connect to your VPS.
yum install epel-release -y
yum install nginx -y
systemctl start nginx systemctl enable nginx
Use the sudo command if you don’t have root access
We’re going to install MySQL using MariaDB service, a community fork of MySQL.
yum install mariadb-server mariadb -y
systemctl start mariadb systemctl enable mariadb
mysql_secure_installation
MariaDB will ask you for the root password, which you do not have because you’ve just installed MySQL. For that reason, simply press enter.
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 authorization. New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success!
Remove anonymous users? [Y/n] Disallow root login remotely? [Y/n] Remove test database and access to it? [Y/n] Reload privilege tables now? [Y/n]
After you complete the setup, proceed to PHP installation.
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm rpm -Uvh remi-release-7.rpm
yum install yum-utils -y yum-config-manager --enable remi-php73
yum --enablerepo=remi,remi-php73 install php-fpm php-common
When asked for installation permission, simply press Y.
yum --enablerepo=remi,remi-php73 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml
The last thing we need to do is configure Nginx to work with PHP 7. Here’s how you do it:
nano /etc/nginx/conf.d/default.conf
Then, insert this code:
server { listen 80; server_name your_server_ip; # note that these lines are originally from the "location /" block root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ .php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
IMPORTANT! Replace your_server_ip with your actual server IP.
systemctl restart nginx
nano /etc/php-fpm.d/www.conf
Find and change these lines:
listen = /var/run/php-fpm/php-fpm.sock
systemctl start php-fpm.service systemctl enable php-fpm.service
LEMP (Linux, ENginx, MySQL, PHP) stack is the perfect choice to serve dynamic web pages or web applications. Fortunately, the installation process is simple. With basic knowledge of the command-line interface, you can easily set up all the required programs on your VPS.
In this article, you have learned how to install LEMP on CentOS 7. To conclude, let’s take a look at all the steps once again:
Good luck and feel free to leave a comment if you have any questions!
April 29 2017
This tutorial was a lifesaver. I could not get PHP-FPM to work on my newly-setup LEMP with VestaCP. For hours I only had LEM with no P because I could not get PHP-FPM configured. Your instructions got me past a huge learning curve. Everything except phpMyAdmin is working now.
June 17 2017
Awesome post. Works as expected!
July 30 2017
when I create this file server { listen 80; server_name your_server_ip; # note that these lines are originally from the "location /" block root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } my nginx can't run, once I check sudo nginx -t, system said nginx: [emerg] unknown directive "my servername" in /etc/nginx/conf.d/default.conf:4 nginx: configuration file /etc/nginx/nginx.conf test failed how to fix it? regards
August 03 2017
Hi great turtorial just one problem i want install phpbb on it but It dont work..... any that have install it and It working .....please share it!
September 26 2017
I've gone through this tutorial in setting up a new server, but how can I check to see if PHP is working properly? I tried using a file designed to output phpinfo as a test, but it simply downloads instead of loading in the browser.
Replied on November 17 2017
I have this result also.
Replied on April 15 2018
Hey! It's probably because the location directive for .php-files wasn't put in the nginx.conf file. If you input the following code below the other location directives (like the '/'-one) it should work: location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } Good luck! ;)
Replied on April 10 2019
--- Put that in a php file, inside the html directory & use a browser to see it.
January 06 2018
Your website design sucks. Can't open a site in half on desktop alongside with terminal all commands float too much on left. Had to fix some CSS manually in inspection mode. And somehow those reditt share button is popping up when clicking on comments box. Thank you so much for this tutorial though
Replied on January 06 2018
oh and who puts those commands in table next time use
or
html tagsReplied on January 15 2018
Hey, Sorry, but where do you see commands in the table?
Replied on January 15 2018
Hey Garry, Thank you for honest feedback. We are in a process of redesigning this our tutorials portal.
April 06 2018
To test installation create a file /usr/share/nginx/html/info.php with this content "<?php phpinfo();" If you open the page in your browser, and it download the page instead of showing content, then you have to comment in /etc/nginx/nginx.conf the "server" block below the line "include /etc/nginx/conf.d/*.conf;" Save, restart nginx and its solved.
Replied on April 28 2018
Hey Jorge, Nice one, thanks!
July 17 2018
Thanks! Saved me! The best tutorial out there for PHP7!
August 08 2018
Thanks, this guide really helped.
April 05 2019
worked perfectly
August 12 2019
Works as expected, Thanks a lot
pvsukale
Replied on August 07 2017
Hey try replacing that line with `server_name ;`