LEMP Stack Setup on CentOS 8
The LEMP stack is one of many server stacks website hosting. It is an acronym for the technologies used in the stack. L for a Linux operating system; CentOS 8 in this case. E for NGINX; albeit it starting with an N, it is commonly pronounced as en-gin-ex. M is for MySQL. P is for PHP. This is an excellent stack for fully custom PHP websites and even PHP CMS options like WordPress.
Prerequisites
To follow along with this guide, it is assumed that you have access to CentOS 8 server and that it is properly configured. If you don’t have a CentOS 8 server, here is a $100 credit(Or two months) with DigitalOcean so you can get started: https://m.do.co/c/453e482edaba. They’re the cheapest reliable solution for only $5 per month.
Installing NGINX
“NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more.” -nginx.com. In the LEMP stack, NGINX is used to serve your website’s content.
Install the nginx package with the command:
sudo dnf install nginx
When prompted, confirm the installation by entering “y”.
After the installation has completed, start the server with the command:
sudo systemctl start nginx
To make sure that the NGINX server starts up when your CentOS 8 server reboots, run the command:
sudo systemctl enable nginx
If you use the firewalld package, you will need to whitelist the HTTP port(80) to allow connections. Run the command:
sudo firewall-cmd --permanent --add-service=http
You would then need to reload firewalld to accept the changes. Run the command:
sudo firewall-cmd --reload
NGINX should then be running as expected. To test that the installation is successful, visit your server’s IP in your web browser. You should see the NGINX welcome page.
You can create your NGINX configuration files at the directory: /etc/nginx/conf.d
By default, NGINX will serve content from /usr/share/nginx/html/. You can place your PHP files here for testing later.
Installing MariaDB
Almost any website will need a database. As aforementioned, we use MySQL through MariaDB.
To install the MySQL package, run the command:
sudo dnf install mariadb-server
Once the package has installed, start the MariaDB server with the command:
sudo systemctl start mariadb
To make sure that the MariaDB server starts up when your CentOS 8 server reboots, run the command:
sudo systemctl enable mariadb
To improve the security of your MariaDB server, it is highly recommended that you run the following security script. It will have you setup a username, password, and other settings related to the integrity of your server’s security. Run the command:
sudo mysql_secure_installation
After all of that is done, you can expect to be able to connect to the MariaDB server and execute your database operations.
Installing PHP
Because we’re using NGINX, we’ll need to install the php-fpm package. This is unlike the Apache where PHP is already embedded. We need PHP to process code for dynamic web-pages.
To install the php-fpm and php-mysql packages, run:
sudo dnf install php-fpm php-mysqlnd
Install the nano package so we can edit a configuration file with the command:
sudo dnf install nano
Now run this command to edit the php-fpm configuration file:
sudo nano /etc/php-fpm.d/www.conf
You should see two lines in the file that say “user = apache” and “group = apache”. Replace “apache” with “nginx” for both lines.
Now you can start the php-fpm service with the command:
sudo systemctl start php-fpm
The command will automatically edit your NGINX default configuration so it will support PHP processing. You will need to restart the NGINX server so it will process the changes. Run the command:
sudo systemctl restart nginx
PHP should then be running as expected.
Conclusion
You should now have a flexible foundation for your PHP based website, but using NGINX as your efficient web server and MariaDB as your MySQL database.