2017-11-10 12 views
1

私はaws ec2に私のウェブサイトを展開しようとしています。これはpython/djangoにあり、Webサイトを自分で展開する方法を学びたいと思っています。私はawsのEBSにいくつかの問題があったので、最初に手動で行う方法を知りたいと思います。 私はこのためにgunicornとnginxを使うことにしました。NginxのDjangoヘルプが必要です

私は、仮想のenvにgunicorn使用してウェブサイトを実行することができると私は/home/ec2-user/gunicorn_start.bashで次のスクリプトを作成しました:

#!/bin/bash 

NAME="davidbiencom"         # Name of the 
application 
DJANGODIR=/home/ec2-user/davidbien    # Django project directory 
SOCKFILE=/home/ec2-user/virtual/run/gunicorn.sock 
USER=ec2-user          
GROUP=ec2-user          
NUM_WORKERS=3          
DJANGO_SETTINGS_MODULE=davidbiencom.settings    
DJANGO_WSGI_MODULE=davidbiencom.wsgi      

echo "Starting $NAME as `whoami`" 

# Activate the virtual environment 
cd $DJANGODIR 
source /home/ec2-user/virtual/bin/activate 
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE 
export PYTHONPATH=$DJANGODIR:$PYTHONPATH 
# Create the run directory if it doesn't exist 
RUNDIR=$(dirname $SOCKFILE) 
test -d $RUNDIR || mkdir -p $RUNDIR 

# Start your Django Unicorn 
# Programs meant to be run under supervisor should not daemonize themselves (do$ 
exec gunicorn ${DJANGO_WSGI_MODULE}:application \ 
--name $NAME \ 
--workers $NUM_WORKERS \ 
--user=$USER --group=$GROUP \ 
--bind=unix:$SOCKFILE \ 
--log-level=debug \ 
--log-file=- 

これは、私はエラーがないと信じて細かい動作します。 次へnginxをインストールしてサービスを開始します。私はウェルカムページを取得すると、実行中であることを確認します。次の私は、次の操作を行います。

  1. Goは/etc/nginx/nginx.confと

    が/etc/nginx/sites-enabled/*.conf含めるhttpに以下を追加することを。

次に/ etc/nginx/sites-availableとsites-enabledに2つのフォルダを作成します。 私は(更新)ファイルdavidbien.confを作成し、内部に次のように入力します。

upstream app_server { 
# fail_timeout=0 means we always retry an upstream even if it failed 
# to return a good HTTP response 

# for UNIX domain socket setups 
server unix:/home/ec2-user/virtual/run/gunicorn.sock fail_timeout=0; 

# for a TCP configuration 
# server 192.168.0.7:8000 fail_timeout=0; 
} 

server { 
listen 80; 
server_name 35.176.185.50; 

#Max upload size 
client_max_body_size 75M; # adjust to taste 

location /static/ { 
    root /home/ec2-user/davidbien/static; 
} 

location/{ 
    # checks for static file, if not found proxy to app 
    try_files $uri @proxy_to_app; 
} 

location @proxy_to_app { 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    # enable this if and only if you use HTTPS 
    # proxy_set_header X-Forwarded-Proto https; 
    proxy_set_header Host $http_host; 
    # we don't want nginx trying to do something clever with 
    # redirects, we set the Host: header above already. 
    proxy_redirect off; 
    proxy_pass http://app_server; 
    } 
} 

私はこのファイルを保存し、次のコマンドを実行します。

ln -s /etc/nginx/sites-available/davidbiencom.conf /etc/nginx/sites-enabled/davidbiencom.conf 

これが行われた後、私はnginxのを再起動し、I IPアドレスを入力すると、502のゲートウェイエラーが発生します。

ここで間違っていますか? ありがとうございます。

EDIT: ここなど/ nginxnginx/confファイルがあります: ここではVAR /ログ/ nginxの/ error.logファイルからエラーログ

2017/11/10 22:26:27 [error] 27620#0: *1 open() "/usr/share/nginx/html/favicon.iicon.ico" failed (2: No such file or directory), client: 2.96.149.96, server: $localhost, request: "GET /favicon.ico HTTP/1.1", host: "35.176.185.50",referrer: "http://35.176.185.50/" 

EDIT 2です

http { 
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
        '$status $body_bytes_sent "$http_referer" ' 
        '"$http_user_agent" "$http_x_forwarded_for"'; 

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

sendfile   on; 
tcp_nopush   on; 
tcp_nodelay   on; 
keepalive_timeout 65; 
types_hash_max_size 2048; 

#include    /etc/nginx/mime.types; 
#default_type  application/octet-stream; 

# Load modular configuration files from the /etc/nginx/conf.d directory. 
# See http://nginx.org/en/docs/ngx_core_module.html#include 
# for more information. 
include /etc/nginx/sites-enabled/*.conf; 

index index.html index.htm; 

server { 
    listen  80 default_server; 
    listen  [::]:80 default_server; 
    server_name localhost; 
    root   /usr/share/nginx/html; 

    # Load configuration files for the default server block. 
    include /etc/nginx/default.d/*.conf; 

    location/{ 

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

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

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
    # 
    #location ~ \.php$ { 
    # proxy_pass http://127.0.0.1; 
    #} 
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
    # 
    #location ~ \.php$ { 
    # root   html; 
    # fastcgi_pass 127.0.0.1:9000; 
    # fastcgi_index index.php; 
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 
    # include  fastcgi_params; 
    #} 
# deny access to .htaccess files, if Apache's document root 
    # concurs with nginx's one 
    # 
    #location ~ /\.ht { 
    # deny all; 
    #} 
} 

答えて

1

あなたは」 127.0.0.1:3031へのリクエストを転送しなければならないが、gunicornを開始するスクリプトから見ると、gunicornがソケットにバインドされていることをnginxに伝える。127.0.0.1:3031にgunicornワーカーを開始する場合は、仕事

+0

私はそれについて考えているので、私はその部分を必要としません。あなたはそれが削除できると思いますか? その行を削除した後、私は403を禁止しています。 – davidb

+0

私はgunicornがあなたがそれを削除するとポート8000​​で始まると思う –

+0

しかし、nginxはそのアプリケーションにポート80に来るすべてのトラフィックを指示していないのですか? それを0.0.0.0:80にする必要がありますか?いくつかのチュートリアルにはそのような設定がないことがわかります。 – davidb