# CentOS/RHEL yum -y install gcc gcc-c++ autoconf automake make \ zlib zlib-devel openssl openssl-devel pcre-devel # 建立專用使用者/群組 groupadd -r nginx useradd -s /sbin/nologin -g nginx -r nginx
wget https://nginx.org/download/nginx-1.5.2.tar.gz tar -zxvf nginx-1.5.2.tar.gz cd nginx-1.5.2 ./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/run/nginx/nginx.pid \ --user=nginx --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_gzip_static_module \ --http-log-path=/var/log/nginx/access.log \ --http-client-body-temp-path=/var/tmp/nginx/client \ --http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \ --with-http_stub_status_module make && make install
提示:若 make 失敗提示缺少編譯器,執行 yum -y install gcc automake autoconf libtool make。
# 啟動 /usr/local/nginx/sbin/nginx # 重載配置(平滑重啟) /usr/local/nginx/sbin/nginx -s reload # 測試配置語法 /usr/local/nginx/sbin/nginx -t
mkdir /usr/local/nginx/conf/vhost.conf,如 linlik.conf:server {
listen 80;
server_name jiahaolin.com www.111cn.net;
index index.html index.htm index.php;
root /www/jiahaolin;
include emlog.conf;
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; }
location ~ .*\.(js|css)?$ { expires 12h; }
access_log /home/wwwlogs/jiahaolin.com.log access;
}
nginx.conf http 區塊加入:include vhost/*.conf;404.html(>512 bytes,避免 IE 內建頁面)。nginx.conf http 區塊加入:fastcgi_intercept_errors on;server 區塊加入:error_page 404 /404.html; 或 error_page 404 =https://www.xxx.com/404.html;error_page 500 502 503 504 /50x.html;關鍵:缺少 fastcgi_intercept_errors on; 會導致自訂錯誤頁不生效。
bind() to 0.0.0.0:80 failed (98: Address already in use)# 找出佔用 80 埠的進程 lsof -i :80 # 或 netstat -anp | grep :80 # 強制終止 kill -9# 或 killall -9 nginx # 重新啟動 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
worker_connections > open file resource limit# 臨時生效 ulimit -n 65535 # 永久生效 /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535 # 需重新登入或重啟
error_log off 不會關閉日誌正確關閉:error_log /dev/null crit;
確保 Nginx 執行使用者對日誌目錄有寫入權限,或調整 error_log 路徑。
nginx -t 再 nginx -s reload。access_log 與 error_log,設定 logrotate 定期輪轉。include 模組化管理虛擬主機,便於維護。