In the world of web development, setting up a LAMP stack is a fundamental 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.
Prerequisites
Before beginning, ensure 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’s a good practice 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
After the installation is complete, you’ll want to start the Apache service and enable it to start on boot:
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 its 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
For security, run the mysql_secure_installation script:
sudo mysql_secure_installation
Follow the on-screen prompts to configure 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 the web root directory:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
Navigate to http://<your-public-dns-or-ip>/phpinfo.php in a web browser. You should see the PHP information page.
Step 5: Secure Your Stack
Security is paramount. Ensure your AWS security group allows traffic only on 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’ll create a virtual host for a sample website named example.com.
- Create a Directory for Your Website
- Set Permissions
Next, adjust the permissions to ensure that your website’s files are accessible to the Apache web server.
sudo chown -R apache:apache /var/www/example.com/public_html
sudo chmod -R 755 /var/www
- Create a Sample Page
Create a simple HTML file to test the virtual host configuration.
echo "<h1>Welcome to example.com!</h1>" | sudo tee /var/www/example.com/public_html/index.html
- Create Apache Virtual Host File
For Apache to serve this content, you need to create a virtual host file for example.com.
sudo vi /etc/httpd/conf.d/example.com.conf
Add the following configuration, adjusting for your domain name and paths:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined </VirtualHost>
- Restart Apache
Apply the changes by restarting Apache.
sudo systemctl restart httpd
Your virtual host is now set up. Accessing http://example.com should display the sample page we created.
First, create a directory to hold your website’s files. Replace example.com with your actual domain name.
sudo mkdir -p /var/www/example.com/public_html
Step 7: Secure with Let’s Encrypt
Securing your website with HTTPS is essential for safety and SEO. Let’s Encrypt provides free SSL certificates. Here’s how to set one up for your new virtual host.
- Install Certbot
- Run Certbot
Execute Certbot to automatically obtain an SSL certificate and configure your virtual host to use it.
sudo certbot --apache -d example.com -d www.example.com
Follow the on-screen instructions. Certbot will modify the Apache configuration to use the SSL certificate and reload the server automatically.
- Auto-Renewal Setup
Let’s Encrypt certificates are valid for 90 days. Certbot can automatically renew them. Test automatic renewal with:
sudo certbot renew --dry-run
If this command runs without errors, automatic renewal is set up correctly.
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
Conclusion
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 own 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. Regularly update your software, monitor your server’s performance, and ensure your applications are secure. With these steps, you’re well on your way to successfully managing a robust, secure web presence.