LoginSignup
26
31

More than 5 years have passed since last update.

Nginxを使って、1つのサーバーで複数サイトを運用

Posted at

やりたいこと

1つのサーバーで複数のサイトを構築したい!

nginxをインストール

さくらVPSで、SSL+CentOS7+Nginx+PHP7+MySQLを構築する方法 を参考にnginxをインストールします。

複数サイト用に設定ファイルのディレクトリ作成

各サイトの設定ファイルを置くディレクトリ(環境に合わせて好きな場所に)

$ mkdir /etc/nginx/conf.d/sites-available

メンテナンス性を高めるために、設定ファイルのシンボリックリンクを張るディレクトリ

$ mkdir /etc/nginx/conf.d/sites-enable

nginxに権限付与

$ sudo chown -R nginx.nginx /etc/nginx/conf.d/sites-*

運用したい各サイトのソースを置くディレクトリを作成

$ mkdir /var/www/html/{service_name}
$ chown -R nginx.nginx /var/www/

各サイトの設定ファイルを作成

vim /etc/nginx/conf.d/sites-available/{service_name}.conf

内容は、

server {
    listen       80;
    server_name  {ドメイン};
    index        index.php;
    access_log   /var/log/nginx/{service_name}.access.log; // 環境に合わせて指定
}

各サイトの設定ファイルのシンボリックリンクを張る

$ ln -s /etc/nginx/conf.d/sites-available/{service_name}.conf /etc/nginx/conf.d/sites-enable/

各サイトの設定ファイルを読み込むようにする

$ vim /etc/nginx/nginx.conf

以下をhttp { ... }の中に追記し、各サイトの設定ファイルをシンボリックリンクで読み込めるようにする。

include /etc/nginx/conf.d/sites-enable/*.conf;

/etc/nginx/nginx.confと各サイトの設定ファイルの全体像はこのようになります。

/etc/nginx/nginx.confの内容

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

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

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

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

    index   index.php index.html index.htm;

    include /etc/nginx/conf.d/sites-enable/*.conf;
}

各サイトの設定ファイルの内容

server {
    listen       80;
    server_name  {ドメイン};
    index        index.php;
    access_log   /var/log/nginx/{service_name}.access.log;
}

nginx再起動

$ systemctl restart nginx.service
26
31
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
26
31