Nginx安裝、配置及使用總結
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
Nginx的安裝、配置及使用總結:
Nginx是一個高性能的HTTP及反向代理服務器,也是IMAP/POP3/SMTP代理服務器。在高並發情況下,Nginx突出了它的高性能和穩定性,對比同類服務器技術而言,它是很多國內大中型網站首選的服務器環境。和往常一樣,在總結一門新技術時都會先從它的環境配置及使用開始的,下面就以Nginx的安裝、配置及簡單的使用為導向進行總結說明。
l Nginx安裝
l Nginx配置
l php-fpm配置
l Nginx使用
l 問題及解決
一、Nginx安裝
1、從官網(http://www.nginx.org)下載最新的Nginx並解壓,進入解壓目錄進行相關安裝操作即可,具體如下:
$ tar –xvf nginx-1.8.1.tar
$ cd nginx-1.8.1
$ sudo ./configure
$ sudo make
$sudo make install
2、安裝之後,使用nginx –v驗證下是否安裝完成:
$ nginx -V
3、開啟nginx服務,並打開瀏覽器地址:127.0.0.1
$ sudo ./nginx // 開啟服務
上圖說明,Nginx環境已經安裝並運行正常,接下來需要對nginx.conf進行幾項重要的配置了。
二、Nginx配置
一般情況下,我們只需要對conf下的nginx.conf進行基本配置即可,但有時我們也需要特殊的配置,這個在下面會介紹到,我們修改默認的nginx.conf(最後備份下方便回滾)配置如下,註釋部分即為修改內容(實際使用時,去掉註釋):
1、修改的nginx.conf配置文件
#user nobody;
worker_processes auto; #根據設備cpu的個數 自動選擇
#error_log /nginx/nginx-1.8.1/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024; #允許請求的連接數
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main ‘$remote_addr - $remote_user[$time_local] "$request" ‘
# ‘$status $body_bytes_sent"$http_referer" ‘
# ‘"$http_user_agent""$http_x_forwarded_for"‘;
#access_log logs/access.log main;
sendfile on; #允許發送文件
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #會話超時時間
#gzip on;
server {
listen 80; #監聽的端口
server_name localhost; #服務端域名或ip
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #Web服務的根目錄
root /project/cwteam/cwteam/cwteam;
index index.html index.htm index.php;# 加入了html和php
#如果文件不存在則嘗試TP解析
try_files $uri /index.php$uri;
}
error_page 404 /404.html;
# redirect server error pages to thestatic page /50x.html
#
error_page 500 502 503 504 /50x.html; #可自定義錯誤頁面
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apachelistening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGIserver listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root /project/cwteam/cwteam/cwteam;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
location ~ \.php { #默認nginx不支持php拓展 這裏把它添加上
root /project/cwteam/cwteam/cwteam;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
#設置PATH_INFO,註意fastcgi_split_path_info已經自動改寫了fastcgi_script_name變量,
#後面不需要再改寫SCRIPT_FILENAME,SCRIPT_NAME環境變量,所以必須在加載fastcgi.conf之前設置
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
#加載Nginx默認"服務器環境變量"配置
include fastcgi.conf;
}
# deny access to .htaccess files, ifApache‘s document root
# concurs with nginx‘s one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-,name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
註:
默認Nginx不支持對php的拓展,所以需要添加對其的拓展支持,具體查看上岸註釋內容說明。
2、測試下配置是否正常
$ sudo ./nginx -t
三、php-fpm配置
參考:
http://blog.csdn.net/why_2012_gogo/article/details/51112477
四、Nginx使用
正如上面的Nginx.conf配置,Web的服務根路徑已經修改為自定義項目了,所以可以直接輸入訪問即可,而我的項目采用了ThinkPHP開源框架,步驟如下:
1、添加html頁面
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
</head>
<bodystyle="background-color:#9999;">
<div>Hello Nginx!</div>
</body>
</html>
2、添加控制器
public function index() {
$this->display();
}
這個控制器只是展示上面的頁面哦!
3、瀏覽器的結果
五、問題及解決
在上面的整個過程中,遇到了些許問題,具體可參看如下:
1、nginx:[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No suchfile or directory)
上面的報錯,是在使用nginx–s reload時發生的(前提是先nginx –s stop之後),原因是reload是檢查正在運行的nginx服務,stop之後不能重新加載,只要nginx再次開啟,就可以使用reload了,所以這個問題不會影響我們使用nginx,如果就是要解決的話,可以這樣:
$ nginx –c /usr/local/nginx/conf/nginx.conf (關聯位置)
註:
使用nginx -c的參數指定nginx.conf文件的位置。
2、[error] 3846#0: *3 kevent()reported that connect() failed (61: Connection refused) while connecting toupstream, client: 127.0.0.1, server: localhost, request: "GET /index.phpHTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host:"localhost"
報錯問題:因為php-fpm進程服務未啟動,所以需要開啟之外,還需要對php-fpm.conf中的error_log 和pid進行配置,否則會導致php-fpm因為找不到位置而啟動不了。
解決方法:
首先,添加php-fpm配置文件:
$ sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf (復制一份默認配置文件並修改)
其次,修改php-fpm.conf配置文件:
去掉前面的註釋,將php-fpm.conf中的error_log修改為/var/log/php-fpm.log,而pid修改為/var/run/php-fpm.pid即可。
最後,啟動php-fpm:
$ sudo php-fpm (啟動)
註:
當啟動時,報服務已經在使用,不能進行綁定,那麽請使用kill -9 pid強制殺掉重新啟動即可。
3、SQLSTATE[HY000][2002] No such file or directory
上面的報錯,是本人在剛搭建好的Nginx環境中試運行PHP訪問數據時出現錯誤了,原因是因為Nginx的數據庫連接未打開,即使Mysql服務運行正常,Nginx服務也找不到數據庫,解決的辦法:
$ sudo find / -namemysql.sock(數據庫連接文件)
註:已經發現了mysql.sock文件,從目錄可看出這個.sock文件是之前系統中
xampp繼承環境所持有的數據庫Mysql連接文件,所以簡單了,只需要將該文件映射關聯到/var/mysql下即可。
$ ls /var/mysql (檢查是否存在,若不存在就創建)
$ sudo mkdir/var/mysql (創建完成之後,使用ln關聯)
$ sudo ln –s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock /var/mysql/mysql.sock(關聯之後 刷新頁面即可)
好了,到這裏,我們已經介紹了Nginx的安裝、基本配置及簡單的驗證使用了,馬上開始Nginx的高效之旅吧!
Nginx安裝、配置及使用總結