Installing LEMP-Stack on CentOS7

This guide will show you the steps needed to install a basic LEMP stack on your Non-cPanel server running CentOS. All the commands shown will need to be run as root on your server via SSH.

Installing LEMP Stack

  1. Connect to your server as the root user via SSH.
  2. Before installing we need to ensure your packages and repositories are up to date by running the following command. You will be prompted to continue after it has finished checking for updates by pressing y (yes) or n (no), Hit y and then enter to continue.

    yum update

  3. NGINX uses port 80 for non-SSL traffic and 443 for SSL traffic. Both of these ports need to be open in the firewall. Run the following three commands to open them.

    service firewalld start
    firewall-cmd –permanent –zone=public –add-service=http
    firewall-cmd –permanent –zone=public –add-service=https
    firewall-cmd –reload

  4. Install NGINX by running the following command, You will be prompted to continue, Press y for yes and hit enter.

    yum install nginx

    After the install has finished you need to start the service by running the command below.

    systemctl start nginx

  5. Installing MariaDB(MySQL), MariaDB is the recommended service for MySQL currently included in the CentOS repositories.

    yum install mariadb-server mariadb

    Once installed start the service by running the command below.

    systemctl start mariadb

    The default install of MariaDB contains settings and users for testing that are not secure, To secure the installation run the command below. You will be asked if you want to set a root password for MariaDB, You should press y and hit enter to continue, You will then be prompted to set a password for MariaDB, Ensure this password is set to a strong password. After setting the password you will get more yes or no prompts, Just hit enter until you have completed the prompts. You will see the message “installation should now be secure. Thanks for using MariaDB!” When complete.


  6. Install PHP and some common extensions for PHP by running the following command. You may be prompted to press y and enter again.

    yum install php70u php70u-mcrypt php70u-mbstring php70u-mysqlnd php70u-ioncube-loader php70u-soap php70u-common php70u-tidy php70u-pecl-imagick php70u-pspell php70u-pdo php70u-bcmath php70u-cli php70u-pear php70u-enchant php70u-xml php70u-pspell php70u-pear php70u-enchant php70u-xml php70u-pspell php70u-fpm php-70fpm

  7. Once PHP has been installed we need to configure it for NGINX. To do this you will need to open /etc/php-fpm.d/www.conf:
    Find the line that says “listen =” and change the line to look like the entry below.

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

    Next find the lines “listen.owner” and “” and change them to match the entry below.

    listen.owner = nginx = nginx

    Finally find the lines with “user” and “group” and change them to match the entry below.

    user = nginx group = nginx

    Now that we have configured PHP-FPM we can start it by running the command below.

    systemctl start php-fpm

  8. We now need to configure NGINX to use PHP to proccess PHP files using PHP-FPM. To do so you will need to open the file /etc/nginx. Once opened you will need to find the section that starts with “server {” and make it match the configuration below
    server {     listen       80;     server_name  domain_name_or_IP;      root   /usr/share/nginx/html;     index index.htm index.html index.php;      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;     } } 

Once you have made the changes you need to restart NGINX using the command below.

service nginx restart

<li> Now we need to test PHP to ensure its working, You can run the following command to place a PHP info page in your document root.

echo “<?php phpinfo();” > /usr/share/nginx/html/phpinfo.php

After running that command browse to https://yourservers_IP/phpinfo.php. You should receive a page showing your PHP configuration information.

  • To ensure your services start automatically on reboot we need to enable them in Systemd(The software that manages services by default on CentOS). Run the following commands.

    systemctl enable mariadb.service
    systemctl enable nginx
    systemctl enable php-fpm

  • Congratulations, You should now have a working LEMP stack.

    Learn more from our Cloud Server Hosting Product Guide.

    Experience full control over your server environment and deploy the best operating and management systems that fit your needs with our reliable Cloud VPS Hosting!

    Was this article helpful? Join the conversation!

    Shop the Black Friday Sale
    Hurry! These Deals Won't Last Long