LAMP Stack Setup Tutorial (Linux, Apache, MySQL, PHP)
LAMP Stack Overview Linux: The operating system, typically Ubuntu, CentOS, or Debian. Apache: The web server that handles HTTP requests. **MySQL: **A relational database management system for storing website data. PHP: A programming language used to create dynamic web pages. Step 1: Update Your System First, log in to your VPS and update your system’s package manager to ensure you have the latest packages and security patches: sudo apt update && sudo apt upgrade -y Step 2: Install Apache Apache is one of the most popular web servers. To install Apache, run the following command: sudo apt install apache2 -y Once the installation is complete, start the Apache service: sudo systemctl start apache2 Ensure Apache starts automatically on boot: sudo systemctl enable apache2 You can check if Apache is installed correctly by typing your VPS’s public IP address in the browser (e.g., http://your-vps-ip). You should see the default Apache welcome page. Step 3: Install MySQL Next, install MySQL for the database management. Run the following command: sudo apt install mysql-server -y Once installed, start the MySQL service: sudo systemctl start mysql Ensure MySQL starts automatically on boot: sudo systemctl enable mysql Now, run the following command to configure MySQL security: sudo mysql_secure_installation This command will prompt you to set the MySQL root password and choose other security options. Step 4: Install PHP Install PHP and PHP modules to ensure Apache can process PHP files: sudo apt install php libapache2-mod-php php-mysql -y This command installs PHP and the MySQL-PHP connection module. Step 5: Test PHP Configuration To verify that PHP is working correctly with Apache, create a test file. First, create a file called info.php in the Apache root directory /var/www/html/: sudo nano /var/www/html/info.php Add the following code to the file: Save and close the file. Now, open your browser and visit http://your-vps-ip/info.php. You should see a PHP configuration page, confirming that PHP is successfully installed and working with Apache. Step 6: Configure Firewall If you have a firewall enabled on your VPS, ensure HTTP and HTTPS traffic is allowed. Run the following command: sudo ufw allow 'Apache Full' This will allow both HTTP and HTTPS traffic through the firewall. If you're using another type of firewall tool, make sure to configure it accordingly. Step 7: Set Up Virtual Hosts (Optional) If you plan to host multiple websites or domains, you can set up virtual hosts. If you want to save deployment costs, it is recommended to use a virtual host that charges by the hour, such as LightNode, Vultr First, create a new configuration file: sudo nano /etc/apache2/sites-available/yourdomain.com.conf Add the following configuration to the file: ServerAdmin webmaster@yourdomain.com ServerName yourdomain.com DocumentRoot /var/www/yourdomain.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Replace yourdomain.com with your actual domain name, and /var/www/yourdomain.com/public_html with the directory where your website will be stored. Next, create the directory and enable the site: sudo mkdir -p /var/www/yourdomain.com/public_html sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html sudo a2ensite yourdomain.com.conf sudo systemctl reload apache2 If you’re using a domain name, ensure it points to your VPS’s public IP address. Step 8: Test the Website Now, you can upload your website files to the /var/www/yourdomain.com/public_html directory. In your browser, navigate to your domain (e.g., http://yourdomain.com) to view your website. Step 9: Install SSL (Optional) For added security, you can set up HTTPS on your site by using a free SSL certificate from Let's Encrypt. First, install Certbot: sudo apt install certbot python3-certbot-apache -y Then, run the following command to obtain and install an SSL certificate: sudo certbot --apache Certbot will automatically configure HTTPS for your domain. Step 10: Regular Updates It’s important to regularly update your LAMP stack to keep the system secure. You can update your system and installed packages using: sudo apt update && sudo apt upgrade -y Summary You’ve now successfully set up a basic LAMP stack! You can now host dynamic PHP websites on your server with MySQL databases. Here’s a recap of what you’ve done: Installed Apache as the web server. Installed MySQL as the database management system. Installed PHP and verified it works with Apache. Configured firewall to allow web traffic. Optionally, set up virtual hosts and SSL.

LAMP Stack Overview
Linux: The operating system, typically Ubuntu, CentOS, or Debian.
Apache: The web server that handles HTTP requests.
**MySQL: **A relational database management system for storing website data.
PHP: A programming language used to create dynamic web pages.
Step 1: Update Your System
First, log in to your VPS and update your system’s package manager to ensure you have the latest packages and security patches:
sudo apt update && sudo apt upgrade -y
Step 2: Install Apache
Apache is one of the most popular web servers. To install Apache, run the following command:
sudo apt install apache2 -y
Once the installation is complete, start the Apache service:
sudo systemctl start apache2
Ensure Apache starts automatically on boot:
sudo systemctl enable apache2
You can check if Apache is installed correctly by typing your VPS’s public IP address in the browser (e.g., http://your-vps-ip). You should see the default Apache welcome page.
Step 3: Install MySQL
Next, install MySQL for the database management. Run the following command:
sudo apt install mysql-server -y
Once installed, start the MySQL service:
sudo systemctl start mysql
Ensure MySQL starts automatically on boot:
sudo systemctl enable mysql
Now, run the following command to configure MySQL security:
sudo mysql_secure_installation
This command will prompt you to set the MySQL root password and choose other security options.
Step 4: Install PHP
Install PHP and PHP modules to ensure Apache can process PHP files:
sudo apt install php libapache2-mod-php php-mysql -y
This command installs PHP and the MySQL-PHP connection module.
Step 5: Test PHP Configuration
To verify that PHP is working correctly with Apache, create a test file. First, create a file called info.php in the Apache root directory /var/www/html/:
sudo nano /var/www/html/info.php
Add the following code to the file:
phpinfo();
?>
Save and close the file. Now, open your browser and visit http://your-vps-ip/info.php. You should see a PHP configuration page, confirming that PHP is successfully installed and working with Apache.
Step 6: Configure Firewall
If you have a firewall enabled on your VPS, ensure HTTP and HTTPS traffic is allowed. Run the following command:
sudo ufw allow 'Apache Full'
This will allow both HTTP and HTTPS traffic through the firewall. If you're using another type of firewall tool, make sure to configure it accordingly.
Step 7: Set Up Virtual Hosts (Optional)
If you plan to host multiple websites or domains, you can set up virtual hosts. If you want to save deployment costs, it is recommended to use a virtual host that charges by the hour, such as LightNode, Vultr
First, create a new configuration file:
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
Add the following configuration to the file:
ServerAdmin webmaster@yourdomain.com
ServerName yourdomain.com
DocumentRoot /var/www/yourdomain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Replace yourdomain.com with your actual domain name, and /var/www/yourdomain.com/public_html with the directory where your website will be stored.
Next, create the directory and enable the site:
sudo mkdir -p /var/www/yourdomain.com/public_html
sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html
sudo a2ensite yourdomain.com.conf
sudo systemctl reload apache2
If you’re using a domain name, ensure it points to your VPS’s public IP address.
Step 8: Test the Website
Now, you can upload your website files to the /var/www/yourdomain.com/public_html directory. In your browser, navigate to your domain (e.g., http://yourdomain.com) to view your website.
Step 9: Install SSL (Optional)
For added security, you can set up HTTPS on your site by using a free SSL certificate from Let's Encrypt. First, install Certbot:
sudo apt install certbot python3-certbot-apache -y
Then, run the following command to obtain and install an SSL certificate:
sudo certbot --apache
Certbot will automatically configure HTTPS for your domain.
Step 10: Regular Updates
It’s important to regularly update your LAMP stack to keep the system secure. You can update your system and installed packages using:
sudo apt update && sudo apt upgrade -y
Summary
You’ve now successfully set up a basic LAMP stack! You can now host dynamic PHP websites on your server with MySQL databases. Here’s a recap of what you’ve done:
Installed Apache as the web server.
Installed MySQL as the database management system.
Installed PHP and verified it works with Apache.
Configured firewall to allow web traffic.
Optionally, set up virtual hosts and SSL.