首页 文章

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

提问于
浏览
1

我在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 回答

  • 5

    确保 /home/ec2-user/samplesite.com/public_html 具有755权限 .

    使其工作的另一种方法是将nginx工作进程作为 ec2-user 运行 . 在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
    

    希望能帮助到你 .

相关问题