SETUP APACHE ON AWS EC2 INSTANCE WITH USER-DATA AUTOMATIC SETUP

Abdul Gofur
3 min readJul 20, 2020
Amazon EC2 Linux AMI

OS Requirement: AMI Amazon Linux

USER-DATA (ADD IN CREATING EC2 INSTANCE)

#!/bin/bash

sudo yum update -y

sudo yum install -y httpd24 php72 php72-mysqlnd php72-mbstring

sudo yum install -y php72-mcrypt php72-zip php72-intl php72-gd

sudo service httpd start

sudo chkconfig httpd on

sudo groupadd www

sudo usermod -a -G www ec2-user

sudo usermod -a -G www apache

sudo chown -R ec2-user /var/www

sudo chgrp -R www /var/www

sudo chmod 2775 /var/www

find /var/www -type d -exec sudo chmod 2775 {} +

find /var/www -type f -exec sudo chmod 0664 {} +

sudo chmod 0777 /var/lib/php/7.2/session

sudo service httpd restart

sudo yum install mod24_ssl

VCONF

<VirtualHost *:80>

# REQUIRED. Set this to the host/domain/subdomain that

# you want this VirtualHost record to handle.

ServerName example.com

# Optional. You can specify additional host names that

# serve up the same site. This can be top-level, domains,

# sub-domains, and can even use wildcard subdomains such

# as *.yourdomain.com — just separate each host name

# with a single space.

ServerAlias www.example.com

# REQUIRED. Set this to the directory you want to use for

# this vhost site’s files.

DocumentRoot /var/www/html

# Optional. Uncomment this and set it to your admin email

# address, if you have one. If there is a server error,

# this is the address that Apache will show to users.

ServerAdmin your.email@domain.com

# Optional. Uncomment this if you want to specify

# a different error log file than the default. You will

# need to create the error file first.

#ErrorLog /var/www/vhosts/logs/error_log

# REQUIRED. Let’s make sure that .htaccess files work on

# this site. Don’t forget to change the file path to

# match your DocumentRoot setting above.

<Directory /var/www/html>

AllowOverride All

</Directory>

</VirtualHost>

<VirtualHost *:443>

# REQUIRED. Set this to the host/domain/subdomain that

# you want this VirtualHost record to handle.

ServerName example.com

# Optional. You can specify additional host names that

# serve up the same site. This can be top-level, domains,

# sub-domains, and can even use wildcard subdomains such

# as *.yourdomain.com — just separate each host name

# with a single space.

# REQUIRED. Set this to the directory you want to use for

# this vhost site’s files.

DocumentRoot /var/www/html

# Optional. Uncomment this and set it to your admin email

# address, if you have one. If there is a server error,

# this is the address that Apache will show to users.

ServerAdmin your.email@example.com

# Optional. Uncomment this if you want to specify

# a different error log file than the default. You will

# need to create the error file first.

#ErrorLog /var/www/vhosts/logs/error_log

SSLEngine on

SSLCertificateFile /path/to/your/certificate/file.pem

SSLCertificateKeyFile /path/to/your/certificate/privatekey/file.pem

SSLCertificateChainFile /path/to/your/certificate/chain/file.pem

# REQUIRED. Let’s make sure that .htaccess files work on

# this site. Don’t forget to change the file path to

# match your DocumentRoot setting above.

<Directory /var/www/html>

AllowOverride All

</Directory>

</VirtualHost>

Install certbot-auto

cd ~

wget https://dl.eff.org/certbot-auto

chmod a+x certbot-auto

Requesting a certificate (on root dir location)

sudo ./certbot-auto — debug -v — server https://acme-v01.api.letsencrypt.org/directory certonly -d example.com -d www.example.com

Open virtual host file

Then update vhost file with new certificate file

sudo vi /etc/httpd/conf.d/vhost.conf

--

--

Abdul Gofur
0 Followers

I’m not Developer but Problem Solver