In this article we will explain how to install a LEMP stack (Linux, Nginx, MariaDB, PHP) along with PHP-FPM on RHEL/CentOS 7/6 and Fedora 26-29 servers using yum and dnf package manager.
During the process we will install and enable Epel, Remi, Nginx and MariaDB repositories in order to be able to install the latest versions of these packages.
Read Also: Install Apache, MySQL 8 or MariaDB 10 and PHP 7 on CentOS 7
Step 1: Installing EPEL and Remi Repository
EPEL (Extra Packages for Enterprise Linux) is a community based repository offers add-on software packages for RHEL-based Linux distributions.
Remi is a repository where you can find the latest versions of the PHP stack (full featured) for installation in the Fedora and Enterprise Linux distributions.
On RHEL/CentOS 7
# yum update && yum install epel-release # rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm ------ For RHEL 7 Only ------ # subscription-manager repos --enable=rhel-7-server-optional-rpms
On RHEL/CentOS 6
# yum update && yum install epel-release # rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm ------ For RHEL 6 Only ------ # subscription-manager repos --enable=rhel-6-server-optional-rpms
On Fedora 24-29
# rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-29.rpm [On Fedora 29] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-28.rpm [On Fedora 28] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-27.rpm [On Fedora 27] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-26.rpm [On Fedora 26] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-25.rpm [On Fedora 25] # rpm -Uvh http://rpms.remirepo.net/fedora/remi-release-24.rpm [On Fedora 24]
Step 2: Installing Nginx and MariaDB Repositories
The Nginx repository is only needed in RHEL and CentOS distributions. Create a file called /etc/yum.repos.d/nginx.repo and add the following lines to it.
For RHEL 7/6:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/ gpgcheck=0 enabled=1
For CentOS 7/6:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1
To enable the MariaDB repository, create a file named /etc/yum.repos.d/mariadb.repo with the following contents:
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.1/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
Step 4: Installing Ngnix and MariaDB
Nginx (Engine X) is open source, robust, lightweight and high performance Web server, reverse proxy sever and also mail proxy server for HTTP, SMTP, POP3 and IMAP protocols. For further details, visit http://wiki.nginx.org/Overview.
MariaDB is a fork of the well-known MySQL, one of the world’s most popular Relational Database Management System (RDBMS). It is entirely developed by the community and as such it is intended to remain FOSS and compatible with the GPL.
To install Ngnix and MariaDB, run the following commands.
----------- Installing on RHEL/CentOS 7/6 ----------- # yum --enablerepo=remi install nginx MariaDB-client MariaDB-server php php-common php-fpm ----------- Installing on Fedora ----------- # dnf --enablerepo=remi install nginx MariaDB-client MariaDB-server php php-common php-fpm
Step 3: Installing PHP Using Remi Repository
PHP (Hypertext Preprocessor) is a Free and Open Source server-side scripting language that is best suited for web development. It can be used to produce dynamic web pages for a website and is most frequently found in *nix servers. One of the advantages of PHP is that it is easily extensible through the use of a wide variety of modules.
To install PHP, first you need to enable Remi repository by installing yum-utils
, a collection of useful programs for managing yum repositories and packages.
# yum install yum-utils
Once installed, you can use yum-config-manager provided by yum-utils
to enable Remi repository as the default repository for installing different PHP versions as shown.
For example, to install PHP 7.x version, use the following command.
------------- On CentOS & RHEL ------------- # yum-config-manager --enable remi-php70 && yum install php [Install PHP 7.0] # yum-config-manager --enable remi-php71 && yum install php [Install PHP 7.1] # yum-config-manager --enable remi-php72 && yum install php [Install PHP 7.2] # yum-config-manager --enable remi-php73 && yum install php [Install PHP 7.3] ------------- On Fedora ------------- # dnf --enablerepo=remi install php70 [Install PHP 7.0] # dnf --enablerepo=remi install php71 [Install PHP 7.1] # dnf --enablerepo=remi install php72 [Install PHP 7.2] # dnf --enablerepo=remi install php73 [Install PHP 7.3]
Next, we are going to install all these following PHP modules.
------ On RHEL/CentOS 7/6 ------ # yum --enablerepo=remi install php-mysqlnd php-pgsql php-fpm php-pecl-mongo php-pdo php-pecl-memcache php-pecl-memcached php-gd php-xml php-mbstring php-mcrypt php-pecl-apcu php-cli php-pear ------ On Fedora ------ # dnf --enablerepo=remi install php-mysqlnd php-pgsql php-fpm php-pecl-mongo php-pdo php-pecl-memcache php-pecl-memcached php-gd php-xml php-mbstring php-mcrypt php-pecl-apcu php-cli php-pear
Step 6: Stopping and Disabling Apache Service
By default, Apache and Nginx listen in same port (TCP 80). For that reason, if Apache is installed in your server, you need to stop it and disable / mask it (a stronger version of disable that links the service to /dev/null) in order to use Nginx, or you can remove it if you’re not planning on using it anymore.
# systemctl stop httpd # systemctl disable httpd or # systemctl mask httpd
Step 7: Starting/Stopping Nginx, MariaDB and PHP-FPM
----------- Enable Nginx, MariaDB and PHP-FPM on Boot ----------- # systemctl enable nginx # systemctl enable mariadb # systemctl enable php-fpm ----------- Start Nginx, MariaDB and PHP-FPM ----------- # systemctl start nginx # systemctl start mariadb # systemctl start php-fpm
Step 8: Configuring Nginx and PHP-FPM
Let us now create a directory structure for your website (a virtual host, or server block as it is called in Nginx) under /srv/www/. In this example we will use www.tecmint.com
, but feel free to choose another domain and main directory if you want.
# mkdir -p /srv/www/tecmint/public_html # mkdir /srv/www/tecmint/logs # chown -R nginx:nginx /srv/www/tecmint
Step 9: Configuring Nginx Virtual Host Directories
As you know, the ability of running several sites from the same machine is one of the distinguishing features of major web servers. Let us now proceed to create the directories to store our server blocks (known as virtual hosts in Apache) under /etc/nginx.
# mkdir /etc/nginx/sites-available # mkdir /etc/nginx/sites-enabled
The following line of code, which must be inserted before closing the http block in /etc/nginx/nginx.conf, will ensure that configuration files inside the /etc/nginx/sites-enabled directory will be taken into account when Nginx is running:
## Load virtual host conf files. ## include /etc/nginx/sites-enabled/*;
To create the server block for tecmint.com
, add the following lines of code to /etc/nginx/sites-available/tecmint (this file will be created when you enter the full path to start your preferred text editor). This is a basic virtual host config file.
server { listen 80 default; server_name tecmint; access_log /srv/www/tecmint/logs/access.log; error_log /srv/www/tecmint/logs/error.log; root /srv/www/tecmint/public_html; location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; } }
The process of “activating” a virtual host consists of creating a symbolic link from the definition of the tecmint
virtual host to /etc/nginx/sites-enabled.
# ln -s /etc/nginx/sites-available/tecmint /etc/nginx/sites-enabled/tecmint
In order to actually apply the changes we have been doing, we now need to restart Nginx. It is sometimes useful to check the configuration files for syntax errors before doing so:
# nginx -t # systemctl restart nginx # systemctl status nginx
To access your newly created virtual host, you need to add the following line to /etc/hosts as a basic method of domain name resolution.
192.168.0.18 www.tecmint.com tecmint.com
Step 10: Testing Nginx, MySQL, PHP and PHP-FPM
Let’s stick with the classic way of testing PHP. Create a file called test.php
under /srv/www/tecmint/public_html/ and add the following lines of code to it.
The phpinfo()
function shows a great deal of information about the current PHP installation:
<?php phpinfo(); ?>
Now point your web browser to http://tecmint/test.php
and check the presence of the installed modules and additional software:
Congratulations! You now have a working installation of a LEMP stack. If something did not go as expected, feel free to contact us using the form below. Questions and suggestions are also welcome.
Hi,
When I will restart nginx service, i get below error:
Then, i run jounalctl -xe command, so i get this error:
-- Unit nginx.service has begun starting up.
سپتامبر 07 10:43:22 localhost.localdomain nginx[15561]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
سپتامبر 07 10:43:22 localhost.localdomain nginx[15561]: nginx: [emerg] open() "/srv/www/testsite/logs/access.log" failed (13:
سپتامبر 07 10:43:22 localhost.localdomain nginx[15561]: nginx: configuration file /etc/nginx/nginx.conf test failed
سپتامبر 07 10:43:22 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
سپتامبر 07 10:43:22 localhost.localdomain systemd[1]: Failed to start nginx - high performance web server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
سپتامبر 07 10:43:22 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
سپتامبر 07 10:43:22 localhost.localdomain systemd[1]: nginx.service failed.
سپتامبر 07 10:43:22 localhost.localdomain polkitd[599]: Unregistered Authentication Agent for unix-process:15555:5312320 (syste
[root@localhost ~]#
How I can solve this problem? please help me.
What is the problem, Ravi? How can I fix it?
2017/03/18 13:06:48 [error] 5956#0: *8012409 FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream, client: 5.111.252.13, server: https://akatorrent.com, request: “GET /movies.php?genre=horror HTTP/1.1”, upstream: “fastcgi://unix:/tmp/php_fpm.sock:”, host: “https://akatorrent.com”
Hi team,
I was followed this tutorial, everything works fine except an error in error log, many logs was generated
What kind of this error, team? And how can I fix it?
The website torrent4all.com is work normally?
You should check your error log carefully, “GET /wp-login.php HTTP/1.1”
I think it is wrong path
———– Installing on RHEL/CentOS 7/6 ———–
# yum –enablerepo=remi install nginx MariaDB-server (depend: MariaDB-client) php php-common php-fpm
repo MariaDB for CentOS – http://yum.mariadb.org/10.0/centos6-amd64
How to install LEMP on CentOS 7 x86_64 ?
Please use the below link to setup LEMP on CentOS 7.
https://www.tecmint.com/install-nginx-in-centos-7/
I did exactly what it says several times and I do not phphinfo displays. Only The webpage cannot be found.
My system is VPS, CentOS 6, nginx, mysql, php.
Sorry for trouble, actually the article is little outdated and might some of the functions not work due to latest nginx version. Please use the below guide and follow the only nginx configuration part.
Install LEMP (Linux, Nginx, MySQL and PHP) in Arch Linux
Hi,
I installed nginx and php follow above tips, the nginx was ok and I can see the “Welcome to nginx!” site. But when I visit the php site, the browser download the php file. How can I fix this issue?
Hello Ravie. Great tutorial here. Thank’s.
I was success installed nginx and sql by the following howtoforge tutorial and try to install wp on it but i have 403 error Forbidden when go to http://wordpress/. i has ckecked my code, and i am sure it correctly.
Please advice me.
Hello,
Nginx is perfectly working in my server with your help…thanks for that…..
I think the nginx is working is based on the reverse proxy concept…. So it needs two or more machines to setup and understand the actual working of nginx…,…..
1. So, is this configuration is for proxy server or proxy client machine…………? Or Do we have to install nginx on both machines..?
2. In which configuration file, will we mention the proxy client details…… Is it in Virtual host file?
Thank you
Hi Ravi,
I installed LEMP with this document.
But I can’t Install MariaDB.
( https://www.tecmint.com/install-mariadb-in-linux/ )
How can I solve this?
Thank you.
CentOS 6.3 64bit.
Error Summary is below.
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: data.nicehosting.co.kr
* epel: http://ftp.neowiz.com
* extras: data.nicehosting.co.kr
* remi: remi.kazukioishi.net
* remi-test: remi.kazukioishi.net
* updates: data.nicehosting.co.kr
Setting up Install Process
Package nginx-1.4.4-1.el6.ngx.x86_64 already installed and latest version
Package mysql is obsoleted by MariaDB-client, trying to install MariaDB-client-5.5.34-1.x86_64 instead
Package mysql-server is obsoleted by MariaDB-server, trying to install MariaDB-server-5.5.34-1.x86_64 instead
Package php-5.4.23-1.el6.remi.x86_64 already installed and latest version
Package php-common-5.4.23-1.el6.remi.x86_64 already installed and latest version
Package php-fpm-5.4.23-1.el6.remi.x86_64 already installed and latest version
Resolving Dependencies
–> Running transaction check
—> Package MariaDB-client.x86_64 0:5.5.34-1 will be installed
–> Processing Dependency: MariaDB-common for package: MariaDB-client-5.5.34-1.x86_64
—> Package MariaDB-server.x86_64 0:5.5.34-1 will be installed
–> Running transaction check
—> Package MariaDB-common.x86_64 0:5.5.34-1 will be installed
–> Processing Dependency: MariaDB-compat for package: MariaDB-common-5.5.34-1.x86_64
–> Running transaction check
—> Package MariaDB-compat.x86_64 0:5.5.34-1 will be installed
–> Finished Dependency Resolution
Dependencies Resolved
===================================================================================================================================================================
Package Arch Version Repository Size
===================================================================================================================================================================
Installing:
MariaDB-client x86_64 5.5.34-1 mariadb 10 M
MariaDB-server x86_64 5.5.34-1 mariadb 34 M
Installing for dependencies:
MariaDB-common x86_64 5.5.34-1 mariadb 23 k
MariaDB-compat x86_64 5.5.34-1 mariadb 2.7 M
Transaction Summary
===================================================================================================================================================================
Install 4 Package(s)
Total size: 47 M
Installed size: 195 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Check Error:
file /usr/share/mysql/czech/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/danish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/dutch/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/english/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/estonian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/french/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/german/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/greek/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/hungarian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/italian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/japanese/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/korean/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/norwegian-ny/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/norwegian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/polish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/portuguese/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/romanian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/russian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/serbian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/slovak/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/spanish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/swedish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/ukrainian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /etc/my.cnf from install of MariaDB-common-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
file /usr/share/mysql/charsets/Index.xml from install of MariaDB-common-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
Error Summary
————-
The error indicates that your mysql-libs package conflicting with MariaDB-common. So, remove the mysql-libs package using the yum as shown.
And, then try to install Maria DB..
Thank you very much!!!
—
Before, I just do
#yum remove mysql-server mysql-client
to remove mysql
—
As you say,
# yum remove mysql-libs
and
https://www.tecmint.com/install-mariadb-in-linux/
It works!!!
Hello Ravi,
While accessing http://tecmint/phpinfo.php , its shows 404 error.What maybe the problem.I checked ownership permissions and all. But accessing techmint or localhost, it says “Welocome to nginx………………….”
@Ashiq, replace tecmint with localhost in URL and try again.
Excellent work. Keep it up.
Hey Ravi,
Thanks for the tutorial. I have a little problem at the end of it.
When I try to access http://my-website-name.com/phpinfo.php I get a “404 Not Found” error.
Could this be because i wrote “my-website-name” instead of “my-website-name.com” evertime?
http://i.imgur.com/R1RiRPY.png
Yes! you must access your domain with “.com” extension. Try it, I hope you will see PHP info.
Hi Ravi,
Thank you for great work. I am a beginner with linux and planning to host a website from my homeserver on CentOS. I Have followed both of your tutorial on Nginx + PHP & Phpmyadmin. However I am not able to get the phpmyadmin screen. I would also like to install drupal on it. I have created 2 vhost, drupal & phpmyadmin. But only able to get the drupal installation screen. phpmyadmin vhost gives 404 error. Would you please be kind to make tutorial for CentOS+ Nginx+PHP+ MysQL+ Phpmyadmin+ drupal on a combined vhost file please? I would be very grateful to you. I am sure it will be useful for many others like me…. Thanks again
OK noted your requirement, will write up on this soon, if we get time. stay tuned for this.
This is exactly the problem im having too. A tutorial for that would be great. I’m hoping this will be for 6.4? Thanks for the tutorials though they are great.
Ok, I think we need to recheck the article to make it 100% working. Give us some time to check.
Hi Ravi,
Can Nginx run both php 5.2 and 5.4 ?
Thanks,
Yes! Nginx can run both the PHP versions.
Sir can you tell me the default web directory…….
Were I will just copy and paste my html and php file that can browsable from other clients to view that web page.
The default web directory for for Nginx is /srv/www/. Please follow the guide correctly.
hello, I have the following problem in php-fpm. I set up nginx + php-fpm + mysql and it occurs, the processes of php-fpm are always running on Linux, even after the client close the connection, the processes remain active, the only way to solve it by restarting the service php -fpm. Did this happen to you?
http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
The 6-7 release is no longer active via the link in the first section of this post. Just a heads up in case someone encounters a 404 when attempting to install the RPM.
Thanks.. updated now..
how to fix error 502 Bad Gateway???
nginx , php-fpm status is enable and active.
i have installed mysql
# rpm -qa mysql\*
mysql-libs-5.5.29-1.fc18.x86_64
mysql-server-5.5.29-1.fc18.x86_64
mysql-5.5.29-1.fc18.x86_64
how to install php-cgi ??
Please use the below command is being used for latest fedora 18/17 onwards to start services.
Have you installed MySQL and php-cgi packages correctly? If not, install them correctly. Because the errors clearly saying you don’t have those files.
# chkconfig –add mysqld
error reading information on service mysqld: No such file or directory
# /etc/init.d/mysqld start
bash: /etc/init.d/mysqld: No such file or directory
# /etc/init.d/php-fpm start
Starting php_fpm /etc/init.d/php-fpm: line 44: /usr/local/bin/php-cgi: No such file or directory
failed
Hi,
I am unable to install php-fpm on centos 6.2. getting below mentioned error.
[root@localhost home]# yum –enablerepo=remi,remi-test install nginx mysql mysql-server php php-common php-fpm
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.01link.hk
* extras: centos.01link.hk
* updates: centos.01link.hk
Setting up Install Process
Package nginx-1.2.5-1.el6.ngx.x86_64 already installed and latest version
Package mysql-5.1.66-1.el6_3.x86_64 already installed and latest version
Package mysql-server-5.1.66-1.el6_3.x86_64 already installed and latest version
Package php-5.3.3-14.el6_3.x86_64 already installed and latest version
Package php-common-5.3.3-14.el6_3.x86_64 already installed and latest version
No package php-fpm available.
Nothing to do
@Ravi
You have not added remi repository. see the above errors the mirrors loading from CentOS base. Install remi repository and try again.
YUM SERVER & DNS SERVER configuration on RHEL 6
In step 7, why some of commands in “Creating Website Directory” and “Creating Website Logs” such as mkdir -p /srv/www/tecmint/public_html & chown -R nginx:nginx /srv/www/tecmint are duplicate?