How to setup the LAMP stack on Amazon Linux

22 feb 2024 4 min di lettura
How to setup the LAMP stack on Amazon Linux
Indice dei contenuti


In the world of web development, setting up a LAMP stack is a critical skill that every developer should possess. The LAMP stack, which stands for Linux, Apache, MySQL, and PHP, provides a powerful and versatile platform for developing and hosting web applications. This guide will walk you through the process of installing a LAMP stack on Amazon Linux 2, a popular choice for many developers due to its stability and integration with AWS services.


Before you begin, make sure you have:

  • An AWS account
  • A running instance of Amazon Linux 2
  • SSH access to your instance

Step 1: Update your system

First, connect to your Amazon Linux 2 instance via SSH. Once logged in, it is a good idea to update your system to the latest packages. Run the following command:

sudo yum update -y

Step 2: Install Apache

Apache is a widely used web server software that will serve your web application to users. To install Apache, run:

sudo yum install httpd -y

Once the installation is complete, we recommend starting the Apache service and enabling it to start on startup:

sudo systemctl start httpd
sudo systemctl enable httpd

You can verify that Apache is running by accessing your instance's public DNS or IP address in a web browser. You should see the Apache test page.

Step 3: Install MySQL (MariaDB)

Amazon Linux 2 uses MariaDB, a community-developed fork of MySQL, as the default database management system. To install MariaDB, use the following command:

sudo yum install mariadb-server -y

Similar to Apache, start and enable the MariaDB service:

sudo systemctl start mariadb
sudo systemctl enable mariadb

To be safe, run the mysql_secure_installation script:

sudo mysql_secure_installation

Follow the on-screen instructions to set up your MariaDB installation, including setting a root password and removing anonymous users.

Step 4: Install PHP

PHP is a server scripting language used for web development. To install PHP along with some common extensions, run:

sudo yum install php php-mysqlnd php-pdo php-gd php-mbstring -y

After installing PHP, you need to restart Apache to apply the changes:

sudo systemctl restart httpd

To test your PHP installation, create a test PHP file in your web root directory:

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

Go to http://<your-public-dns-or-ip>/phpinfo.php in a web browser. You should see the PHP information page.

Step 5: Protect your stack

Safety is paramount. Make sure your AWS security group allows traffic only on the necessary ports (80 for HTTP, 443 for HTTPS, and optionally 22 for SSH). Consider setting up a firewall with firewalld or iptables and always keep your software up to date.

Step 6: Create a virtual host (optional)

A virtual host allows you to serve multiple websites from a single Apache server. We will create a virtual host for an example website named

Create a directory for your website

First, create a directory to hold your website files. Replace with your actual domain name.

sudo mkdir -p /var/www/

Set permissions

Next, change the permissions to ensure that your website files are accessible to the Apache web server.

sudo chown -R apache:apache /var/www/
sudo chmod -R 755 /var/www

Create a sample page

Create a simple HTML file to test your virtual host configuration.

echo "<h1>Welcome to!</h1>" | sudo tee /var/www/

Create the Apache virtual host file

For Apache to serve this content, you need to create a virtual host file for

sudo vi /etc/httpd/conf.d/

Add the following configuration, adapting it to your domain name and paths:

<VirtualHost *:80>
 DocumentRoot /var/www/
 ErrorLog /var/log/httpd/
 CustomLog /var/log/httpd/ combined

Restart Apache

Apply the changes by restarting Apache.

sudo systemctl restart httpd

Your virtual host is now configured. Going to should display the example page we created.

Step 7: Protect yourself with Let's Encrypt

Securing your website with HTTPS is essential for security and SEO. Let's Encrypt provides free SSL certificates. Here's how to set one up for your new virtual host.

Install Certbot

Certbot is a tool that automates the process of obtaining and renewing Let's Encrypt SSL certificates.

sudo yum install certbot python3-certbot-apache -y

Run Certbot

Run Certbot to automatically obtain an SSL certificate and configure your virtual host to use it.

sudo certbot --apache -d -d

Follow the on-screen instructions. Certbot will modify the Apache configuration to use the SSL certificate and automatically reload the server.

Configuring automatic renewal

Let's Encrypt certificates are valid for 90 days. Certbot can renew them automatically. Try automatic renewal with:

sudo certbot renew --dry-run

If this command runs without errors, auto-renewal is set up correctly.


You have successfully installed a LAMP stack on Amazon Linux 2. This setup provides a solid foundation for hosting web applications. From here you can deploy your applications, explore more advanced configurations, and start developing with one of the most popular stacks in web development.

Remember, managing a web server and websites is an ongoing process. Update your software regularly, monitor your server performance and make sure your applications are secure. With these steps you are well on your way to successfully managing a robust and secure web presence.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.