AWS EC2: How To Set Up Apache Virtual Hosts on CentOS 7
Are you looking for a way to host multiple sites on your AWS EC2 instance? Well it is possible, after all it is still apache that is running on the server. Below is a quick step i used to have two of my applications running on my AWS EC2 micro server.
Before you Start!
Before you begin with this guide, there are a few steps that need to be completed first. link
You will need access to a CentOS 7 server with a non-root user that has sudo privileges.
You will also need to have Apache installed in order to configure virtual hosts for it. If you haven’t already done so, you can use yum to install Apache through CentOS’s default software repositories:
sudo yum -y install httpd
sudo systemctl enable httpd.service
After these steps are complete, log in as your non-root user account through SSH and continue with the tutorial.
Note: The example configuration in this guide will make one virtual host for
website1.com and another for
website2.com. We will be using these 2 throughout the guide, but you should substitute your own domains or values while following along or after you test that it works with this example.
Step 1: Create your new website directory
sudo mkdir -p /var/www/website1.com/public_html
sudo mkdir -p /var/www/website12.com/public_html
Step 2: Grant permissions to the logged in User
sudo chown -R $USER:$USER /var/www/website1.com/public_html
sudo chown -R $USER:$USER /var/www/website12.com/public_html
sudo chmod -R 755 /var/www
Step 3: Create pages inside your Virtual Host
Create test html content (this is just a test)
<h1>Success! The website1.com virtual host is working!</h1>
Repeat Create Step 3 for all your new Virtual Hosts
Step 4: Create New Virtual Host Config Files
sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled
Open /etc/httpd/conf/httpd.conf and add below to the end of the file
Create the First Virtual Host File
sudo vi /etc/httpd/sites-available/website1.com.conf
<VirtualHost *:80> ServerName www.website1.com ServerAlias website1.com DocumentRoot /var/www/website1.com/public_html ErrorLog /var/www/website1.com/error.log CustomLog /var/www/website1.com/requests.log combined </VirtualHost>
Copy First Virtual Host and Customize for Additional Domains
Step 5: Enable the New Virtual Host Files
Now that we have created our virtual host files, we need to enable them so that Apache knows to serve them to visitors
sudo ln -s /etc/httpd/sites-available/website1.com.conf /etc/httpd/sites-enabled/website1.com.conf
Repeat same for all new virtual domains
When you are finished, restart Apache to make these changes take effect:
sudo apachectl restart
Check your domain now: www.website1.com, you should be good.
Note: If you have .htaccess in your applications, you can eithier use your rewrite/mod rules within each virtual host config or you can check your
Update AllowOverride None to AllowOverride All
If it works, let me know …cheers