In this article, we will have explained the necessary steps to install and configure LEMP Stack on CentOS 8. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.
LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of Linux, Apache, MySQL, and PHP. Due to its modular nature, the components can easily be swapped out. With LEMP, Apache is replaced with the lightweight yet powerful Nginx.
Install LEMP Stack on CentOS
Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.
sudo dnf update
Step 2. Installing Nginx.
Starting with CentOS 8, the Nginx package is available in the default CentOS repositories. Installing Nginx on CentOS 8 is as simple using following command:
sudo dnf install nginx
Once the installation is complete, enable and start the Nginx service with:
sudo systemctl start nginx sudo systemctl enable nginx
To verify that the service is running, check its status:
sudo systemctl status nginx
The next step is to open the ports in the Firewall:
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
Step 3. Installing MariaDB.
Run the following command as root or user with sudo privileges to install MariaDB on CentOS 8:
sudo dnf install @mariadb
Once MariaDB server is installed, start and enable the service with:
sudo systemctl start mariadb.service sudo systemctl enable mariadb.service
MariaDB server package comes with a script called mysql_secure_installation performs several security-related operations, and sets the root password:
sudo mysql_secure_installation
Step 4. Installing PHP.
Add remi repository in CentOS so we can install latest PHP 7.3, typing following command:
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
The default PHP module is set to PHP 7.3. To install a newer PHP release, enable the appropriate version:
sudo dnf module reset php sudo dnf module enable php:remi-7.3
Then, that we have Remi repository enabled, we can install PHP FPM and several most common PHP modules with:
sudo dnf install php php-opcache php-gd php-curl php-mysqlnd
PHP uses FPM is installed as a dependency and used as FastCGI server. Start the FPM service and enable it to automatically start on boot:
sudo systemctl enable --now php-fpm
Now, open PHP-FPM configuration:
nano /etc/php-fpm.d/www.conf
Find and replace 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 And, lastly, under ;listen = 127.0.0.1:9000 add this line: listen = /var/run/php-fpm/php-fpm.sock
Step 5. Set up Nginx configuration file.
Create directory inside var/www/html named idroot.us (you can use your domain name):
sudo mkdir -p /var/www/html/linuxtips.us
Configuration files for the website are stored inside /etc/nginx/conf.d directory so you need to create configuration file inside this directory named idroot.us.conf (you can use your domain name). Then enter following code inside that file by replacing idroot.us with your domain name:
nano /etc/nginx/conf.d/example.com.conf
server { listen 80 default_server; listen [::]:80 default_server; server_name linuxtips.us www.linuxtips.us; root /var/www/html/linuxtips.us; index index.php; location / { try_files $uri $uri/ =404; } location ~* \.php$ { fastcgi_pass unix:/run/php/php7.3-fpm.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; } }
Do not forget to restart the Nginx service for the changes to take effect:
sudo systemctl restart php-fpm sudo nginx -s reload
That’s all you need to do to Install LEMP Stack on CentOS 8. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.