いやーこれを設定するのに結構苦労しました。。orz
ググればいろいろ出てくるのですが、やはりやりたい事によってそれぞれ細かく設定が違うみたいですね。
今までのサーバーの環境
LAMP (Linux, Apache, Mysql, PHP)
つまりwordpressのための環境ですね。
今回やりたいこと
Apacheを動かしたまま、今までのサイトを維持しながらNode.jsとnginxを動かしたい
Node.jsでアプリ作りたいので只今勉強中・・・・
目標を絵で書くとこんな感じ
ザ・自作w 人が可愛く出来ましたw
nginxのrevers-proxy (リバースプロキシ)で上手く割り当てれば良いわけです。イメージ的にはApacheのvhostの設定に似ています。
では、やりましょう!
事前準備
・Apache, Node.js(expressも), nginx 全てのインストール
これはググれば沢山出てきますのでココには載せません。
・ドメイン3つ
これはそれぞれお任せします。
わたしはMacのツールhosterを使用しました。これさえあればわざわざドメインを買わなくて済みます。hosterにexampleaa.com, examplebb.com,examplecc.comを設定しておきます。
重宝しておりますよhosterさん!! hosterダウンロード
1. Apache の設定 wordpressのサイト
まずは元々80番ポートだったのを8080番ポートに変えて行きましょう。
Apacheに割り振っているドメインはexampleaa.comです。元々wordpressがあった所に設定します。
rootになって、/etc/httpd/conf/httpd.conf を編集しましょう。
1 |
$vim /etc/httpd/conf/httpd.conf |
約133行目辺りにlisten 80があるかと思います。
無い場合は、/80で検索しましょう。そして#でコメントアウトしてコピーをし8080を追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# # Listen: Allows you to bind Apache to specific IP addresses and/or # ports, in addition to the default. See also the <VirtualHost> # directive. # # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 #Listen 80 Listen 8080 # # Dynamic Shared Object (DSO) Support # # To be able to use the functionality of a module which was built as a DSO you # have to place corresponding `LoadModule' lines at this location so the # directives contained in it are actually available _before_ they are used. # Statically compiled modules (those listed by `httpd -l') do not need # to be loaded here. # # Example: |
お次はNameVirtualHostを8080に変えましょう
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # #NameVirtualHost *:80 NameVirtualHost *:8080 # # NOTE: NameVirtualHost cannot be used without a port specifier # (e.g. :80) if mod_ssl is being used, due to the nature of the # SSL protocol. # |
次はvhostです。設定していなければ無視で構いません。conf.dへ行きましょう。
1 |
cd /etc/httpd/conf.d |
exampleaa.confを編集します(ファイル名は.confであればapacheは認識しますのでなんでOK)
1 2 3 4 5 6 7 8 9 10 11 |
<VirtualHost *:8080> #<-ここが8080やで ServerName exampleaa.com DocumentRoot "/var/www/exampleaa/wordpress" DirectoryIndex index.html index.php ErrorLog /var/log/httpd/exampleaa.com_error_log CustomLog /var/log/httpd/examleaa.com_access_log combined AddDefaultCharset UTF-8 <Directory "/var/www/exampleaa/wordpress"> AllowOverride All </Directory> </VirtualHost> |
さてapacheの設定は以上です。
2.Node.jsの設定
Node.jsのサイトにはexamplecc.comを当てます。
恐らくnode.jsの基本ポートは3000かと思いますが、1023番以上であれば別に何でも構わない(厳密にいえば違いますがw)なので別のポートの設定の仕方も覚えましょう。
今回はexpressのサンプルを使います。
ファイルパスはお任せで、わたしは/root/node_Folder/2pj/って所に次のコマンドを叩きました。
1 |
express testexpress |
インストール
1 |
cd testexpress && npm install |
さぁて出来ましたね!次はポートの変更です!
ポート設定はbin/wwwってファイルにあります。15行目辺りかな〜
1 2 3 4 5 6 7 8 9 10 11 |
/** * Get port from environment and store in Express. */ //var port = normalizePort(process.env.PORT || '3000'); //コメントアウト var port = normalizePort(process.env.PORT || '55555'); app.set('port', port); /** * Create HTTP server. */ |
あとhostsに設定しておく必要があります。/etcにあるhostsを編集
1 2 3 |
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 133.242.XXX.XXX(自分のサーバIP入れてね) examplecc.com |
以上です!
3.nginxリバースプロキシの設定
さぁ大台でございます。ここが上手く設定出来れば全部見れますよ!
パスは/etc/nginx/conf.d です。default.confを編集します。ここにexamplebb.comを設定しましょう。
1 2 3 4 5 6 7 8 9 10 11 12 |
server { listen 80 default; #server_name localhost; #<-コメントアウト server_name examplebb.com; #<- ここに入力 #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/html; #<-パスを変えたかったらココ index index.html index.htm; } |
お次はApacheで設定したexampleaa.comです。conf.d配下にファイルを作ります。exampleaa_apache.confとでもしましょう。(必ず.confでファイルを作って下さい。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
server { server_name exampleaa.com; location / { proxy_pass http://127.0.0.1:8080;#<- ここは8080ポートで proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ~ |
続きましてnode.jsで作ったexpressへ飛ばすドメイン設定です。examplecc.comで設定しましょう。
ファイル名はexamplecc_node.confにしときます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
server { server_name examplecc.com; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { proxy_pass http://localhost:55555; #<- ずっと間違って放置してましたwスンマセン } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } |
以上です!では起動していきましょう!
おっとnode.js のexpressですがnode app.jsで立ち上がらない現象が起きました。これはpackage.jsonファイルでscriptを設定してまして以下でexpressが立ち上がります。
ここまで同じ様にやっていた方は
/root/node_Folder/2pj/testexpress 配下で
1 |
$npm start |
打ちます。
Apache起動
1 |
$service httpd start |
nginx起動
1 |
$service nginx start |
では、それぞれアクセスしていきます。
exampleaa.com
examplebb.com
examplecc.com
如何でしたか??見れなかったらどこか設定が噛み合っていないので今までのところでずれている所を直しましょう。
これで1つのサーバーでいろんなサイトを作れます。
もうちょっとしたらnginxからmysqlを繋げられる様にしてみますかね