diadia

興味があることをやってみる。自分のメモを残しておきます。

centos7にnginxをインストール

nginxをインストールする

まずnginxをインストールするためのレポジトリを準備する。以下のコマンドを実行しないとnginxのソースがないのでnginxのインストールできない。

yum install -y epel-release

nginxのインストール

sudo yum install -y nginx

nginxのサービス開始

sudo systemctl start nginx
sudo systemctl enable nginx

 

http(80番ポート)を開放

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload

 

nginxの設定ファイルを編集

nginxの設定についてはnginx実践入門という本を読むと少しわかる。といっても以下の内容に帰結するので以下のコードを設定ファイルに張り付ければよい。

sudo vi /etc/nginx/conf.d/project.conf
server {
    listen  80;
    server_name 153.126.216.172;

    location /static {
        alias /usr/share/nginx/html/static;
    }

    location /media {
        alias /usr/share/nginx/html/media;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

server_name に使用しているサーバのIPを記述すること。

設定ファイルの適正性を確認

sudo nginx -t
#以下のように表示されれば設定ファイルは問題ない。
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl restart nginx