LoginSignup
5
9

More than 5 years have passed since last update.

CentOS7にnginxの設定

Last updated at Posted at 2017-12-31

CentOS7でnginxの設定手順。
CentOSで安定版のNginxをインストールする。

設定ファイル

/etc/yum.repos.d/nginx.repo というファイルを作成し、そのファイルに以下の設定を追加します。

/etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=0


#{OSのバージョン}箇所に特定のバージョンを記述
baseurl=http://nginx.org/packages/centos/{OSのバージョン}/$basearch/

nginxをインストール

sudo yum -y --enablerepo=nginx install nginx

インストール後、バージョンが表示されればOK。

nginx -v
・
・
・
nginx version: nginx/1.12.2

nginxの設定

nginxの初期設定のファイルをバックアップ、
{app_name}.confを新規で作成します。

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf_old
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/{app_name}.conf
vi /etc/nginx/conf.d/{app_name}.conf

{app_name}.confを編集して、{app_name}の設定をします。
rootの箇所がドキュメントルートを設定します。

/etc/nginx/conf.d/{app_name}.conf
server {
    listen       80;
    server_name {app_name};
    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log;
    root /var/www/{app_name}/public;

    error_page  404              /404.html;
    error_page  500 502 503 504  /50x.html;
}

nginxの起動

systemctl status nginxでnginxの起動状態を確認します。
起動している場合は、Activeと表示されます。
起動している場合は、sudo systemctl restart nginxで再起動して、設定したファイルを読み込ませます。
※nginxの設定を書き換えた場合、再起動してください。

#状態確認
systemctl status nginx

#起動
sudo systemctl start nginx

#再起動
sudo systemctl restart nginx

起動して、ドキュメントルート以下に置いたファイルが表示できればOK。

5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9