
CentOS / Fedora / AmaLinux are a popular choices for web servers due to their robustness and stability and familiarity with RHEL. If you’re looking to set up a web server on one of these distros, you might have come across two common setups: LAMP and LEMP. These acronyms refer to combinations of software used to serve websites and web applications.
- LAMP: Linux, Apache, MySQL/MariaDB, PHP
- LEMP: Linux, Nginx (pronounced “Engine-X”), MySQL/MariaDB, PHP
In this guide, we’ll walk you through setting up both the LAMP and LEMP stacks on CentOS, but these instructions should work on most RHEL derivatives. Let’s begin!
1. System Update
Before installing any software, it’s essential to ensure that your CentOS system is up-to-date.
sudo yum update -y2. LAMP Stack Setup
2.1 Installing Apache
To install the Apache web server:
sudo yum install httpd -yStart and enable the Apache service:
sudo systemctl start httpd
sudo systemctl enable httpd2.2 Installing MySQL/MariaDB
Install MariaDB (a MySQL fork):
sudo yum install mariadb-server mariadb -yStart and enable MariaDB:
sudo systemctl start mariadb
sudo systemctl enable mariadbSecure your MariaDB installation:
sudo mysql_secure_installationThis script will help you set a root password, remove anonymous users, disable remote root login, and remove the test database.
2.3 Installing PHP
Install PHP and some common extensions:
sudo yum install php php-mysql php-fpm -yRestart Apache to integrate PHP:
sudo systemctl restart httpd3. LEMP Stack Setup
3.1 Installing Nginx
First, you need to add the EPEL repository, which provides additional packages for CentOS:
sudo yum install epel-release -yInstall Nginx:
sudo yum install nginx -yStart and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx3.2 MySQL/MariaDB Installation
The installation process is the same as described in the LAMP setup (see section 2.2).
3.3 Installing PHP for Nginx
Install PHP and its necessary modules:
sudo yum install php-fpm php-mysql -yStart and enable php-fpm:
sudo systemctl start php-fpm
sudo systemctl enable php-fpmEdit the PHP-FPM configuration to make it work with Nginx:
sudo vi /etc/php-fpm.d/www.confFind and change these lines:
user = nginx
group = nginxSave and close the file.
Restart PHP-FPM:
sudo systemctl restart php-fpm4. Configuring Firewalls
For both LAMP and LEMP, you should adjust the firewall to allow web traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload5. Testing Your Setup
For LAMP:
- Create a sample PHP file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.phpAccess the file through your web browser by navigating to:
http://your_server_IP/info.phpFor LEMP:
- Modify Nginx’s default server block to process PHP:
sudo vi /etc/nginx/conf.d/default.confAdd the following inside the server block:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}- Create a sample PHP file:
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.phpRestart Nginx:
sudo systemctl restart nginxAccess the file through your web browser:
http://your_server_IP/info.phpThis guide has walked you through setting up both LAMP and LEMP on CentOS. The steps provided here will give you a foundation to host websites and web applications. Make sure to further configure and optimize your servers based on specific needs and always ensure security best practices.

Leave a Reply