Create and access multiple ports for Apache on Amazon EC2(aka AWSEC2) or DigitalOcean
We can do this by using the virtual host. There is two part to complete to open and configure the ports. I used port 8080 but you can use any port that is not using for any service.
Part 1: Add/Update Virtual Host
- Navigate to the sites-available directory for the configuration file
cd /etc/apache2/sites-available/
You can check all available configuration files by
ls -la
- For updating existing configuration file choose one from the list and jump at step 5
- Create a virtual host that listens to 8080 or the port number that Apache is listening to.
- Copy the default configuration file for setting up a new one
sudo cp 000-default.conf 8080-default.conf
You can name the file as you like, I named it with 8080 because it will be easy for me to understand.
- Now open the file to edit
sudo nano 8080-default.conf
- Copy below code block or Update the file like as below
Listen 8080
<VirtualHost *:8080>
#ServerName www.your-domain.com
ServerAdmin admin@your-domain.com
DocumentRoot /var/www/html/path/to/project/directory
#LogLevel info ssl:warn
LogLevel warn
ErrorLog //var/www/html/path/to/project/directory/logs/error.log
CustomLog /var/www/html/path/to/project/directory/logs/access.log combined
</VirtualHost>
- Copy below code block or Update the file like as below
- Enable the site with sudo a2ensite name of the conf without .conf
sudo a2ensite 8080-default
- Reload the Apache first that if there is any error, we can fix that before the restart
sudo service apache2 reload
- Restart the apache now if reload is successful
sudo service apache2 restart
Check in the browser with www.your-domain.com:8080, if you can access then it’s done. If not then you follow Part 2 to open the port 8080.
Part 2: Set rule for the Port 8080 or the port you want.
- Go to your Amazon/DigitalOcean Console Panel
- Find the security group of the instance on the instances list.
- Go to Security Group (for AWS EC2, find it on the LHS menu) and add the rules to allow the external world to talk to the port numbers your apache is listening to. Please follow this link (for AWS EC2) to add the rules.