Attempting to use symbolic link for var/www/html

2020-03-02 04:57发布

问题:

this is what I'm trying to accomplish: creating symbolic link from var/www/html to a directory in the home (~) folder. the directory I'm trying to symlink to in home (~) is a git repository, if that makes any difference. I have an index.html file in this directory.

I've created a symbolic link to var/www/html on an Amazon EC2 instance using this command: ln -s ~/dirIWant/ html, however this is resulting in the following error when I try to access my webpage: 403 Forbidden "You don't have permission to access / on this server." I'm using apache.

Has anybody else tried to do something similar and gotten it to work?

Currently, when I go to my website www.blah.com, it shows this 403 error. I've tried to change the permission using sudo chown -h apache:apache but it doesn't seem to help. Do you have any other ideas?

回答1:

This is because apache runs as apache user and the /var/www/html is owned by root in Amazon Linux AMI. You can change ownership/permissions as suggested by Frank, or use userdirs.

It seems to me that you want the webserver's folder to be conveniently accessible from your home directory (~). I wanted something like this on my EC2 and decided to use Per-user web directories (mod_userdir).

This feature of lets you keep parts of the HTTP server's space in a directory owned by a user. Each user gets his own directory, located by default in /home/username/public_html. The pages, scripts and other files in that directory are accessible to the internet by appending /~username to your domain. Additionally, you can change the name of that folder in httpd.conf from public_html to something else, like gitRepo. In that case, if you have an index.html file in /home/ec2-user/gitRepo/index.html, it will be accessible to the public via http://ec2-hostname.aws.amazon.com/~ec2-user/index.html and be owned by ec2-user, which is convenient for editing files from user level.

To set this up on EC2 (assuming "gitRepo" for the folder name you want to use), use nano /etc/httpd/conf/httpd.conf to open Apache config file and scroll down until you see <IfModule mod_userdir.c>. Then change this section to look like the following:

<IfModule mod_userdir.c>
    #
    # UserDir is disabled by default since it can confirm the presence
    # of a username on the system (depending on home directory
    # permissions).
    #
    UserDir enabled all

    #
    # To enable requests to /~user/ to serve the user's public_html
    # directory, remove the "UserDir disabled" line above, and uncomment
    # the following line instead:
    #
    UserDir gitRepo

</IfModule>

Afterwards you should be good to go but ensure the permissions are set up correctly:

chmod 711 /home/ec2-user
chmod 755 /home/ec2-user/gitRepo
chown -R ec2-user /home/ec2-user/gitRepo

And finally reload the web server like this:

sudo service httpd reload