In this tutorial we will learn how to install (LEMP) Nginx, MySQL or MariaDB and PHP with PHP-FPM on CentOS 7 server. Before starting just a little introduction what is “Nginx”. Nginx is an open-source, light weight high-performance HTTP server packed with lots of great features and that is very low in resource consumption.
This comprehensive guide walks you through the installation while expecting you to run the given commands on the linux machine. if you proceed as a privileged user you can simply run them but if you proceed as a non privileged one, you will proceed by using the commands with
Since we have to install Nginx, MySQL or Maria DB and PHP modules, we need to set up EPEL repository. EPEL (Extra Packages for Enterprise Linux) provides us the extra packages we need to install on our CentOS 7 server.
By default, CentOS 7 does not have these packages as native, so we will enable the EPEL. Using wget, we will download and install the EPEL RPM to enable it.
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -ivh epel-release-latest-7.noarch.rpm
Once we have enabled the EPEL, we can verify it using yum.
We should see a list, where all the EPEL packages were shown. Now lets move forward and install our LEMP stack.
To serve web pages to our visitors, we will be installing Nginx, a modern and efficient web server. For this we will be using yum package manager to complete the installation.
yum install nginx
After that start nginx from systemctl
systemctl start nginx
Now make the service permanent during boot by below command
systemctl enable nginx
Now we need to open the required ports on our CentOS 7 server to accept the connections. CentOS 7 uses firewalld daemon for this purpose, open the ports using firewall-cmd utility as shown.
firewall-cmd --permanent --add-port=80/tcp firewall-cmd --reload
Type in your server’s IP address or hostname or
http://Your_Server_ip and you will see a default with Nginx output.
If you are not getting the output from the we page, then try stopping the firewalld daemon.
Install MySQL / MariaDB
Since MariaDB is a MySQL fork of the original MySQL developer Monty Widenius, We will go with MariaDB.
yum -y install mariadb-server mariadb
After installation we need to start and enable the DB services.
systemctl start mysqld
Now we need to configure the Database to complete the installation n
We will be asked to enter the password for the MySQL root account, since
we haven’t set this yet, so just hit ENTER . Then we will be asked to set that password. we should type Y to set a root password.
To rest of all the questions, the script asks, you should press Y, followed by the ENTER key at each prompt. During this process we will remove anonymous users and the test database, disable remote root logins, and load these new rules MySQL.
Once all set restart MySQL:
systemctl restart mysqld
NOTE :This is an optional step, since MariaDB is already installed. If you want to go with MySQL instead of MariaDB, then this for you.
First we need to add MySQL repo for centos 7 server.
yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
yum install mysql-community-server
make MySQL to auto start when system boot
systemctl start mysqld systemctl enable mysqld
Further installation is similar to MariaDB..
Install PHP And PHP-FPM
By default, php and php-fpm packages are available on CentOS 7, so we will install using yum
Follow the below commands to install packages
yum install php php-common php-fpm -y
Due to some security reasons, we need to edit the following file.
Find the line, cgi.fix_pathinfo=1, and change the 1 to 0.
If this number is set to 0, the interpreter will only process the exact file path, which minimize the risk.
Now start and enable php-fpm
systemctl start php-fpm systemctl enable php-fpm
We can increase number of workers for Nginx to our cpu value in /etc/nginx/nginx.conf file. we can get this number from “lscpu” command.
Here our value is 2 from the value, hence we set the value to 2.
Now save and close the file, edit the “/etc/nginx/conf.d/default.conf” file with your server details as shown below
listen x.x.x.x:80; server_name www.codesposts.com
Modify the above lines as per your details, save and close the file.
Now restart Nginx service to make the changes apply
service nginx restart
We need to change the user from Apache to Nginx in php-fpm configuration file.
; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache Choosed to be able to access some dir as httpd user = nginx ; RPM: Keep a group allowed to write in log dir. group = nginx
In the above config file, user and group are replaced with “nginx”, instead of “apache”. Now save and close the file and restart php-fpm service
systemctl restart php-fpm
We will create a test file called “codesposts.php” to check the php components. Enter “<?php phpinfo(); ?>” into the below file and save it.
<?php phpinfo(); ?>
Save and close the file, restart the Nginx service.
systemctl restart nginx
we can visit this web page in a web browser from the below shown URL.
we should see a web page that has been generated by PHP with information about your server.
With this, we have successfully completed the installation of LEMP with php-fpm on CentOS 7 server.
Thanks for being with us, hope this help you in gaining knowledge and we are always ready to help you. For furthers queries or suggestions, kindly drop a comment below.