近日有一需求,需要在一台主机上用nginx部署2个php应用,分别是wordpress和wiki,探索了半天,终于部署好了,下面把过程记录下来。
1. 在nginx下创建vhosts目录,用以放置vhost文件。
mkdir vhosts
2. 修改nginx.conf的配置, 在http节点增加下面内容设置,用来包含vhosts里的配置文件
# vhosts files include /usr/local/nginx/vhosts/*;
3. 在vhosts下创建testwp.com.conf和duwiki.com.conf文件, 注意:这里的配置文件以conf结尾
testwp.com.conf
server{ listen 80; server_name www.testwp.com; location / { root /Users/jiangzhiqiang/phpwork/wordpress; index index.php index.html index.htm; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /Users/jiangzhiqiang/phpwork/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # $document_root指前面的root路径 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
duwiki.com.conf
server { listen 80; server_name www.duwiki.com; location / { root /Users/jiangzhiqiang/phpwork/dokuwiki; index index.php index.html index.htm; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /Users/jiangzhiqiang/phpwork/dokuwiki; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to files, dokuwiki settings location ~ /(data|conf|bin|inc)/ { deny all; } }
carbon.conf
server { listen 80; server_name www.carbon.com; location / { root /Users/jiangzhiqiang/phpwork/carbon; index index.php index.html index.htm; } # nginx configuration error_page 404 /404.php; rewrite ^([^\.]*)/dashboard$ $1/dashboard.php; rewrite ^([^\.]*)/favorites(/page/([0-9]*))?$ $1/favorites.php?page=$3 last; rewrite ^([^\.]*)/json/([0-9a-z_-]+)$ $1/json.php?action=$2 last; rewrite ^([^\.]*)/login$ $1/login.php; rewrite ^([^\.]*)/manage$ $1/manage.php; rewrite ^([^\.]*)/new$ $1/new.php; rewrite ^([^\.]*)/notifications$ $1/notifications.php; rewrite ^([^\.]*)/page/([0-9]+)$ $1/index.php?page=$2 last; rewrite ^([^\.]*)/register$ $1/register.php; rewrite ^([^\.]*)/reply$ $1/reply.php; rewrite ^([^\.]*)/robots.txt$ $1/robots.php; rewrite ^([^\.]*)/search.xml$ $1/open_search.php; rewrite ^([^\.]*)/search/(.*?)(/page/([0-9]*))?$ $1/search.php?keyword=$2&page=$4 last; rewrite ^([^\.]*)/settings$ $1/settings.php; rewrite ^([^\.]*)/sitemap-(topics|pages|tags|users|index)(-([0-9]+))?.xml$ $1/sitemap.php?action=$2&page=$4 last; rewrite ^([^\.]*)/statistics$ $1/statistics.php; rewrite ^([^\.]*)/t/([0-9]+)(-([0-9]*))?$ $1/topic.php?id=$2&page=$4 last; rewrite ^([^\.]*)/tag/(.*?)(/page/([0-9]*))?$ $1/tag.php?name=$2&page=$4 last; rewrite ^([^\.]*)/tags/following(/page/([0-9]*))?$ $1/favorite_tags.php?page=$3 last; rewrite ^([^\.]*)/u/(.*?)$ $1/user.php?username=$2 last; rewrite ^([^\.]*)/users/following(/page/([0-9]*))?$ $1/favorite_users.php?page=$3 last; rewrite ^([^\.]*)/upload_controller$ $1/upload_controller.php; #QSA is automatic in nginx. rewrite ^([^\.]*)/view-(desktop|mobile)$ $1/view.php?view=$2 last; # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /Users/jiangzhiqiang/phpwork/carbon; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to files, settings location ~ /(data|conf|bin|inc)/ { deny all; } }
4. 修改hosts文件
sudo vi /etc/hosts 127.0.0.1 www.testwp.com 127.0.0.1 www.duwiki.com 127.0.0.1 www.carbon.com
5. 重启 nginx
sudo nginx -s reload
在浏览器url栏输入 www.testwp.com, 就访问到本地部署的wordpress应用,访问 www.duwiki.com,就访问到本地部署的 dokuwiki 应用。