Installing the Linux, Nginx, MySQL, and PHP (LEMP) stack on CentOS 7 is described in detail here.
Introduction
In order to host dynamic websites and online applications, a LEMP software stack is a collection of open source software that is often deployed together. As a matter of fact, this phrase is an abbreviation for Linux and the ENginx web server (which replaces the Apache component of a LAMP stack). Using a MySQL database and PHP, the site’s dynamic content is created.
In this tutorial, we’ll learn how to setup an LEMP stack on a CentOS 7 server using PHP 7.4 and MariaDB as the database server. Using MariaDB as a replacement for the original MySQL server does not need any configuration or code modifications in your application, making it easy to transition to MariaDB.
Prerequisites
You should have a non-root user account set up on your server before beginning this course. When you’ve completed steps 1-4 in CentOS 7’s basic server setup, you will learn how to accomplish this
Step 1 – Getting Nginx up and running it
The high-performance web server Nginx will be used to show our website’s content. Installing the EPEL repository, which offers extra software for the CentOS 7 operating system, is the first step in getting the newest version of Nginx.
CentOS 7 EPEL repositories may be added by using the command:
sudo yum install epel-release
These tasks are performed as root since we used the sudo command. To make sure you have root rights, it will ask you for your usual user password. You’ll also be asked to accept the terms of the agreement, so hit Y.
Nginx can now be installed on your server thanks to the EPEL repository installed on your machine.
sudo yum install nginx
Once the installation is complete, start the Nginx service by entering the following command:
sudo systemctl start nginx
By accessing your server’s public IP address in your web browser immediately away, you can verify that everything happened as intended (if you do not already know your server’s public IP address, see the note under the following section for further information on how to find out what it is):
Open in a web browser:
http://server_domain_name_or_IP/
Because it’s there for testing and informative reasons, the CentOS 7 Nginx home page will appear. Something like this should be what you get:
hA successful web server installation is confirmed by the fact that you can see this page now.
When booting, use the following command to allow Nginx to start automatically.
sudo systemctl enable nginx
What Is The Public IP Address Of Your Server?
The public IP address of your server can be found in many different places. As a general rule, this is the SSH hostname you’ll use when connecting to your server.
There are many methods to do this from the command line. Your IP address can be retrieved using the iproute2 tools if you enter this command:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
A few lines of text will be retrieved using this method. Neither of these addresses is incorrect, however your machine may only be able to utilize one of them.
As an alternative, you may hire a third-party to tell you what it thinks about your server. If you want to know what your IP address is, you can simply query a certain server:
curl http://icanhazip.com
To access your server, no matter how you get your IP address, you can put it into your web browser’s address bar and go to your server.
Step 2 – Getting MariaDB up and running
It’s time to install MariaDB, a MySQL drop-in alternative, now that our web server is up and running. Branched from MySQL, MariaDB is a free and open-source alternative.
Our program may be acquired and installed using yum again. To help us get our components to interact with one other, we’ll also install some more auxiliary packages:
sudo yum install mariadb-server mariadb
MariaDB has to be started when the installation is complete using the following command:
sudo systemctl start mariadb
It is time to execute a security script that removes certain risky defaults and restricts access to our MariaDB database now that it is up and running. Run the interactive script to get it started:
sudo mysql_secure_installation
To proceed, you’ll be prompted to provide your MariaDB root password. If you’ve just installed MariaDB, you’re probably going to be without one, so simply hit enter to clear it. After that, you’ll be prompted to choose a root password. As long as you comply with the directions, you’re good to go!
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 authorisation. Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success!
To accept the default settings for the remaining questions, just press “ENTER” at each prompt. These new rules will be loaded so that MySQL quickly recognizes the modifications we’ve made, and this will eliminate some example users and databases.
Make sure MariaDB is not started when the system boots up. You can accomplish this by using the following command:
sudo systemctl enable mariadb
We can now proceed with setting up your database system.
Step 3 – Creating a PHP server
In order to show dynamic material, we’ll be using PHP, a component of our setup. With its scripting capabilities and the ability to access our MySQL databases, it can process data and pass it on to our web server for display.
For this reason, we’ll need to install a third-party package repository on your CentOS 7 server in order to download PHP 7+ and have it installed on your CentOS 7 server. Remi is a well-known CentOS package repository that provides the most recent PHP versions.
To set up the Remi repository on CentOS 7, follow these steps:
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
The repository holding your desired version of PHP will need to be enabled when the installation is complete. Run the following command to see which Remi releases of PHP 7+ are now available:
yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64
You should expect to see something like this:
Output Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * remi-safe: mirrors.ukfast.co.uk Available Packages php70.x86_64 2.0-1.el7.remi remi-safe php71.x86_64 2.0-1.el7.remi remi-safe php72.x86_64 2.0-1.el7.remi remi-safe php73.x86_64 2.0-1.el7.remi remi-safe php74.x86_64 1.0-3.el7.remi remi-safe php80.x86_64 1.0-3.el7.remi
In this tutorial, we’ll learn how to set up PHP 7.4, the most recent stable version. Run the following command to install the right Remi package for PHP 7.4:
sudo yum-config-manager --enable remi-php74
We can now continue with the installation of PHP using yum as normal. PHP 7.4 can now connect to MySQL databases with the help of the following Nginx command line configuration.
sudo yum install php php-mysqlnd php-fpm
Run the following command to verify that the version of PHP you selected is indeed available:
php –version
You should expect to see something like this:
OutputPHP 7.4.5 (cli) (built: Apr 14 2020 12:54:33) ( NTS )Copyright (c) The PHP GroupZend Engine v3.4.0, Copyright (c) Zend Technologies
Your system now has PHP installed and ready to use. Make a few changes to the default setup next. As an alternative to CentOS’s default text editor, vi, we’ll first install the more user-friendly nano:
sudo yum install nano
Make use of nano or another text editor to access the configuration file at /etc/php-fpm.d/www.conf:
sudo nano /etc/php-fpm.d/www.conf
Look for the directions for users and groups now. In nano, you can use CTRL+W to look for these words in the currently open file by searching with those phrases.
/etc/php-fpm.d/www.conf … ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache user chosen to provide access to the same directories as httpd user = apache ; RPM: Keep a group allowed to write in log dir. group = apache …
There is a noticeable difference between the apache user and group variables. These need be changed to nginx:
/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 …
The listen directive can then be found by searching for it. PHP-fpm is set up to listen on a specified port on a certain host by default. To increase server speed, we want to alter this configuration such that it listens on a local socket file. A new directive for listening should be added:
/etc/php-fpm.d/www.conf listen = /var/run/php-fpm/php-fpm.sock;
Our socket file is now owned by us, therefore we’ll need to alter the group and owner settings. Determine where to find the commands for listening on behalf of an individual, group, and mode. By default, these lines are omitted from the source code. Remove the ; symbol at the beginning of the line to uncomment them. After that, make nginx the new owner and group:
/etc/php-fpm.d/www.conf listen.owner = nginx listen.group = nginx listen.mode = 0660
Once you’ve finished editing, save and shut the file. In nano, hit CTRL + X, Y, and ENTER to make your selection.
To activate and start the php-fpm service, type:
sudo systemctl start php-fpm
Finally, your PHP environment is ready to go. The next step is to set up Nginx so that all requests for PHP scripts are sent to php-fpm for processing.
Step 4 – Setting up Nginx for PHP Page Processing
Now that we’ve installed all of the necessary components, we’re good to go. Nginx is already configured to utilize our PHP processor for dynamic content, so we simply need to tell it to.
Using a server block, we can construct a different configuration file for each hosted website in a dedicated directory in Nginx. Virtual hosts in Apache are a good analogy for this.
The default installation, on the other hand, leaves this directory completely untouched. You can override the nginx.conf file’s default PHP website block by creating a new file in the /etc directory, which will act as the default for this server’s PHP website.
Open the /etc/nginx/conf.d directory and create a new file:
sudo nano /etc/nginx/conf.d/default.conf
Make sure to change the server name directive with your server’s domain name or IP address before copying the following PHP server definition block into your configuration file:
/etc/nginx/conf.d/default.conf server { listen 80; server_name server_domain_or_IP; 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; } }
The file should be saved and closed after you’re done with it.
Nginx must be restarted to take effect.
sudo systemctl restart nginx
Everything on your web server has been set up now. We’ll test the PHP interaction with Nginx in the following stage.
Step 5 – Checking your web server’s PHP processing capabilities
Using php-fpm, we can now write a test PHP script to verify that Nginx is properly handling.php files.
Nginx’s default document root ownership will be changed so that our ordinary sudo user can create files there before we begin writing our script.
In order to make a modification to the default Nginx document root’s ownership, run the following command as the tommy user and group, replacing the highlighted username and group.
sudo chown -R tommy.tommy /usr/share/nginx/html/
A test PHP page will now be created to ensure that the web server functions as planned.
In the /usr/share/nginx/html directory, create a new PHP file named info.php.
nano /usr/share/nginx/html/info.php
The following PHP code will provide details about the server’s current PHP environment:
/usr/share/nginx/html/info.php <? phpinfo(); ?>
Save and close the file when you’re done.
Now we can check whether our web server can show PHP script-generated content appropriately. Access the /info.php page on your server by typing its hostname or IP address into your browser:
http://server host or IP/info.php
A page like this will appear:
The file you produced, which includes critical information about your PHP environment and your CentOS server, should be deleted once you have checked the required information about your PHP server on that page. Remove that file using rm:
rm /usr/share/nginx/html/info.php
If you ever need this file again, you can easily regenerate it.
Conclusion
In this article, you learned how to create a flexible basis for providing PHP websites and apps to your users by using Nginx as a web server and the most recent PHP release version available. You’ve configured Nginx to handle PHP requests using the php-fpm module, and you’ve also configured a MariaDB database to hold the data for your website.
Mу partner and I stumbled over here coming from
a different page and thought I should chеck
things out. I like whаt I sеe so now i am following you.
Look forward to finding out about your web page for a second
time.
Exactly what i was looking for. Thank You