Connect with us


How To Install Linux Nginx MySQL PHP (LAMP) stack On CentOS 7



How To Install Linux, Nginx, MySQL, PHP (LAMP) stack On CentOS 7

In this article, we’ll get a LAMP stack installed on an CentOS 7 VPS. If you don’t know how to install Centos 7 ,link is attached please follow the steps.


Before you begin with this tutorial, you should have a separate, non-root user account set up on your server.

Note: If you have issues with Nginx not running, make sure the SELinux of your Nginx configuration are correct or change the SELinux mode to permissive or disabled.

Step One — Install Nginx

In order to display web pages to our site visitors, we need to first install Nginx on server. Add the CentOS 7 EPEL repository, open terminal and use the following command:

$ sudo yum install epel-releas

We are using a sudo command, these operations get executed with root privileges. It will prompt for your regular user’s password to verify that you have privileges to run commands with root.

Now you have to install Nginx repository on your server, install Nginx using the following yum command:

$ sudo yum install nginx

After the installation you can start Nginx on your VPS.

$ sudo systemctl start nginx

Once the installation will complete you will see the default CentOS 7 Nginx web page. It will look something like the screenshot shown below. if you see the web page like this then your web server is correctly installed.

Use the following command to enable Nginx to start on system boot.

$ sudo systemctl enable nginx

Step Two – Install MySQL (MariaDB)

Now we have to install MariaDB, a MySQL drop-in replacement. MariaDB is a community-developed fork of the MySQL relational DMS. Run the following commond to install DB.

sudo yum install mariadb-server mariadb

After the installation is complete, run the following commond to start MariaDB:

$ sudo systemctl start mariadb

We needto run a simple security script that will lock down access to our database system a little bit and remove some dangerous defaults. Run the following script:

$ sudo mysql_secure_installation

There will be a prompt asking for your current root password. You don’t have one, so leave it blank by pressing enter. Then it will prompt again and ask you to set a root password if you want. Go ahead and enter Y, and follow the instructions:

mysql_secure_installation prompts: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: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
 ... Success!

The last thing you need to do is to enable MariaDB to start on boot. Use the following command for this:

$ sudo systemctl enable mariadb

Step Three — Install PHP

In this step we will install PHP FPM by using the following command:

$ sudo yum install php php-mysql php-fpm

PHP Processor Configurations

Now Open the main php-fpm configurations file with root privileges from the following path:

$ sudo vi /etc/php.ini

In this file we are looking for cgi.fix_pathinfo parameter. This will be disabled and commented out with a semi-colon (;) with default value to “1”.

First We will remove the semi-colon and then we will set the value from 1 to “0” like this:

Path: /etc/php.ini excerp

$ cgi.fix_pathinfo=0

Now close this file and open the PHP-FPM configurations file from the following path:

$ sudo vi /etc/php-fpm.d/www.conf

Find and change the line that specifies the listen parameter as shown bellow.

listen = /var/run/php-fpm/php-fpm.sock

Now find the lines that set the listen.owner and and uncomment them both.

listen.owner = nobody = nobody

There is something more you need to do, find the lines that set the user and group and change their values as shown bellow.

user = nginx

group = nginx

Then save and quit and start our PHP processor by using the following command:

$ sudo systemctl start php-fpm

Now set php-fpm to start on boot with this command:

$ sudo systemctl enable php-fpm

Step Four – Configure Nginx to Process PHP

All of the required components has been installed. The only pending configuration we still need to do is tell Nginx to use our PHP processor for dynamic content pages.

Open the default Nginx server block level configuration file by using the following command:

$ sudo vi /etc/nginx/conf.d/default.conf

By default the file will look like as following:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;

If you want you can just copy and paste everything, then replace with you file.

server {
    listen       80;
    server_name  server_domain_name_or_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;

Save the file and restart Nginx to make the necessary changes:

$ sudo systemctl restart nginx

That’s it you are done with all configurations. If you like this article do share with you friends to help them.

Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *