构建基于Nginx的web服务器
生活随笔
收集整理的這篇文章主要介紹了
构建基于Nginx的web服务器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
系統平臺:RHEL 5 Nginx版本:nginx-0.8.54 一.安裝及配置Nginx 1.安裝pcre軟件包,pcre的作用為nginx提供兼容perl的正則表達式庫。以下采用RHEL5光盤自帶的rpm包進行安裝,另外也可下載最新的源碼包進行編譯安裝。 [root@localhost~]# rpm -ivh pcre-6.6-2.el5_1.7 [root@localhost~]# rpm -ivh pcre-devel-6.6-2.el5_1.7 2.安裝nginx [root@localhost~]# tar zxf nginx-0.8.54.tar.gz [root@localhost~]# cd nginx-0.8.54 [root@localhost nginx-0.8.54]# ./configure \????? > --user=nginx \?????????? 定義nginx運行的用戶 > --group=nginx \??????? 定義nginx運行的組 >?--with-http_stub_status_module???? \\啟用站點狀態統計模塊 \\其他更多配置選項可以使用./configure --help命令進行查看 [root@localhost nginx-0.8.54]# make && make install 二.Nginx服務的運行控制 1.添加nginx運行的用戶組: [root@localhost nginx-0.8.54]# useradd -s /sbin/nologin nginx 2.Nginx默認安裝在/usr/local/nginx目錄下,為了方便應用,可以添加一個nginx主程序的符號鏈接: [root@localhost nginx-0.8.54]# ln -sf /usr/local/nginx/sbin/nginx? /usr/sbin 3.使用nginx -t命令檢查nginx配置文件是否有語法錯誤: 執行nginx -t后出現上述提示表示配置文件語法正確。 4.使用nginx啟動服務,然后使用netstat命令進行查看: [root@localhost nginx-0.8.54]# nginx 5.nginx啟動成功后,可以在瀏覽器中查看初始的web頁面: 在客戶端瀏覽器中執行:http://172.16.10.118(服務器IP地址)進行查看: 另外在服務器命令行下使用文本瀏覽器工具elink進行查看: [root@localhost nginx-0.8.54]# elinks http://172.16.10.118 6.使用系統信號控制nginx進程: [root@localhost~]# kill -s HUP nginx?? //重新加載配置文件,等同于“killall -1 nginx” [root@localhost~]# kill -s QUIT nginx? //安全退出,等同于“kill -3 nginx” [root@localhost~]# kill -s TERM nginx //快速退出,不等待處理完當前連接 另外,為了方便管理,可以添加一個nginx服務腳本,使用chkconfig和service命令管理nginx服務: [root@localhost~]# vi /etc/init.d./nginx #!/bin/bash
#chkconfig: - 99 20
#description: Nginx Service Control Script
case "$1" in
??start)
???????? /usr/sbin/nginx????
???????? ;;
??stop)
???????? /usr/bin/killall -s QUIT nginx
????????????;;
??restart)
???????? $0 stop
???????? $0 start
???????? ;;
??reload)
???????? /usr/bin/killall -s HUP nginx
???????? ;;
??*)
????echo "Usage:$0 {start|stop|restart|reload}"
????exit 1
esac
exit 0
[root@localhost~]# chmod a+x /etc/init.d/nginx??? 為nginx腳本賦予可執行權限 [root@localhost~]# chkconfig --add nginx [root@localhost~]# chkconfig --level 2345 nginx on 接下來就可以使用service nginx stop|start|restart|reload對nginx服務進行控制: 三.構建基于域名的虛擬主機 1.修改nginx主配置文件: [root@localhost~]# vi /usr/local/nginx/conf/nginx.conf user? nginx nginx;???????????????? \\運行nginx的用戶
worker_processes? 8;?????????? \\工作進程數 error_log? logs/error.log;?????? \\錯誤日志的位置
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info; pid??????? logs/nginx.pid;??????????? \\進程文件默認位于/usr/local/nginx/logs/nginx.pid
events {
??? use epoll;?????????????????????????????? \\參考事件模型,用于2.6以上的內核
??? worker_connections? 65535;??? \\每個工作進程可接受的連接數
}
http {
??? include?????? mime.types;????????\\文件擴展名與文件類型映射表
??? default_type? application/octet-stream;?? \\默認文件類型
??? charset utf-8;??????????????????????????? \\站點頁面文件使用的默認字符編碼 #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;????? \\防止網絡阻塞
??? tcp_nodelay???? on;?????? \\防止延遲 #keepalive_timeout? 0;
??? keepalive_timeout? 65;?? \\超時時間 #gzip? on; #第一虛擬主機配置 server {
??????? listen?????? 80;???????????????? \\監聽端口
??????? server_name? www.sjzz.com;???????????? \\站點的FQDN名稱 #charset koi8-r; #access_log? logs/host.access.log? main; location / {
??????????? root?? /var/www/sjzz;???????? \\網站的根目錄
??????????? 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;
??????? } location /Status {?????????????????//站點狀態統計
???? stub_status on;
???? access_log off;
?} # 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?????? 80;?????????\\監聽端口
??? #??? listen?????? somename:8080;
???????? server_name? www.linux5234.com;????? \\站點的FQDN名稱 location / {
???????????? root?? /var/www/linux5234;????????? \\網站的根目錄
???????????? index? index.html index.htm;
???????? }
?
? location /Status {????????//站點狀態統計
????? stub_status on;
????? access_log off;
?}
??? }
??? # HTTPS server
??? #
??? #server {
??? #??? listen?????? 443;
??? #??? server_name? localhost; #??? ssl????????????????? on;
??? #??? ssl_certificate????? cert.pem;
??? #??? ssl_certificate_key? cert.key; #??? ssl_session_timeout? 5m; #??? ssl_protocols? SSLv2 SSLv3 TLSv1;
??? #??? ssl_ciphers? ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
??? #??? ssl_prefer_server_ciphers?? on; #??? location / {
??? #??????? root?? html;
??? #??????? index? index.html index.htm;
??? #??? }
??? #} }
2.重啟nginx服務: [root@localhost~]# service nginx restart 3.建立虛擬主機對應網頁目錄及測試頁面: [root@localhost~]#? mkdir -p /var/www/sjzz?? /var/www/linux5234 [root@localhost~]#? echo "This is www.sjzz.com" > /var/www/sjzz/index.html [root@localhost~]#? echo "This is www.linxu5234.com" > /var/www/linux5234/index.html 4.在客戶瀏覽器中訪問不同的虛擬主機: 四.配置站點狀態統計 1.修改nginx.conf文件,在server{........}配置部分分別添加如下配置項: location /Status {
???? stub_status on;????????????\\啟用狀態統計模塊
???? access_log off;???????????? \\關閉日志記錄
?}
2.重啟nginx服務 [root@localhost~]# service nginx restart 3.在客戶端瀏覽器中訪問“http://www.sjzz.com/Status”和“http://www.sjzz.com/Status”, 即可看到站點的狀態統計信息: 五.配置FastCGI方式支持的PHP頁面 使用RHEL5系統自帶的php軟件包時,php-cgi工具由php-cli-5.1.6-23.2.el5_3 提供,位于/usr/bin/php-cgi。若使用源碼包編譯安裝php環境,需要在“./configure”時添加“--enable-cgi”選項,同進去掉“--with-apxs2”選項,否則可能無法編譯出php-cgi程序。本文采用系統默認安裝php環境。 可以使用rpm -qa |grep php查看php-cli-5.1.6-23.2.el5_3是否安裝,如果沒有安裝,就立即掛載光盤進行安裝。 1.獲取spawn-fcgi工具 spawn-fcgi從Lighttpd源碼包中獲得: [root@localhost~]# tar zxf lighttpd-1.4.20.tar.gz [root@localhost~]# cd lighttpd-1.4.20 [root@localhost~]# ./configure && make [root@localhost~]# cp src/spawn-fcgi? /usr/sbin/ 2.啟動php-cgi [root@localhost~]# spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi? -C 16??? \\啟動16個php-cgi子進程,在127.0.0.1的9000端口監聽服務 3.查看spawn-fcgi運行狀態: 使用ps aux |grep php-cgi查看進程: 4.如果需要在每次開機后都運行spawn-fcgi命令,可以將它添加到/etc/init.d/nginx或者/etc/rc.local腳本中: [root@localhost~]# vi /etc/rc.local /usr/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi? -C 16??? 5.修改nginx.conf配置文件,配置nginx支持PHP頁面: location ~ \.php$ {
???? root?? /var/www/php;
???? fastcgi_pass 127.0.0.1:9000;
???? fastcgi_index index.php;
???? fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
???? include fastcgi_params;
?}
重啟nginx服務: [root@localhost~]# service nginx restart 6.建立php網頁目錄和測試頁面: [root@localhost~]#?mkdir /var/www/php [root@localhost~]# vi /var/www/php/index.php <?php echo "PHP is OK!"; phpinfo{}; ?> 7.在客戶瀏覽器中進行驗證(成功):
#chkconfig: - 99 20
#description: Nginx Service Control Script
case "$1" in
??start)
???????? /usr/sbin/nginx????
???????? ;;
??stop)
???????? /usr/bin/killall -s QUIT nginx
????????????;;
??restart)
???????? $0 stop
???????? $0 start
???????? ;;
??reload)
???????? /usr/bin/killall -s HUP nginx
???????? ;;
??*)
????echo "Usage:$0 {start|stop|restart|reload}"
????exit 1
esac
exit 0
[root@localhost~]# chmod a+x /etc/init.d/nginx??? 為nginx腳本賦予可執行權限 [root@localhost~]# chkconfig --add nginx [root@localhost~]# chkconfig --level 2345 nginx on 接下來就可以使用service nginx stop|start|restart|reload對nginx服務進行控制: 三.構建基于域名的虛擬主機 1.修改nginx主配置文件: [root@localhost~]# vi /usr/local/nginx/conf/nginx.conf user? nginx nginx;???????????????? \\運行nginx的用戶
worker_processes? 8;?????????? \\工作進程數 error_log? logs/error.log;?????? \\錯誤日志的位置
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info; pid??????? logs/nginx.pid;??????????? \\進程文件默認位于/usr/local/nginx/logs/nginx.pid
events {
??? use epoll;?????????????????????????????? \\參考事件模型,用于2.6以上的內核
??? worker_connections? 65535;??? \\每個工作進程可接受的連接數
}
http {
??? include?????? mime.types;????????\\文件擴展名與文件類型映射表
??? default_type? application/octet-stream;?? \\默認文件類型
??? charset utf-8;??????????????????????????? \\站點頁面文件使用的默認字符編碼 #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;????? \\防止網絡阻塞
??? tcp_nodelay???? on;?????? \\防止延遲 #keepalive_timeout? 0;
??? keepalive_timeout? 65;?? \\超時時間 #gzip? on; #第一虛擬主機配置 server {
??????? listen?????? 80;???????????????? \\監聽端口
??????? server_name? www.sjzz.com;???????????? \\站點的FQDN名稱 #charset koi8-r; #access_log? logs/host.access.log? main; location / {
??????????? root?? /var/www/sjzz;???????? \\網站的根目錄
??????????? 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;
??????? } location /Status {?????????????????//站點狀態統計
???? stub_status on;
???? access_log off;
?} # 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?????? 80;?????????\\監聽端口
??? #??? listen?????? somename:8080;
???????? server_name? www.linux5234.com;????? \\站點的FQDN名稱 location / {
???????????? root?? /var/www/linux5234;????????? \\網站的根目錄
???????????? index? index.html index.htm;
???????? }
?
? location /Status {????????//站點狀態統計
????? stub_status on;
????? access_log off;
?}
??? }
??? # HTTPS server
??? #
??? #server {
??? #??? listen?????? 443;
??? #??? server_name? localhost; #??? ssl????????????????? on;
??? #??? ssl_certificate????? cert.pem;
??? #??? ssl_certificate_key? cert.key; #??? ssl_session_timeout? 5m; #??? ssl_protocols? SSLv2 SSLv3 TLSv1;
??? #??? ssl_ciphers? ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
??? #??? ssl_prefer_server_ciphers?? on; #??? location / {
??? #??????? root?? html;
??? #??????? index? index.html index.htm;
??? #??? }
??? #} }
2.重啟nginx服務: [root@localhost~]# service nginx restart 3.建立虛擬主機對應網頁目錄及測試頁面: [root@localhost~]#? mkdir -p /var/www/sjzz?? /var/www/linux5234 [root@localhost~]#? echo "This is www.sjzz.com" > /var/www/sjzz/index.html [root@localhost~]#? echo "This is www.linxu5234.com" > /var/www/linux5234/index.html 4.在客戶瀏覽器中訪問不同的虛擬主機: 四.配置站點狀態統計 1.修改nginx.conf文件,在server{........}配置部分分別添加如下配置項: location /Status {
???? stub_status on;????????????\\啟用狀態統計模塊
???? access_log off;???????????? \\關閉日志記錄
?}
2.重啟nginx服務 [root@localhost~]# service nginx restart 3.在客戶端瀏覽器中訪問“http://www.sjzz.com/Status”和“http://www.sjzz.com/Status”, 即可看到站點的狀態統計信息: 五.配置FastCGI方式支持的PHP頁面 使用RHEL5系統自帶的php軟件包時,php-cgi工具由php-cli-5.1.6-23.2.el5_3 提供,位于/usr/bin/php-cgi。若使用源碼包編譯安裝php環境,需要在“./configure”時添加“--enable-cgi”選項,同進去掉“--with-apxs2”選項,否則可能無法編譯出php-cgi程序。本文采用系統默認安裝php環境。 可以使用rpm -qa |grep php查看php-cli-5.1.6-23.2.el5_3是否安裝,如果沒有安裝,就立即掛載光盤進行安裝。 1.獲取spawn-fcgi工具 spawn-fcgi從Lighttpd源碼包中獲得: [root@localhost~]# tar zxf lighttpd-1.4.20.tar.gz [root@localhost~]# cd lighttpd-1.4.20 [root@localhost~]# ./configure && make [root@localhost~]# cp src/spawn-fcgi? /usr/sbin/ 2.啟動php-cgi [root@localhost~]# spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi? -C 16??? \\啟動16個php-cgi子進程,在127.0.0.1的9000端口監聽服務 3.查看spawn-fcgi運行狀態: 使用ps aux |grep php-cgi查看進程: 4.如果需要在每次開機后都運行spawn-fcgi命令,可以將它添加到/etc/init.d/nginx或者/etc/rc.local腳本中: [root@localhost~]# vi /etc/rc.local /usr/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi? -C 16??? 5.修改nginx.conf配置文件,配置nginx支持PHP頁面: location ~ \.php$ {
???? root?? /var/www/php;
???? fastcgi_pass 127.0.0.1:9000;
???? fastcgi_index index.php;
???? fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
???? include fastcgi_params;
?}
重啟nginx服務: [root@localhost~]# service nginx restart 6.建立php網頁目錄和測試頁面: [root@localhost~]#?mkdir /var/www/php [root@localhost~]# vi /var/www/php/index.php <?php echo "PHP is OK!"; phpinfo{}; ?> 7.在客戶瀏覽器中進行驗證(成功):
轉載于:https://blog.51cto.com/kk5234/514578
總結
以上是生活随笔為你收集整理的构建基于Nginx的web服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用8位字节的编码格式将字节流安全的转换
- 下一篇: 毕业后的第一个五年