This guide demonstrates how to install LEMP Stack on CentOS 7.

1. Installing NGNIX Web Server

The Nginx web server is the most popular web servers in the world. It’s active community and detailed documentation eventually makes it a great choice for hosting a website.

Install Nginx using ( Yellowdog Updater, Modified ) YUM :

The sudo (Super User DO) command allows you to run programs with the security privileges.

In order to display web pages to our site visitors, we are going to employ Nginx, a high-performance web server. To get the latest Nginx version, we’ll first install the EPEL repository, which contains additional software for the CentOS 7 operating system.

To add the CentOS 7 EPEL repository, run the following command:

sudo yum install epel-release

Now that the EPEL repository is installed on your server, install Nginx using the following yum command:

sudo yum update
sudo yum install nginx -y

Once the installation is completed, enable and start the Apache service.

sudo systemctl enable nginx
sudo systemctl start nginx

Configure Firewall

Enable Firewalld

systemctl enable firewalld

Start Firewalld

systemctl start firewalld

Check the Status of Firewalld

systemctl status firewalld

If your server is protected by the firewall and you haven’t opened the HTTP and HTTPS ports. Enable them with the following command.

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

To verify that the change was applied, you can run:

sudo firewall-cmd --permanent --list-all

You’ll see output like this:

Output
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: cockpit dhcpv6-client http https ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

You’ll need to reload the firewall configuration so the changes take effect:

sudo firewall-cmd --reload

Now you can verify Apache installation. Every process in Apache is managed with the systemctl command. Verify the installation of Apache with the following command.

sudo systemctl status nginx
nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor prese>
   Active: active (running) since Tue 2020-09-08 16:16:00 CST; 8min ago
 Main PID: 50194 (nginx)
    Tasks: 2 (limit: 5045)
   Memory: 9.4M
   CGroup: /system.slice/nginx.service
           ├─50194 nginx: master process /usr/sbin/nginx
           └─50195 nginx: worker process
Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ systemd[1]: Starting The nginx HTTP and>
Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ nginx[50190]: nginx: the configuration >
Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ nginx[50190]: nginx: configuration file>
Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ systemd[1]: nginx.service: Failed to pa>
Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ systemd[1]: Started The nginx HTTP and >
lines 1-15/15 (END)

You can now check which IP address is accessible, by running the following command:

curl -4 icanhazip.com

Type the IP address in your web browser and it will take you to Apache’s default landing page:

2. Install MySQL/MariaDB Database

The third layer of the LAMP stack is MySQL or MariaDB. Both are open-source database management systems used for storing and managing data on your website.

sudo yum install mariadb-server mariadb -y

When the installation is complete, we need to start MariaDB with the following command:

sudo systemctl start mariadb

After completing the installation, enable the MySQL service to auto-start on the system start. Also start service manually for the first time.

sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service

Then check the service current status using the following command:

sudo systemctl status mariadb.service
 mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-10-06 14:31:53 CST; 15min ago
 Main PID: 26834 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─26834 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─26997 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysq...

Oct 06 14:31:51 iZa2dc2w6sz7gbkh2kc8exZ mariadb-prepare-db-dir[26752]: MySQL ...
Oct 06 14:31:51 iZa2dc2w6sz7gbkh2kc8exZ mariadb-prepare-db-dir[26752]: Please...
Oct 06 14:31:51 iZa2dc2w6sz7gbkh2kc8exZ mariadb-prepare-db-dir[26752]: The la...
Oct 06 14:31:51 iZa2dc2w6sz7gbkh2kc8exZ mariadb-prepare-db-dir[26752]: You ca...
Oct 06 14:31:51 iZa2dc2w6sz7gbkh2kc8exZ mariadb-prepare-db-dir[26752]: http:/...
Oct 06 14:31:51 iZa2dc2w6sz7gbkh2kc8exZ mariadb-prepare-db-dir[26752]: Consid...
Oct 06 14:31:51 iZa2dc2w6sz7gbkh2kc8exZ mariadb-prepare-db-dir[26752]: https:...
Oct 06 14:31:51 iZa2dc2w6sz7gbkh2kc8exZ mysqld_safe[26834]: 201006 14:31:51 m...
Oct 06 14:31:51 iZa2dc2w6sz7gbkh2kc8exZ mysqld_safe[26834]: 201006 14:31:51 m...
Oct 06 14:31:53 iZa2dc2w6sz7gbkh2kc8exZ systemd[1]: Started MariaDB database ...
Hint: Some lines were ellipsized, use -l to show in full.

The MySQL installation has been completed. Now you can connect the MySQL server without any password but we recommend securing the MySQL installation. The MySQL packages provide mysql_secure_installation command to apply the security. Just run the below command on terminal:

sudo mysql_secure_installation

and follow the on-screen instructions. Below are the details which require user input.

  • Press y|Y for Yes, any other key for No: y
  • Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
  • New password: [ENTER STRONG PASSWORD HERE]
  • Re-enter new password: RE ENTER PASSWORD HERE
  • Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
  • Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
  • Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
  • Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
  • Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

5: Install PHP

We need to download and install an additional CentOS repository that contains the required packages for PHP v7.3. Run these commands one after another:

wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm

Enable php73 repository, which is disabled by default:

yum install yum-utils -y
yum-config-manager --enable remi-php73

After that, install the PHP package:

yum --enablerepo=remi,remi-php73 install php-fpm php-common

When asked for installation permission, simply press Y.

Now, install common PHP modules to make sure the service is working properly:

yum --enablerepo=remi,remi-php73 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

Configuring Nginx to work with PHP 7

The last thing we need to do is configure Nginx to work with PHP 7. Here’s how you do it:

Create a new Nginx configuration file by running nano text editor:

nano /etc/nginx/conf.d/default.conf

Then, insert this code:

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

IMPORTANT! Replace your_server_ip with your actual server IP.

Save the file by hitting CTRL + X (or CMD + X for Mac users). Next, restart Nginx so the changes can take effect:

systemctl restart nginx

Open PHP-FPM configuration:

nano /etc/php-fpm.d/www.conf

Find and change these lines:

user = apache to user = nginx

group = apache to group = nginx

listen.owner = nobody to listen.owner = nginx

listen.group = nobody to listen.group = nginx

After it’s done, add the following line under ;listen = 127.0.0.1:9000

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

Save the file by hitting CTRL + X. Lastly, start PHP-FPM and enable it on boot:

systemctl start php-fpm.service
systemctl enable php-fpm.service

6: Verify the LEMP setup

Create a new file with the name info.php

sudo nano /var/www/html/info.php

Paste the following code in the editor and save the file.

<?php
phpinfo();

Hit CTRL + X followed by Y and Enter to save and exit the file.

Now open your browser and point it to your external IP address of your server followed by info.php in the URL.

http://IP_Address/info.php

You will see the PHP information which indicates you have installed and configured NGNIX, MySQL/MariaDB and PHP on your CentOS 8 server.

Conclusion

Now that we have installed LEMP STACK, you can now host your website in the server. One of the popular thing you can do is install WordPress and design your own website or blog for your business.

To manage MySQL with GUI, we need to install phpMyAdmin. Operations such as the management of databases, tables, indexes, permissions, and so on are executed with the graphical user interface of phpMyAdmin.

Good luck and feel free to leave a comment if you have any query!