Introduction:
The LEMP stack, composed of Linux, Nginx, MySQL (or MariaDB), and PHP, is a robust and versatile platform for hosting web applications and websites. In this guide, we will walk you through the process of installing a LEMP stack on a CentOS 8 server. By following these steps, you’ll have a solid foundation for hosting dynamic web content.
Prerequisites:
Before we begin, make sure you have the following:
- A CentOS 8 server with root or sudo access.
- A terminal or SSH client to connect to your server.
Step 1: Update the System
Start by logging into your CentOS 8 server and ensuring your system is up to date by running the following commands:
sudo dnf update
Step 2: Install Nginx
Nginx is a powerful web server that will serve as the front-end of our LEMP stack. Install it using the following command:
sudo dnf install nginx
After the installation, start the Nginx service and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
You can verify that Nginx is running without errors by checking its status:
sudo systemctl status nginx
Step 3: Install MariaDB (MySQL)
MariaDB is a compatible, open-source alternative to MySQL. Install it using the following command:
sudo dnf install mariadb-server
Once the installation is complete, start the MariaDB service and enable it to start on boot:
sudo systemctl start mariadb
sudo systemctl enable mariadb
Secure your MariaDB installation by running the following command and following the on-screen prompts:
sudo mysql_secure_installation
Step 4: Install PHP
PHP is a server-side scripting language used to generate dynamic web content. Install PHP and necessary extensions with the following command:
sudo dnf install php php-mysqlnd
Step 5: Configure Nginx to Use PHP
Create a new Nginx server block configuration for your website:
sudo nano /etc/nginx/conf.d/your-site.conf
Replace “your-site” with your desired site name. Inside the configuration file, add the following:
server {
listen 80;
server_name your-domain.com www.your-domain.com;
root /var/www/your-site;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass unix:/run/php-fpm/www.sock;
}
}
Save and close the file.
Next, create a symbolic link to enable the site:
sudo ln -s /etc/nginx/conf.d/your-site.conf /etc/nginx/conf.d/
Test the Nginx configuration:
sudo nginx -t
If there are no errors, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 6: Create a PHP Test File
To verify that PHP is working correctly, create a test PHP file in the website’s root directory:
sudo nano /var/www/your-site/info.php
Add the following PHP code:
<?php
phpinfo();
?>
Save and exit the file.
Step 7: Test Your LEMP Stack
Open a web browser and enter your server’s IP address or domain name in the address bar, followed by “/info.php” (e.g., http://your-domain.com/info.php). You should see a PHP info page displaying information about your PHP installation. This confirms that Nginx is correctly serving PHP files.
Conclusion:
Congratulations! You have successfully installed a LEMP stack on your CentOS 8 server. You now have a solid foundation for hosting web applications and websites. Be sure to remove the PHP info file once you’ve verified that PHP is working to enhance security.