How to Install LEMP Stack (Nginx, MySQL, PHP v7) on CentOS 7
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.
What Is LEMP?
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 Linux VPS hosting plan, install CentOS from the Servers tab of hPanel.
How to Install LEMP Stack
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.
Step 1: Installing Nginx on CentOS 7
- Since Nginx is not available in default CentOS repositories, we need to install EPEL repository first by running this command:
yum install epel-release -y
- Next, we will install Nginx itself:
yum install nginx -y
- After the installation is completed, type the following lines one by one to activate Nginx:
systemctl start nginx systemctl enable nginx
- Check whether Nginx is running or not by visiting your server’s public IP address. Your page should look like this:
Pro Tip
Use the sudo command if you don’t have root access.
Step 2: Installing MySQL (MariaDB)
We’re going to install MySQL using MariaDB service, a community fork of MySQL.
- MariaDB is included in the default CentOS repositories, so we can simply run the yum command to install it:
yum install mariadb-server mariadb -y
- After the installation is completed, enable and start MariaDB service:
systemctl start mariadb systemctl enable mariadb
- Secure the installation by running the following command:
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.
- The next prompt will ask if you want to set a root password. Enter Y and follow the instructions:
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!
- The script will return a few security questions. We recommend you to press Y for all of them.
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.
Step 3: Installing PHP v7.3
- We need to download and install an additional CentOS repository that contains the required packages for PHP v7.3. Run these commands one after another:
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm rpm -Uvh remi-release-7.rpm
- Enable php73 repository, which is disabled by default:
yum install yum-utils -y yum-config-manager --enable remi-php73
- After that, install the PHP package:
yum --enablerepo=remi,remi-php73 install php-fpm php-common
When asked for installation permission, simply press Y.
- Now, install common PHP modules to make sure the service is working properly:
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
Step 4: Configuring Nginx to Work with PHP 7
The last thing we need to do is configure Nginx to work with PHP 7. Here’s how you do it:
- Create a new Nginx configuration file by running nano text editor:
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.
- Save the file by hitting CTRL + X (or CMD + X for Mac users). Next, restart Nginx so the changes can take effect:
systemctl restart nginx
- Open PHP-FPM configuration:
nano /etc/php-fpm.d/www.conf
Find and change these lines:
- user = apache to user = nginx
- group = apache to group = nginx
- listen.owner = nobody to listen.owner = nginx
- listen.group = nobody to listen.group = nginx
- After it’s done, add the following line under ;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
- Save the file by hitting CTRL + X. Lastly, start PHP-FPM and enable it on boot:
systemctl start php-fpm.service systemctl enable php-fpm.service
Conclusion
LEMP (Linux, Nginx, 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:
- Get EPEL repository and install Nginx on your server.
- Install and configure MariaDB
- Install PHP and all of its common packages.
- Configure Nginx to work with PHP.
Good luck and feel free to leave a comment if you have any questions!
Comments
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 07 2017
Hey try replacing that line with `server_name ;`
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.
November 17 2017
I have this result also.
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! ;)
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
January 06 2018
oh and who puts those commands in table next time use
orJanuary 15 2018
Hey, Sorry, but where do you see commands in the table?
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.
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
December 26 2020
Fantastic, really saved my day. Thanks a lot.