nginxでバーチャルホスト設定、Ghost、Expressを動かす | ドロップシッピング道場【無料ブログで成功するコツを紹介】

ドロップシッピング道場【無料ブログで成功するコツを紹介】

副業として稼ぐためのドロップシッピングをはじめの一歩から公開します。儲からないと諦める前に一度ご覧ください。

WEBサーバーのデファクトスタンダードであるapacheだが、
静的ファイルをより早く処理を実行したい、
高負荷な環境で非同期のイベント駆動を行いたい、
といった場合に力を発揮するのがnginxだ。

今回、このnginxでバーチャルホストを設定する方法を記載する。
アプリケーションにはGhostとexpressを使用するので、
node.jsとGhostのインストールについては、

node.jsのブログプラットフォームGhostをインストールする。

を参考に。
インストール環境はCentOS6.3

expressのインストール


# express - e sample

# cd sample

# npm install

[sample]# npm install

[sample]# forever start ./bin/www

Ghost設定

※ghostは0.4.2をroot直下にインストールしているものとする。 # vi /root/ghost-0.4.2

12行目

url: 'http://blog.○○○.com',


36行目以下

server: {

           // Host to be passed to node's `net.Server#listen()`

           host: '○○○.com',

           // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`

           port: '2368'

       }

50行目以下

production: {

       url: 'http://blog.○○○.com',


nginxインストール


# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

# yum -y install nginx

# nginx -v nginx version: nginx/1.6.0 apacheを共存させるので、httpd.confのポートを変更。


# vi /etc/httpd/conf/httpd.conf ↓Listenを変更

Listen 8080 ↓NameVirtualHostを変更

NameVirtualHost *:8080


↓バーチャルホストの設定箇所を変更

<VirtualHost *:8080> 設定が終わったら、apacheを再起動させる。 #service httpd restart 再起動後はhttp://○○○.com:8080 で表示される。

# mkdir /etc/nginx/sites-available

# mkdir /etc/nginx/sites-enabled

# vi /etc/nginx/nginx.conf

httpセクションに次の1行を加える。

include /etc/nginx/sites-enabled/*;

○○○.comをexpressでの運用 blog.○○○.comをGhostでの運用 とした場合、

# vi /etc/nginx/sites-available/○○○.com


server {

   listen 80;

   server_name ○○○.com www.○○○.com;


   access_log  /root/○○○/○○○.com/log/access.log;


   location / {

       proxy_set_header X-Real-IP $remote_addr;

       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

       proxy_set_header Host $http_host;

       proxy_set_header X-NginX-Proxy true;


       proxy_pass http://111.222.33.44:3000/; #IPはダミー

       proxy_redirect off;


   }


}

server {

   listen 80;

   server_name blog.○○○.com;


   access_log  /root/○○○/blog.○○○.com/log/access.log;


   location / {

       proxy_set_header X-Real-IP $remote_addr;

       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

       proxy_set_header Host $http_host;

       proxy_set_header X-NginX-Proxy true;


       proxy_pass http://111.222.33.44:2368/; #IPはダミー

       proxy_redirect off;

   }

}

access_logの置き先ディレクトリとファイルは事前に作成しておく。 IPアドレスは自分の環境のものに。

# cd /etc/nginx/sites-enabled

# ln -s ../sites-available/○○○.com

# ls -la

total 8~と表示される。 /rootのパーミッションを755にする。

# /etc/init.d/nginx stop

# /etc/init.d/nginx start

nginx restartはエラーが出るので、使わない。

Starting nginx: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32

が出る場合、

# vi /etc/nginx/nginx.conf

server_names_hash_bucket_size  64;

をdefault_type の下に追加する。

これで、 http://○○○.com (Hello Express) http://blog.○○○.com (Ghost) ブラウザからアクセスすると表示される。 ※追記:nginxのserver_nameにwww.○○○.comを追加。  server_name ○○○.com www.○○○.com;