Wednesday, 5 February 2025

How to Set Up a LAMP or LEMP Stack on a Linux VPS: A Step-by-Step Guide

In today's digital world, having a reliable web hosting environment is essential for businesses, developers, and IT professionals. Two of the most popular server setups for hosting web applications on a Linux VPS are the LAMP (Linux, Apache, MySQL, PHP) and LEMP (Linux, Nginx, MySQL, PHP) stacks.

Both stacks provide a powerful and scalable foundation for hosting websites, applications, and databases. However, choosing between them depends on performance needs, server resources, and personal preferences.

In this guide, we will walk you through setting up a LAMP or LEMP stack on a Linux VPS, helping you build a secure and efficient hosting environment. Whether you're using an Ubuntu, Debian, or CentOS VPS, this tutorial will ensure a smooth setup.

If you’re looking for a high-performance Linux VPS with full root access, check out our hosting solutions at 99RDP, AmazingRDP, and OliveVPS—we provide premium VPS and RDP services to meet your hosting needs.

Prerequisites

Before we dive into the installation process, ensure you have the following:

1. A Linux VPS

You need a Linux-based VPS running Ubuntu, Debian, or CentOS. If you don’t have a VPS yet, you can check out 99RDP, AmazingRDP, or OliveVPS for high-performance hosting solutions.

2. SSH Access & Terminal Knowledge

You must have SSH access to your VPS and be comfortable using the command line. You can connect to your server via SSH using:

ssh root@your-server-ip

Replace your-server-ip with your actual VPS IP address.

3. Sudo or Root Privileges

You'll need administrative privileges to install and configure software. If you're logged in as a non-root user, prefix commands with sudo.

4. A Domain Name (Optional but Recommended)

If you plan to host a website, having a domain name makes it easier to access your server. You can use services like Cloudflare or Namecheap to manage your DNS settings.

Choosing Between LAMP and LEMP

Before proceeding, let's quickly compare LAMP and LEMP so you can choose the best stack for your needs.

Feature LAMP (Apache) LEMP (Nginx)
Web Server Apache Nginx
Database Server MySQL / MariaDB MySQL / MariaDB
PHP Integration Uses mod_php Uses PHP-FPM
Performance Good for dynamic sites Faster for high-traffic sites
Memory Usage Higher Lower
Best For Traditional CMS (WordPress, Joomla, Drupal) High-performance apps, reverse proxy setups
  • Choose LAMP if you need an easy-to-configure, widely supported setup.
  • Choose LEMP if you want better performance and lower memory usage, especially for high-traffic websites.

Once you've decided on your preferred stack, follow the corresponding installation steps below.

Setting Up a LAMP Stack on a Linux VPS

Step 1: Update System Packages

Before installing any software, it's always a good idea to update your system packages to ensure you have the latest versions.

For Ubuntu/Debian, run:

sudo apt update && sudo apt upgrade -y

For CentOS, run:

sudo yum update -y

Step 2: Install Apache Web Server

Apache is the most widely used web server and powers a large percentage of websites on the internet. To install Apache, use the following command:

For Ubuntu/Debian:

sudo apt install apache2 -y

For CentOS:

sudo yum install httpd -y

Once installed, start and enable Apache:

sudo systemctl start apache2
sudo systemctl enable apache2

To verify Apache is running, use:

sudo systemctl status apache2

Now, open your browser and enter your server's IP address (http://your-server-ip). If Apache is running correctly, you should see the default Apache welcome page.

Step 3: Install MySQL (or MariaDB) Database Server

MySQL (or its fork, MariaDB) is the database component of the LAMP stack.

To install MySQL on Ubuntu/Debian, run:

sudo apt install mysql-server -y

On CentOS, install MariaDB instead:

sudo yum install mariadb-server -y

Start and enable the database server:

sudo systemctl start mysql
sudo systemctl enable mysql

Secure the MySQL installation with:

sudo mysql_secure_installation

This will guide you through steps like setting a root password and removing anonymous users.

Step 4: Install PHP and Configure It for Apache

PHP is required to run dynamic websites and applications like WordPress. Install it with:

For Ubuntu/Debian:

sudo apt install php libapache2-mod-php php-mysql -y

For CentOS:

sudo yum install php php-mysql -y

Restart Apache to apply the changes:

sudo systemctl restart apache2

To test PHP, create an info.php file in /var/www/html/:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now, open http://your-server-ip/info.php in your browser. If PHP is working correctly, you’ll see a page displaying PHP configuration details.

Step 5: Configure Firewall and Test Setup

If you have a firewall enabled, allow Apache traffic:

sudo ufw allow "Apache Full"

For CentOS users with firewalld:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Now, restart Apache one final time:

sudo systemctl restart apache2

🎉 Congratulations! Your LAMP stack is now up and running! 🎉

Setting Up a LEMP Stack on a Linux VPS

The LEMP stack (Linux, Nginx, MySQL, PHP) is a popular alternative to LAMP, offering better performance and lower memory usage. Instead of Apache, LEMP uses Nginx, which is faster and more efficient for handling multiple requests.

Step 1: Update System Packages

As with the LAMP stack, start by updating your system packages to ensure everything is up to date.

For Ubuntu/Debian:

sudo apt update && sudo apt upgrade -y

For CentOS:

sudo yum update -y

Step 2: Install Nginx Web Server

Nginx is a lightweight and high-performance web server that handles static and dynamic content more efficiently than Apache.

To install Nginx on Ubuntu/Debian:

sudo apt install nginx -y

On CentOS:

sudo yum install nginx -y

After installation, start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

To verify that Nginx is running, check its status:

sudo systemctl status nginx

Now, open your browser and visit http://your-server-ip. You should see the default Nginx welcome page, confirming that the web server is working correctly.

Step 3: Install MySQL (or MariaDB) Database Server

Just like in the LAMP stack, you'll need a database server to store your website’s data.

To install MySQL on Ubuntu/Debian:

sudo apt install mysql-server -y

For CentOS (MariaDB is the default choice):

sudo yum install mariadb-server -y

Start and enable the database server:

sudo systemctl start mysql
sudo systemctl enable mysql

Secure the MySQL installation with:

sudo mysql_secure_installation

During this process, you'll be prompted to:

  • Set a root password.
  • Remove anonymous users.
  • Disable remote root login.
  • Remove test databases.

Step 4: Install PHP and Configure It for Nginx

Since Nginx does not process PHP files directly (unlike Apache), you need to install PHP-FPM (FastCGI Process Manager) to handle PHP requests.

For Ubuntu/Debian:

sudo apt install php-fpm php-mysql -y

For CentOS:

sudo yum install php php-fpm php-mysql -y

Start and enable PHP-FPM:

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

Now, configure Nginx to process PHP files. Open the default Nginx server block configuration file:

sudo nano /etc/nginx/sites-available/default  # (Ubuntu/Debian)

Or for CentOS:

sudo nano /etc/nginx/nginx.conf

Find the section that starts with location ~ \.php$ { and modify it as follows:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Save and exit the file (CTRL + X, then Y and Enter).

Test the Nginx configuration:

sudo nginx -t

If there are no errors, restart Nginx:

sudo systemctl restart nginx

Step 5: Configure Firewall and Test Setup

If you're using UFW (on Ubuntu/Debian), allow Nginx through the firewall:

sudo ufw allow "Nginx Full"

For CentOS users with firewalld:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Step 6: Test PHP with Nginx

To verify that PHP is working, create a test file:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now, open your browser and visit:

http://your-server-ip/info.php

If PHP is installed correctly, you'll see a page displaying PHP configuration details.

🎉 Congratulations! Your LEMP stack is now successfully set up! 🎉

Securing Your LAMP/LEMP Stack

Once your server is up and running, it’s crucial to secure it to prevent potential security threats. Here are some recommended steps:

1. Install an SSL Certificate (Let’s Encrypt)

An SSL certificate encrypts traffic between your server and users, securing sensitive data. Install Certbot to obtain a free SSL certificate:

For Ubuntu/Debian:

sudo apt install certbot python3-certbot-nginx -y

For CentOS:

sudo yum install certbot python3-certbot-nginx -y

Then, run the following command to issue an SSL certificate:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Replace yourdomain.com with your actual domain name. Certbot will configure SSL for Nginx automatically.

2. Secure MySQL

  • Ensure you have a strong MySQL root password.
  • Create separate users with limited privileges for your applications instead of using the root account.
  • Disable remote MySQL access unless necessary.

3. Harden Your Server

  • Disable unused services.
  • Keep your software updated (apt upgrade or yum update).
  • Use a firewall (ufw or firewalld).
  • Implement fail2ban to prevent brute-force attacks:
    sudo apt install fail2ban -y  # Ubuntu/Debian
    sudo yum install fail2ban -y  # CentOS
    

Deploying a Website on Your LAMP/LEMP Stack

Once your stack is ready, you can deploy a website or install a CMS like WordPress. Here’s how:

1. Upload Website Files

You can upload your website files to /var/www/html/ using SFTP or SCP.

Example SFTP command:

sftp user@your-server-ip
put localfile.html /var/www/html/

2. Configure Virtual Hosts (Apache) or Server Blocks (Nginx)

For Apache, create a new virtual host file in /etc/apache2/sites-available/.
For Nginx, configure a new server block in /etc/nginx/sites-available/.

3. Restart the Web Server

After making changes, restart Apache or Nginx:

sudo systemctl restart apache2  # Apache
sudo systemctl restart nginx  # Nginx

Conclusion

Setting up a LAMP or LEMP stack on a Linux VPS is a crucial step for hosting web applications. This guide walked you through choosing the right stack, installing the required components, securing the server, and deploying a website.

For a high-performance VPS with full root access, check out 99RDP, AmazingRDP, or OliveVPS to get the best hosting experience.

🚀 Now that your server is set up, the next step is to install WordPress, Laravel, or any other application on your LAMP/LEMP stack! Let us know if you need further guidance.

FAQs

Q1: What is the difference between LAMP and LEMP?

LAMP uses Apache as the web server, whereas LEMP uses Nginx. Nginx is more efficient in handling multiple concurrent requests and is better suited for high-traffic websites, while Apache is easier to configure and widely supported by traditional CMS platforms.

Q2: Can I install both Apache and Nginx on the same VPS?

Yes, but it's not recommended unless you’re setting up Nginx as a reverse proxy for Apache. Running both simultaneously can cause port conflicts unless properly configured.

Q3: Which one is better for WordPress—LAMP or LEMP?

Both work well, but LEMP (Nginx) is preferred for better performance, especially for high-traffic WordPress sites. However, if you need compatibility with .htaccess files, LAMP (Apache) is the better choice.

Q4: How do I check if Apache or Nginx is running?

To check Apache status:

sudo systemctl status apache2  # Ubuntu/Debian
sudo systemctl status httpd    # CentOS

To check Nginx status:

sudo systemctl status nginx

Q5: What is MySQL, and why do I need it?

MySQL (or MariaDB) is the database management system that stores and manages your website’s data. It’s essential for dynamic websites like WordPress, Joomla, or any PHP-based application that requires data storage.

Q6: How do I restart my LAMP or LEMP services?

For LAMP (Apache, MySQL, PHP):

sudo systemctl restart apache2
sudo systemctl restart mysql
sudo systemctl restart php7.4-fpm  # (Version may vary)

For LEMP (Nginx, MySQL, PHP-FPM):

sudo systemctl restart nginx
sudo systemctl restart mysql
sudo systemctl restart php7.4-fpm  # (Version may vary)

Q7: How do I migrate from LAMP to LEMP?

  1. Backup your website and database.
  2. Uninstall Apache:
    sudo apt remove apache2 -y
    
  3. Install Nginx and PHP-FPM (sudo apt install nginx php-fpm -y).
  4. Reconfigure virtual hosts to use Nginx server blocks.
  5. Restart services and test.

Q8: How do I secure my LAMP or LEMP stack?

  • Install SSL using Let’s Encrypt:
    sudo certbot --nginx -d yourdomain.com
    
  • Use firewalls (ufw for Ubuntu, firewalld for CentOS).
  • Secure MySQL with a strong password.
  • Regularly update your system (sudo apt update && sudo apt upgrade -y).

Q9: What should I do if my PHP files don’t execute?

  1. Make sure PHP is installed.
  2. Check if Apache (mod-php) or Nginx (PHP-FPM) is configured properly.
  3. Restart services (sudo systemctl restart apache2 or sudo systemctl restart nginx).
  4. Ensure PHP files have the correct permissions:
    sudo chown -R www-data:www-data /var/www/html/
    sudo chmod -R 755 /var/www/html/
    

Q10: Where can I get a reliable Linux VPS for my LAMP/LEMP stack?

You can get high-performance VPS solutions from:

0 comments:

Post a Comment

Popular Posts

Blog Archive