發現其實Nginx有時候挺神器的
原本的apache2+tomcat7雙端口的server
用Nginx整合到了80端口用
這樣就不需要開多個vps
浪費空間時間以及金錢啦~
PHP方面:
先是安裝Nginx
1 | apt-get install nginx |
再是安裝mysql以及php
1 | apt-get install php5-cli php5-cgi mysql-server php5-mysql |
安裝FastCgi PHP解析器
1 | apt-get install spawn-fcgi |
啓動Nginx
1 | service nginx restart |
安裝的Nginx在啓動時
會自動加載/etc/nginx/conf.d中的配置文件
所以我們想要增加虛擬伺服器的話
直接在其中添加配置文檔即可
先編輯/etc/nginx/nginx.conf文檔
將server塊中的代碼如下編輯
使直接ip訪問代理到/usr/share/nginx/www/index.html文件上
1 2 3 4 5 6 7 8 9 10 | server { listen 80; server_name _; # access_log /var/log/nginx/access.log main; server_name_in_redirect off; location / { root /usr/share/nginx/www ; index index.html; } } |
開啓Gzip壓縮
Nginx會對Text類型自動Gz壓縮
但是並不會對xml css js壓縮
所以我們要去掉下面代碼前的注視
將代碼生效
1 | gzip_types text /plain text /css application /json application /x-javascript text /xml application /xml application$ |
然後我們就開始新建虛擬伺服器
在conf.d中新增yumao.name.conf文檔
在其中寫入以下內容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | server { listen 80; #傳入域名 server_name yumao.name www.yumao.name; #非www域名進行301跳轉 if ($host != 'www.yumao.name' ) { rewrite ^/(.*)$ https: //www .yumao.name/$1 permanent; } #設置www根目錄 以及僞靜態設置 location / { root /var/www/yumao .name; index index.php index.html index.htm; #ignored: "-" thing used or unknown variable in regex/rew if (!-f $request_filename){ set $rule_1 1$rule_1; } if (!-d $request_filename){ set $rule_1 2$rule_1; } if ($rule_1 = "21" ){ rewrite /. /index .php last; } } #使用FastCgi解釋php文件 location ~ \.php$ { root /var/www/yumao .name; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/fastcgi_params ; } } |
啓動FastGgi 監聽端口9000
1 | spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi |
在/var/www/yumao.name中新建index.php
編寫內容如下
1 | <?php phpinfo(); ?> |
保存後重啓Nginx
1 | service nginx restart |
使用域名訪問即可獲得php信息
順帶nginx的用戶也是www-data
使用以下命令對www資料夾進行權限分配
1 | chown www-data:www-data -R /var/www/ |
爲了每次開機都可以運行fastcgi
可以將fastcgi的啓動命令
添加入/etc/rc.local文檔中
JSP方面:
安裝jre
1 | apt-get install openjdk-7-jre |
在官網下載Tomcat
解壓至/var/www/teemo.name
運行/var/www/teemo.name/bin/startup.sh驅動tomcat
在/etc/nginx/conf.d中添加teemo.name.conf
內容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | server { listen 80; #傳入域名 server_name teemo.name www.teemo.name; #非www域名進行301跳轉 if ($host != 'www.teemo.name' ) { rewrite ^/(.*)$ http: //www .teemo.name/$1 permanent; } #將所有teemo.name傳入的請求都轉發至Tomcat location / { proxy_pass http: //localhost :8080; } } |
重啓Nginx
1 | service nginx restart |
使用teemo.name域名訪問
即可獲得Tomcat默認頁面
高级啊,深奥啊,看不懂啊。
哈哈。
时间长没玩都忘记了。。