二進制源碼包下載安裝,爲安裝順利可先使用yum安裝pcre-devel;openssl; openssl-devel包javascript
cd /usr/local/srcphp
wget http://nginx.org/download/nginx-1.12.1.tar.gzcss
tar zxf nginx-1.12.1.tar.gzhtml
./configure --prefix=/usr/local/nginxjava
make &&make installnode
vim /etc/init.d/nginx #新建添加如下內容:nginx
#!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL
chmod 755 /etc/init.d/nginx; chkconfig --add nginx ;chkconfig nginx on #設置權限和開機自啓動vim
cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak #備份原有的nginx.conf文件bash
vim /usr/local/nginx/conf/nginx.conf #新建nginx.conf配置文件並添加如下內容:app
#啓動nginx程序的用戶 user nobody nobody; #定義子進程個數 worker_processes 2; #錯誤日誌 error_log /usr/local/nginx/logs/nginx_error.log crit; #pid pid /usr/local/nginx/logs/nginx.pid; #最多打開多少個文件 worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; #最大鏈接 } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; #如下,每一個server對應一個虛擬主機 server { listen 80; #監聽端口 server_name localhost; #域名 index index.html index.htm index.php; root /usr/local/nginx/html; #網站根目錄 location ~ \.php$ #配置解析php部分 { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; #指定調用php;可寫 ip:端口 形式 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }
/usr/local/nginx/sbin/nginx -t #檢測配置文件是否有問題
/etc/init.d/nginx start #啓動nginx服務
測試php解析:
vim /usr/local/nginx/html/1.php #建立測試php文件並添加如下內容:
<?php echo "test php scripts."; ?>
curl localhost/1.php
在nginx.conf中配置server{}來定義虛擬主機並不方便管理,能夠使每一個虛擬主機都有一個本身的配置文件:
vim /usr/local/nginx/conf/nginx.conf #增長如下內容:
include vhost/*.conf #表示虛擬主機配置文件將存放在/usr/local/nginx/conf/vhost中;/usr/local/nginx/conf/vhost須要手動建立
vim /usr/local/nginx/conf/vhost/default.conf #新建虛擬主機配置文件並添加如下內容:
server { listen 80 default_server; #定義監聽端口;有default_server表示默認虛擬主機 server_name aaa.com; #定義域名 index index.html index.htm index.php; #定義支持的首頁格式 root /data/wwwroot/default; #定義網頁根目錄 }
mkdir -p /data/wwwroot/default/ #建立默認虛擬主機的根目錄
echo 「This is a default site.」>/data/wwwroot/default/index.html #建立測試文件
/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reload #檢查並從新加載配置文件
curl localhost #測試
vim /usr/local/nginx/conf/vhost/test.com.conf #建立新的虛擬主機配置文件並添加如下內容:
server { listen 80; server_name www.test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / #表示匹配根目錄 { auth_basic "auth"; #表示用戶認證的名字 auth_basic_user_file /data/wwwroot/.htpasswd; #認證用戶保存文件,該文件須要使用httpd的htpasswd命令生成 } }
mkdir /data/wwwroot/test.com ;echo "this is test.com" >/data/wwwroot/test.com/index.html #建立根目錄和測試文件
yum install -y httpd ;htpasswd -c /usr/local/nginx/conf/htpasswd aming #指定用戶aming並生成密碼,-c重置
/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reload #檢查並從新加載配置文件
curl -x127.0.0.1:80 test.com -I #測試,狀態碼爲401說明須要驗證
curl -uaming:lishiming -x127.0.0.1:80 test.com #加入用戶密碼測試
#在server的{}中添加 location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; }
Apache中,servername後面只能帶一個域名。而在nginx中,server_name後面能夠跟多個域名
vim /usr/local/nginx/conf/vhost/test.com.conf #更改配置文件爲如下內容:
server { listen 80; server_name test.com test1.com test2.com; #配置多個域名 index index.html index.htm index.php; root /data/wwwroot/test.com; #設置主域名 if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; #其餘域名跳轉到test.com,permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302 } }
/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reload #檢查並從新加載配置文件
curl -x127.0.0.1:80 test2.com/index.html -I #測試提示301,重定向到test.com/index.html