Setting Up a LAMP/LEMP Stack on a VPSSetting up LAMP and LEMP on CentOS: A Comprehensive Guide

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 -y

2. LAMP Stack Setup

2.1 Installing Apache

To install the Apache web server:

sudo yum install httpd -y

Start and enable the Apache service:

sudo systemctl start httpd
sudo systemctl enable httpd

2.2 Installing MySQL/MariaDB

Install MariaDB (a MySQL fork):

sudo yum install mariadb-server mariadb -y

Start and enable MariaDB:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Secure your MariaDB installation:

sudo mysql_secure_installation

This 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 -y

Restart Apache to integrate PHP:

sudo systemctl restart httpd

3. 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 -y

Install Nginx:

sudo yum install nginx -y

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

3.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 -y

Start and enable php-fpm:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Edit the PHP-FPM configuration to make it work with Nginx:

sudo vi /etc/php-fpm.d/www.conf

Find and change these lines:

user = nginx
group = nginx

Save and close the file.

Restart PHP-FPM:

sudo systemctl restart php-fpm

4. 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 --reload

5. Testing Your Setup


  1. Create a sample PHP file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Access the file through your web browser by navigating to:



  1. Modify Nginx’s default server block to process PHP:
sudo vi /etc/nginx/conf.d/default.conf

Add the following inside the server block:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  1. Create a sample PHP file:
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.php

Restart Nginx:

sudo systemctl restart nginx

Access the file through your web browser:


This 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

Your email address will not be published. Required fields are marked *