Nginx is an open-source, high-performance web server. It is known for its stability, very simple configuration, rich feature set, and low resource consumption.

Nginx lets you use it as a reverse proxy, load balancer, HTTP cache, and mail proxy.

In this tutorial we will show you how to install LEMP Stack on CentOS 8.

First let’s start by ensuring your system is up-to-date.

# dnf update

Installing Nginx.

# dnf install nginx

 Enable and start Nginx.

# systemctl start nginx
# systemctl enable nginx

Open your web browser and go to your server. http://server-ip/ you will see something like this.

Install MariaDB

MariaDB is a pretty good database manager. It is a MySQL fork and therefore compatible with it. It is one of the open-source values. Install MariaDB on CentOS 8 with the command

# dnf install mariadb-server

Enable and start MariaDB server.

# systemctl start mariadb
# systemctl enable mariadb

Now it is necessary to define a password for the root user. In addition to this, the installation must be secured.

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

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 authorisation.

Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

When you’re finished, log in to the MariaDB console.

# mysql -u root -p

Create a new database.

MariaDB [(none)]> CREATE DATABASE database_name;

Create a new user and grant them full privileges on the custom database

MariaDB [(none)]> GRANT ALL PRIVILEGES ON database_name.* TO 'db_user'@'localhost' IDENTIFIED BY 'password';

Flush the privileges to ensure that they are saved and available in the current session:

MariaDB [(none)]> FLUSH PRIVILEGES;

Install and configure PHP

# dnf install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring

Modify php-fpm configuration file to make it work with Nginx.

# nano /etc/php-fpm.d/www.conf
...
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
...

Save and close the file.

Start and enable the php-fpm service.

# systemctl start php-fpm
# systemctl enable php-fpm

Creating Virtual Host

By default, Nginx is configured to load all configuration files that ends with .conf from the/etc/nginx/conf.d/ directory.

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

server {
listen 80;
server_name example.com www.example.com;
root /home/example.com;
index index.php index.html index.htm;

access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;

location ~ \.(php|phar)(/.*)?$ {
fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-fpm;
}
}

Create virtual host home folder.

# mkdir -p /home/example.com

Nginx should be made the owner of that folder.

# chown -R nginx: /home/example.com

To test the Nginx configuration, run the following command.

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx service.

# systemctl restart nginx

Testing PHP Information

To test PHP with the web server, you’ll have to create a phpinfo file to the document root directory.

# echo "<?php phpinfo();" > /home/example.com/index.php

Now we can test our webserver. Go to your browser and access your server hostname. You should see your server’s PHP information.

This means PHP scripts can run properly with Nginx web server. You have successfully install LEMP.

Tagged: