Setting up and running a PHP Web Application on AWS with SSL

A scalable and secure PHP application on Apache with AWS EC2, RDS, and Let's Encrypt

In this tutorial, we will cover the steps to set up and run a PHP web application on Amazon Web Services (AWS) with SSL. We will use AWS services like Amazon EC2, Amazon RDS, and Let's Encrypt.

Step 1: Launching an Amazon EC2 Instance

  1. Go to the AWS EC2 Console and launch a new instance.
  2. Choose an Amazon Machine Image (AMI) with PHP pre-installed, such as the Amazon Linux 2 AMI or the Ubuntu Server AMI.
  3. Configure the instance details, such as the instance type, VPC, subnet, and security group.
  4. Add a new rule to the security group to allow inbound traffic on port 80 (HTTP) and port 443 (HTTPS).
  5. Launch the instance and connect to it via SSH.

Step 2: Installing the Required Software

First, install the required software packages on the EC2 instance, such as Apache, PHP, and MySQL.

sudo yum update -y
sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2

Start the Apache and MySQL services.

sudo systemctl start httpd
sudo systemctl start mariadb


Configure the Apache virtual host for the PHP web application. This will initially not be accessible with SSL (HTTPS). However, by adding the HTTP accessibility we can test our server and ensure PHP is working properly and add the SSL certificate a bit later.

sudo nano /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
</VirtualHost>


Step 3: Configuring an Amazon RDS Instance

  1. Go to the AWS RDS Console and create a new database instance.
  2. Choose a database engine and version, such as MySQL 8.0.
  3. Configure the instance details, such as the instance class, storage, and security group.
  4. Set the master username and password for the database instance.
  5. Launch the instance and wait for it to become available.

Step 4: Installing an SSL Certificate

You can enable SSL easily with Let's Encrypt by installing Certbot on the EC2 instance using the following commands.

sudo yum install certbot python2-certbot-apache -y
sudo certbot --apache --email [your email address] --agree-tos --no-eff-email --domains [domain name]

Follow the instructions to generate and install the SSL certificate. Then, verify that SSL is enabled by accessing the PHP application with HTTPS from a browser.

Step 5: Enabling HTTPS on the Apache Server

Install the mod_ssl​ module for Apache

sudo yum install -y mod_ssl

Edit the Apache virtual host configuration file and add the SSL certificate settings. We'll also update the HTTP configuration we added earlier and redirect it to HTTPS.

sudo nano /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName [domain name]
Redirect permanent / https://[domain name]/
</VirtualHost>

<VirtualHost *:443>
ServerName [domain name]
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/[domain name]/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/[domain name]/privkey.pem
</VirtualHost>

Restart the Apache service

sudo systemctl restart httpd


Step 6: Deploying the PHP Web Application

Copy the PHP files of the web application to the document root directory of the Apache server.

sudo cp -R /path/to/web/app /var/www/html

Configure the PHP settings, such as the database connection credentials, in the configuration file of the web application.

sudo nano /var/www/html/config.php
define('DB_HOST', '<rds_instance_endpoint>');
define('DB_NAME', '<database_name>');
define('DB_USER', '<database_username>');
define('DB_PASS', '<database_password>');

Your application should now be accessible on your domain name via HTTPs. You can also choose to setup AWS Auto Scaling and Load balancing to enable more traffic. You can do this by creating an AMI image from your running instance.

Sign in to leave a comment
Building a React App with a MongoDB Backend on AWS
Building a portable, scalable React web application with a MongoDB backend on AWS