How to serve a website from home directory is a common question being asked in developer community. To serve a website from home directory of your hosting account may it be a virtual private server or a cloud server is not much difficult as you think. This article explains in brief about achieving this.
Some of you may think that why is it required? The answer is, instead of forking the web server’s default web root permissions it is most convenient way. Also, transferring files to server directly to default web-root is not possible when non root user is not granted the write permissions to it. To avoid this scenario, here we suggest an alternative way to host a website in home directory.
To achieve this, we will make a child directory in user’s home named public_html folder and grant specific permissions to directories in it so that it will be accessed by world. After creating the directory, the structure would look like /home/ec2-user/public_html .
To serve your website on an ec2 instance without uploading to root directory /var/www/html/. Given below are the detailed instructions to follow assuming the server is amazon linux:
To host a website in the EC2-user home directory instead of the default /var/www/html
directory, you’ll need to configure your web server to serve files from your home directory. Here are detailed instructions using Apache as an example:
Step 1: Launch an EC2 Instance
- Sign in to your AWS Management Console.
- Navigate to the EC2 Dashboard and click Launch Instance.
- Configure the instance: Choose an Amazon Machine Image (AMI), instance type, and security group.
- Launch the instance and connect to it via SSH.
Step 2: Install Apache Web Server
Update your package lists:
sudo yum update -y
Then, Install Apache:
sudo yum install httpd -y
Finally, Start and enable Apache:
sudo systemctl start httpd
sudo systemctl enable httpd
Step 3: Configure Apache to Serve Files from Your Home Directory
Create a symbolic link from your home directory to the default web root directory:
sudo ln -s /home/ec2-user/public_html /var/www/html
Set permissions to ensure the web server can access the files:
sudo chown -R apache:apache /home/ec2-user/public_html
sudo chmod -R 755 /home/ec2-user/public_html
Step 4: Configure Apache Virtual Hosts (Optional)
If you want to use a custom domain, you can configure virtual hosts.
Create a new configuration file in /etc/httpd/conf.d/
sudo nano /etc/httpd/conf.d/your-domain.conf
Then, add the following configuration in it:
<VirtualHost *:80>
ServerAdmin admin@yourdomain.com
DocumentRoot /home/ec2-user/public_html
ServerName yourdomain.com
ErrorLog /var/log/httpd/your-domain-error.log
CustomLog /var/log/httpd/your-domain-access.log combined
</VirtualHost>
Replace yourdomain.com
with your domain name in above configuration.
Step 5: Restart Apache web server
We need to Restart Apache to apply the changes
sudo systemctl restart httpd
Step 6: Test Your Setup to serve a website from home directory
Access your website using your domain name or the EC2 instance’s public IP address.
By following these steps, you can host your website in your EC2-user home directory instead of the default /var/www/html
directory.