1. Nginx安裝
首先需要預先安裝gcc-g++、PCRE, zlib价涝。PCRE用于重寫rewrite女蜈,zlib用于gzip壓縮
到安裝目錄下,
下載nginx
wget http://nginx.org/download/nginx-1.5.9.tar.gz
解壓
tar -zxvf nginx-1.5.9.tar.gz
進入到新的目錄下
cd nginx-1.5.9
Nginx的安裝配置
進行安裝配置configure
在configure命令后面加上相應配置項:
--prefix=path 定義一個目錄色瘩,存放服務器上的文件 伪窖,也就是nginx的安裝目錄。默認使用 /usr/local/nginx居兆。
--prefix=path 定義一個目錄覆山,存放服務器上的文件 ,也就是nginx的安裝目錄泥栖。默認使用 /usr/local/nginx簇宽。
--sbin-path=path 設置nginx的可執(zhí)行文件的路徑,默認為 prefix/sbin/nginx.
--conf-path=path 設置在nginx.conf配置文件的路徑吧享。nginx允許使用不同的配置文件啟動魏割,通過命令行中的-c選項。默認為prefix/conf/nginx.conf.
--pid-path=path 設置nginx.pid文件钢颂,將存儲的主進程的進程號钞它。安裝完成后,可以隨時改變的文件名 殊鞭, 在nginx.conf配置文件中使用 PID指令遭垛。默認情況下,文件名 為prefix/logs/nginx.pid.
--error-log-path=path 設置主錯誤钱豁,警告耻卡,和診斷文件的名稱。安裝完成后牲尺,可以隨時改變的文件名 姨涡,在nginx.conf配置文件中 使用 的error_log指令辱魁。默認情況下匪补,文件名 為prefix/logs/error.log.
--http-log-path=path 設置主請求的HTTP服務器的日志文件的名稱咕宿。安裝完成后,可以隨時改變的文件名 蜒简,在nginx.conf配置文件中 使用 的access_log指令瘸羡。默認情況下,文件名 為prefix/logs/access.log.
--user=name 設置nginx工作進程的用戶搓茬。安裝完成后犹赖,可以隨時更改的名稱在nginx.conf配置文件中 使用的user指令队他。默認的用戶名是nobody。
--group=name 設置nginx工作進程的用戶組峻村。安裝完成后麸折,可以隨時更改的名稱在nginx.conf配置文件中 使用的 user指令。默認的為非特權用戶粘昨。
--with-select_module --without-select_module 啟用或禁用構建一個模塊來允許服務器使用select()方法垢啼。該模塊將自動建立,如果平臺不支持的kqueue张肾,epoll芭析,rtsig或/dev/poll。
--with-poll_module --without-poll_module啟用或禁用構建一個模塊來允許服務器使用poll()方法吞瞪。該模塊將自動建立馁启,如果平臺不支持的kqueue,epoll尸饺,rtsig或/dev/poll进统。
--without-http_gzip_module — 不編譯壓縮的HTTP服務器的響應模塊助币。編譯并運行此模塊需要zlib庫浪听。
--without-http_rewrite_module 不編譯重寫模塊。編譯并運行此模塊需要PCRE庫支持眉菱。
--without-http_proxy_module — 不編譯http_proxy模塊迹栓。
--with-http_ssl_module — 使用https協(xié)議模塊。默認情況下俭缓,該模塊沒有被構建克伊。建立并運行此模塊的OpenSSL庫是必需的。
--with-pcre=path — 設置PCRE庫的源碼路徑华坦。PCRE庫的源碼(版本4.4 - 8.30)需要從PCRE網(wǎng)站下載并解壓愿吹。其余的工作是Nginx的./ configure和make來完成。正則表達式使用在location指令和 ngx_http_rewrite_module 模塊中惜姐。
--with-pcre-jit —編譯PCRE包含“just-in-time compilation”(1.1.12中犁跪, pcre_jit指令)。
--with-zlib=path —設置的zlib庫的源碼路徑歹袁。要下載從 zlib(版本1.1.3 -
1.2.5)的并解壓坷衍。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模塊需要使用zlib 条舔。--with-cc-opt=parameters — 設置額外的參數(shù)將被添加到CFLAGS變量枫耳。例如,當你在FreeBSD上使用PCRE庫時需要使用:--with-cc-opt="-I /usr/local/include。.如需要需要增加 select()支持的文件數(shù)量:--with-cc-opt="-D FD_SETSIZE=2048".
--with-ld-opt=parameters —設置附加的參數(shù)孟抗,將用于在鏈接期間迁杨。例如钻心,當在FreeBSD下使用該系統(tǒng)的PCRE庫,應指定:--with-ld-opt="-L /usr/local/lib".
例如
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--without-http_rewrite_module \
--without-http_gzip_module \
--user=nginx \
--group=nginx
(這里由于我沒有裝pcre, zlib,所以選的是without)
成功后铅协,可以看到目錄中出現(xiàn)了Makefile文件扔役。
先后使用make和make install,安裝完成警医。
可以用 nginx –V
來查看版本號亿胸。
2. Nginx的啟動、停止
啟動代碼:
格式:nginx安裝目錄地址 -c nginx配置文件地址
如:/usr/sbin/nginx -c /root/zhangxiao/nginx-1.5.9/conf/nginx.conf
查看進程:
ps -ef|grep nginx
停止進程:
kill -QUIT 進程號
Nginx的nginx.conf配置文件
默認生成的文件為:
#user nobody;
worker_processes 1;
#error_log 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;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache'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;
# }
#}
}
文件結構:
... #全局塊
events { #events塊
...
}
http #http塊
{
... #http全局塊
server #server塊
{
... #server全局塊
location [PATTERN] #location塊
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局塊
}
其中:
1预皇、全局塊:配置影響nginx全局的指令侈玄。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑吟温,日志存放路徑序仙,配置文件引入,允許生成worker
process數(shù)等鲁豪。
2潘悼、events塊:配置影響nginx服務器或與用戶的網(wǎng)絡連接。有每個進程的最大連接數(shù)爬橡,選取哪種事件驅動模型處理連接請求治唤,是否允許同時接受多個網(wǎng)路連接,開啟多個網(wǎng)絡連接序列化等糙申。
3宾添、http塊:可以嵌套多個server,配置代理柜裸,緩存缕陕,日志定義等絕大多數(shù)功能和第三方模塊的配置。如文件引入疙挺,mime-type定義扛邑,日志自定義,是否使用sendfile傳輸文件铐然,連接超時時間蔬崩,單連接請求數(shù)等。
4锦爵、server塊:配置虛擬主機的相關參數(shù)舱殿,一個http中可以有多個server。
5险掀、location塊:配置請求的路由沪袭,以及各種頁面的處理情況。
Nginx異常解決
啟動nginx報錯:
nginx: [emerg] getpwnam("nginx") failed in /usr/local/nginx/conf/nginx.conf:2
是因為配制文件中使用的啟動賬戶在系統(tǒng)中并沒有找到
解決辦法:
useradd -G groupname username
啟動nginx報錯:
Starting nginx: nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed
(2: No such file or directory)
權限不足,解決辦法:
cd /var/tmp/
mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}