使用Amazon Linux AMI在EC2上用于Web目录的Nginx用户帐户

时间:2014-01-22 17:48:19

标签: amazon-web-services nginx amazon-ec2

我在Amazon Linux AMI上设置了Nginx。默认的Nginx页面加载正常:

http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com/

和nginx.conf中的服务器语句代码是:

server {
    listen       80;
    #server_name  localhost;
    server_name ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com;

    #charset koi8-r;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    # redirect server error pages to the static page /40x.html
    #
    error_page  404              /404.html;
    location = /40x.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

通过将server_name更改为

,对我的域进行了测试
server_name samplesite.com;

通过使用浏览器访问

也可以正常工作
http://samplesite.com/

但是当我将“location / root”更改为其他路径时,它不起作用,这里是nginx.conf服务器语句:

server {
    listen       80;
    server_name samplesite.com;

    #charset koi8-r;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root /home/ec2-user/samplesite.com/public_html; <--- Changed here!!!
        index  index.html index.htm;
    }

    # redirect server error pages to the static page /40x.html
    #
    error_page  404              /404.html;
    location = /40x.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

唯一改变的部分是

root /home/ec2-user/samplesite.com/public_html;

这是因为我希望网站目录位于“ec2-user”下。我在

检查了默认的Nginx目录文件所有者
/usr/share/nginx/html

但是所有内容都归“root”所有,我知道不建议使用root帐户,并且应该使用其他用户帐户:

drwxr-xr-x 2 root root 4096 Jan 22 01:14 .
drwxr-xr-x 3 root root 4096 Jan 22 01:14 ..
-rw-r--r-- 1 root root 3696 Nov 21 22:04 404.html
-rw-r--r-- 1 root root 3738 Nov 21 22:04 50x.html
-rw-r--r-- 1 root root 3770 Nov 21 22:04 index.html
-rw-r--r-- 1 root root  370 Nov 21 22:04 nginx-logo.png

所以想要使用“ec2-user”(或者我应该为网络创建一个新帐户?)。所以我的问题是,目录必须由“root”拥有才能工作吗?因为如果目录所有者是/home/目录下的“ec2-user”,我似乎无法访问该网站。

1 个答案:

答案 0 :(得分:5)

确保/home/ec2-user/samplesite.com/public_html拥有755个权限。

使其工作的另一种方法是以ec2-user运行nginx工作进程。在nginx.conf文件中有类似这样的内容:

user              ec2-user;
worker_processes  1;

确保/home/ec2-user/samplesite.com/public_html下的所有内容都拥有owner.group ec2-user

chown -R ec2-user.ec2-user /home/ec2-user/samplesite.com/public_html

希望它有所帮助。