Ubuntu下Nginx做前端代理Apache端口做处理PHP动态请求
1.新建站点目录
mkdir -p /srv/www/wangjiang.net/{logs,public_html}
2.安装Nginx,让它在80端口做监听;
sudo apt-get install nginx
3.在Nginx下新建站点配置文件,试图让Nginx去加载任何请求的文件或目录。
php文件的请求路由到由Apache做处理。
Nginx 占用 80 端口,过滤静态请求,然后动态请求即 Proxy 到 Apache 的 8080 端口。Proxy 反向代理的好处是访问的时候,始终就是 80端口,来访者不会觉察到有任何的区别。
sudo vi /etc/nginx/sites-available/wangjiang.net
server {
listen 80;
access_log /srv/www/wangjiang.net/logs/nginx.access.log;
error_log /srv/www/wangjiang.net/logs/nginx.error.log;
root /srv/www/wangjiang.net/public_html;
index index.php index.html;
server_name wangjiang.net;
location \ {
try_files $uri $uri/ index.php/$uri;
}
location ~* ^.*\.php$ {
if (!-f $request_filename) {
return 404;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.(ht|git) {
deny all;
}
}
sudo ln -s /etc/nginx/sites-available/wangjiang.net /etc/nginx/sites-enabled/wangjiang.net
sudo nginx -t
sudo /etc/init.d/nginx restart
4.Apache监听处理动态请求;
sudo apt-get install apache2
sudo vi /etc/apache2/sites-available/wangjiang.net
<VirtualHost *:8080>
ServerName mysite.com
DocumentRoot /var/www/wangjiang.net/public_html/
CustomLog /var/www/wangjiang.net/logs/apache.access.log common
ErrorLog /var/www/wangjiang.net/logs/apache.error.log
</VirtualHost>
sudo a2ensite wangjiang.net
sudo /etc/init.d/apache2 reload
sudo /etc/init.d/apache2 restart
5.安装PHP模块和mysql
sudo apt-get install mysql-server php5 php5-mysql php5-suhosin
sudo /etc/init.d/apache2 restart
6.
Ubuntu 修改hosts
1、修改hosts
sudo gedit /etc/hosts
2、添加解析记录( . )
127.0.0.1 wangjiang.net
3、保存后重启网络
sudo /etc/init.d/networking restart
7.修改Apache监听端口8080
sudo vi /etc/apache2/ports.conf
NameVirtualHost *:8080
Listen 8080