How to serve a website from home directory instead of default root?

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

  1. Sign in to your AWS Management Console.
  2. Navigate to the EC2 Dashboard and click Launch Instance.
  3. Configure the instance: Choose an Amazon Machine Image (AMI), instance type, and security group.
  4. Launch the instance and connect to it via SSH.

Step 2: Install Apache Web Server

Update your package lists:

Then, Install Apache:

Finally, Start and enable Apache:

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:

Set permissions to ensure the web server can access the files:

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/

Then, add the following configuration in it:

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

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.

Leave a Comment

Please disable your adblocker or whitelist this site!