How To Install LEMP on CentOS 7

Install LEMP on CentOS 7

For those of you who didn’t know, A LEMP software stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym that represents the Linux operating system, with the Nginx webserver (which replaces the Apache component of a LAMP stack). The site data is stored in a MySQL database (using MariaDB), and dynamic content is processed by PHP.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple. I will show you through the step by step installation of LEMP (Linux, Nginx, MariaDB, and PHP) on the CentOS 7 server.

Install LEMP on CentOS 7

Step 1. First of all, make sure that all packages are up to date.

yum -y update

Step 2. Installing and Configuring NGINX in CentOS 7.

Nginx is not yet available in CentOS 7 official repositories, so we have to add/install Nginx yum repository by issuing the command below:

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx

Start Nginx and add it to automatically start on your system start-up using:

systemctl restart nginx
systemctl enable nginx

You can verify that Nginx is really running by opening your favorite web browser and entering the URL http://your-server’s-address, if it is installed, then you will see this:

Install LEMP on CentOS 7
Nginx Default Page

Step 3. Configure the Nginx web server.

Let’s say you have a domain mydomain.com and you like to use it to host a PHP based web application in /var/www/mydomain.com like WordPress, Joomla. To set-up Nginx serve requests for mydomain.com, and serve the PHP scripts in /var/www/mydomain.com you would have to create a server block in /etc/nginx/conf.d/mydomain.com.conf which would look something like:

### nano /etc/nginx/conf.d/mydomain.com.conf

server {
    server_name mydomain.com;
    listen 80;
    root /var/www/mydomain.com;
    access_log /var/log/nginx/mydomain.com-access.log;
    error_log /var/log/nginx/mydomain.com-error.log;
    index index.php;

    location / {
        try_files  $uri $uri/ /index.php?$args;
    }

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }
    location ~ /\.ht {
        deny  all;
    }
    location ~ \.php {
        try_files $uri = 404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Test and re-start Nginx using:

nginx -t
systemctl restart nginx

Step 4. Installing and Configuring PHP on CentOS 7.

Install PHP on CentOS 7 with the following command to begin the install:

yum install php-fpm php-mysql php-mcrypt

You may want to install some other PHP extensions required by your applications. The following is a list of the available PHP modules:

php-bcmath          : A module for PHP applications for using the bcmath library
php-cli             : Command-line interface for PHP
php-common          : Common files for PHP
php-dba             : A database abstraction layer module for PHP applications
php-devel           : Files needed for building PHP extensions
php-embedded        : PHP library for embedding in applications
php-enchant         : Enchant spelling extension for PHP applications
php-fpm             : PHP FastCGI Process Manager
php-gd              : A module for PHP applications for using the gd graphics library
php-intl            : Internationalization extension for PHP applications
php-ldap            : A module for PHP applications that use LDAP
php-mbstring        : A module for PHP applications which need multi-byte string handling
php-mysql           : A module for PHP applications that use MySQL databases
php-mysqlnd         : A module for PHP applications that use MySQL databases
php-odbc            : A module for PHP applications that use ODBC databases
php-pdo             : A database access abstraction module for PHP applications
php-pear.noarch     : PHP Extension and Application Repository framework
php-pecl-memcache   : Extension to work with the Memcached caching daemon
php-pgsql           : A PostgreSQL database module for PHP
php-process         : Modules for PHP script using system process interfaces
php-pspell          : A module for PHP applications for using pspell interfaces
php-recode          : A module for PHP applications for using the recode library
php-snmp            : A module for PHP applications that query SNMP-managed devices
php-soap            : A module for PHP applications that use the SOAP protocol
php-xml             : A module for PHP applications which use XML
php-xmlrpc          : A module for PHP applications which use the XML-RPC protocol

Restart Nginx so all the changes take effect:

systemctl restart nginx
systemctl restart php-fpm
systemctl enable php-fpm

To test PHP, create a test file named info.php with the content below. Save the file, then browse to it to see if PHP is working:

nano /var/www/html/info.php
<?php
phpinfo();
?>

Step 5. Installing and Configuring MariaDB on CentOS 7.

Install MariaDB with the following command to begin the install:

yum install mariadb mariadb-server mysql

After that add it to your system startup and start the MariaDB server using the following commands:

systemctl restart mariadb
systemctl status mariadb
systemctl enable mariadb

By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation script. you should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MySQL:

mysql_secure_installation

To log into MariaDB, use the following command (note that it’s the same command you would use to log into a MySQL database):

mysql -u root -p

Step 6. Configure IPTables or firewalls.

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

Congratulations! You have successfully installed the LEMP stack. Thanks for using this tutorial for installing LEMP (Linux Nginx, MariaDB, and PHP) in CentOS 7 system. For additional help or useful information, we recommend you to check the official Nginx, MariaDB, and PHP web site.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!