构建基于Nginx的web服务器
生活随笔
收集整理的這篇文章主要介紹了
构建基于Nginx的web服务器
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
系統(tǒng)平臺:RHEL 5 Nginx版本:nginx-0.8.54 一.安裝及配置Nginx 1.安裝pcre軟件包,pcre的作用為nginx提供兼容perl的正則表達(dá)式庫。以下采用RHEL5光盤自帶的rpm包進(jìn)行安裝,另外也可下載最新的源碼包進(jìn)行編譯安裝。 [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運(yùn)行的用戶 > --group=nginx \??????? 定義nginx運(yùn)行的組 >?--with-http_stub_status_module???? \\啟用站點(diǎn)狀態(tài)統(tǒng)計(jì)模塊 \\其他更多配置選項(xiàng)可以使用./configure --help命令進(jìn)行查看 [root@localhost nginx-0.8.54]# make && make install 二.Nginx服務(wù)的運(yùn)行控制 1.添加nginx運(yùn)行的用戶組: [root@localhost nginx-0.8.54]# useradd -s /sbin/nologin nginx 2.Nginx默認(rèn)安裝在/usr/local/nginx目錄下,為了方便應(yīng)用,可以添加一個(gè)nginx主程序的符號鏈接: [root@localhost nginx-0.8.54]# ln -sf /usr/local/nginx/sbin/nginx? /usr/sbin 3.使用nginx -t命令檢查nginx配置文件是否有語法錯(cuò)誤: 執(zhí)行nginx -t后出現(xiàn)上述提示表示配置文件語法正確。 4.使用nginx啟動服務(wù),然后使用netstat命令進(jìn)行查看: [root@localhost nginx-0.8.54]# nginx 5.nginx啟動成功后,可以在瀏覽器中查看初始的web頁面: 在客戶端瀏覽器中執(zhí)行:http://172.16.10.118(服務(wù)器IP地址)進(jìn)行查看: 另外在服務(wù)器命令行下使用文本瀏覽器工具elink進(jìn)行查看: [root@localhost nginx-0.8.54]# elinks http://172.16.10.118 6.使用系統(tǒng)信號控制nginx進(jìn)程: [root@localhost~]# kill -s HUP nginx?? //重新加載配置文件,等同于“killall -1 nginx” [root@localhost~]# kill -s QUIT nginx? //安全退出,等同于“kill -3 nginx” [root@localhost~]# kill -s TERM nginx //快速退出,不等待處理完當(dāng)前連接 另外,為了方便管理,可以添加一個(gè)nginx服務(wù)腳本,使用chkconfig和service命令管理nginx服務(wù): [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腳本賦予可執(zhí)行權(quán)限 [root@localhost~]# chkconfig --add nginx [root@localhost~]# chkconfig --level 2345 nginx on 接下來就可以使用service nginx stop|start|restart|reload對nginx服務(wù)進(jìn)行控制: 三.構(gòu)建基于域名的虛擬主機(jī) 1.修改nginx主配置文件: [root@localhost~]# vi /usr/local/nginx/conf/nginx.conf user? nginx nginx;???????????????? \\運(yùn)行nginx的用戶
worker_processes? 8;?????????? \\工作進(jìn)程數(shù) error_log? logs/error.log;?????? \\錯(cuò)誤日志的位置
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info; pid??????? logs/nginx.pid;??????????? \\進(jìn)程文件默認(rèn)位于/usr/local/nginx/logs/nginx.pid
events {
??? use epoll;?????????????????????????????? \\參考事件模型,用于2.6以上的內(nèi)核
??? worker_connections? 65535;??? \\每個(gè)工作進(jìn)程可接受的連接數(shù)
}
http {
??? include?????? mime.types;????????\\文件擴(kuò)展名與文件類型映射表
??? default_type? application/octet-stream;?? \\默認(rèn)文件類型
??? charset utf-8;??????????????????????????? \\站點(diǎn)頁面文件使用的默認(rèn)字符編碼 #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;????? \\防止網(wǎng)絡(luò)阻塞
??? tcp_nodelay???? on;?????? \\防止延遲 #keepalive_timeout? 0;
??? keepalive_timeout? 65;?? \\超時(shí)時(shí)間 #gzip? on; #第一虛擬主機(jī)配置 server {
??????? listen?????? 80;???????????????? \\監(jiān)聽端口
??????? server_name? www.sjzz.com;???????????? \\站點(diǎn)的FQDN名稱 #charset koi8-r; #access_log? logs/host.access.log? main; location / {
??????????? root?? /var/www/sjzz;???????? \\網(wǎng)站的根目錄
??????????? 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 {?????????????????//站點(diǎn)狀態(tài)統(tǒng)計(jì)
???? 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;
??????? #}
??? } #第二個(gè)虛擬主機(jī)配置
??? # another virtual host using mix of IP-, name-, and port-based configuration
??? #
??? server {
???????? listen?????? 80;?????????\\監(jiān)聽端口
??? #??? listen?????? somename:8080;
???????? server_name? www.linux5234.com;????? \\站點(diǎn)的FQDN名稱 location / {
???????????? root?? /var/www/linux5234;????????? \\網(wǎng)站的根目錄
???????????? index? index.html index.htm;
???????? }
?
? location /Status {????????//站點(diǎn)狀態(tài)統(tǒng)計(jì)
????? 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服務(wù): [root@localhost~]# service nginx restart 3.建立虛擬主機(jī)對應(yīng)網(wǎng)頁目錄及測試頁面: [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.在客戶瀏覽器中訪問不同的虛擬主機(jī): 四.配置站點(diǎn)狀態(tài)統(tǒng)計(jì) 1.修改nginx.conf文件,在server{........}配置部分分別添加如下配置項(xiàng): location /Status {
???? stub_status on;????????????\\啟用狀態(tài)統(tǒng)計(jì)模塊
???? access_log off;???????????? \\關(guān)閉日志記錄
?}
2.重啟nginx服務(wù) [root@localhost~]# service nginx restart 3.在客戶端瀏覽器中訪問“http://www.sjzz.com/Status”和“http://www.sjzz.com/Status”, 即可看到站點(diǎn)的狀態(tài)統(tǒng)計(jì)信息: 五.配置FastCGI方式支持的PHP頁面 使用RHEL5系統(tǒng)自帶的php軟件包時(shí),php-cgi工具由php-cli-5.1.6-23.2.el5_3 提供,位于/usr/bin/php-cgi。若使用源碼包編譯安裝php環(huán)境,需要在“./configure”時(shí)添加“--enable-cgi”選項(xiàng),同進(jìn)去掉“--with-apxs2”選項(xiàng),否則可能無法編譯出php-cgi程序。本文采用系統(tǒng)默認(rèn)安裝php環(huán)境。 可以使用rpm -qa |grep php查看php-cli-5.1.6-23.2.el5_3是否安裝,如果沒有安裝,就立即掛載光盤進(jìn)行安裝。 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個(gè)php-cgi子進(jìn)程,在127.0.0.1的9000端口監(jiān)聽服務(wù) 3.查看spawn-fcgi運(yùn)行狀態(tài): 使用ps aux |grep php-cgi查看進(jìn)程: 4.如果需要在每次開機(jī)后都運(yùn)行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服務(wù): [root@localhost~]# service nginx restart 6.建立php網(wǎng)頁目錄和測試頁面: [root@localhost~]#?mkdir /var/www/php [root@localhost~]# vi /var/www/php/index.php <?php echo "PHP is OK!"; phpinfo{}; ?> 7.在客戶瀏覽器中進(jìn)行驗(yàn)證(成功):
#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腳本賦予可執(zhí)行權(quán)限 [root@localhost~]# chkconfig --add nginx [root@localhost~]# chkconfig --level 2345 nginx on 接下來就可以使用service nginx stop|start|restart|reload對nginx服務(wù)進(jìn)行控制: 三.構(gòu)建基于域名的虛擬主機(jī) 1.修改nginx主配置文件: [root@localhost~]# vi /usr/local/nginx/conf/nginx.conf user? nginx nginx;???????????????? \\運(yùn)行nginx的用戶
worker_processes? 8;?????????? \\工作進(jìn)程數(shù) error_log? logs/error.log;?????? \\錯(cuò)誤日志的位置
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info; pid??????? logs/nginx.pid;??????????? \\進(jìn)程文件默認(rèn)位于/usr/local/nginx/logs/nginx.pid
events {
??? use epoll;?????????????????????????????? \\參考事件模型,用于2.6以上的內(nèi)核
??? worker_connections? 65535;??? \\每個(gè)工作進(jìn)程可接受的連接數(shù)
}
http {
??? include?????? mime.types;????????\\文件擴(kuò)展名與文件類型映射表
??? default_type? application/octet-stream;?? \\默認(rèn)文件類型
??? charset utf-8;??????????????????????????? \\站點(diǎn)頁面文件使用的默認(rèn)字符編碼 #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;????? \\防止網(wǎng)絡(luò)阻塞
??? tcp_nodelay???? on;?????? \\防止延遲 #keepalive_timeout? 0;
??? keepalive_timeout? 65;?? \\超時(shí)時(shí)間 #gzip? on; #第一虛擬主機(jī)配置 server {
??????? listen?????? 80;???????????????? \\監(jiān)聽端口
??????? server_name? www.sjzz.com;???????????? \\站點(diǎn)的FQDN名稱 #charset koi8-r; #access_log? logs/host.access.log? main; location / {
??????????? root?? /var/www/sjzz;???????? \\網(wǎng)站的根目錄
??????????? 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 {?????????????????//站點(diǎn)狀態(tài)統(tǒng)計(jì)
???? 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;
??????? #}
??? } #第二個(gè)虛擬主機(jī)配置
??? # another virtual host using mix of IP-, name-, and port-based configuration
??? #
??? server {
???????? listen?????? 80;?????????\\監(jiān)聽端口
??? #??? listen?????? somename:8080;
???????? server_name? www.linux5234.com;????? \\站點(diǎn)的FQDN名稱 location / {
???????????? root?? /var/www/linux5234;????????? \\網(wǎng)站的根目錄
???????????? index? index.html index.htm;
???????? }
?
? location /Status {????????//站點(diǎn)狀態(tài)統(tǒng)計(jì)
????? 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服務(wù): [root@localhost~]# service nginx restart 3.建立虛擬主機(jī)對應(yīng)網(wǎng)頁目錄及測試頁面: [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.在客戶瀏覽器中訪問不同的虛擬主機(jī): 四.配置站點(diǎn)狀態(tài)統(tǒng)計(jì) 1.修改nginx.conf文件,在server{........}配置部分分別添加如下配置項(xiàng): location /Status {
???? stub_status on;????????????\\啟用狀態(tài)統(tǒng)計(jì)模塊
???? access_log off;???????????? \\關(guān)閉日志記錄
?}
2.重啟nginx服務(wù) [root@localhost~]# service nginx restart 3.在客戶端瀏覽器中訪問“http://www.sjzz.com/Status”和“http://www.sjzz.com/Status”, 即可看到站點(diǎn)的狀態(tài)統(tǒng)計(jì)信息: 五.配置FastCGI方式支持的PHP頁面 使用RHEL5系統(tǒng)自帶的php軟件包時(shí),php-cgi工具由php-cli-5.1.6-23.2.el5_3 提供,位于/usr/bin/php-cgi。若使用源碼包編譯安裝php環(huán)境,需要在“./configure”時(shí)添加“--enable-cgi”選項(xiàng),同進(jìn)去掉“--with-apxs2”選項(xiàng),否則可能無法編譯出php-cgi程序。本文采用系統(tǒng)默認(rèn)安裝php環(huán)境。 可以使用rpm -qa |grep php查看php-cli-5.1.6-23.2.el5_3是否安裝,如果沒有安裝,就立即掛載光盤進(jìn)行安裝。 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個(gè)php-cgi子進(jìn)程,在127.0.0.1的9000端口監(jiān)聽服務(wù) 3.查看spawn-fcgi運(yùn)行狀態(tài): 使用ps aux |grep php-cgi查看進(jìn)程: 4.如果需要在每次開機(jī)后都運(yùn)行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服務(wù): [root@localhost~]# service nginx restart 6.建立php網(wǎng)頁目錄和測試頁面: [root@localhost~]#?mkdir /var/www/php [root@localhost~]# vi /var/www/php/index.php <?php echo "PHP is OK!"; phpinfo{}; ?> 7.在客戶瀏覽器中進(jìn)行驗(yàn)證(成功):
轉(zhuǎn)載于:https://blog.51cto.com/kk5234/514578
總結(jié)
以上是生活随笔為你收集整理的构建基于Nginx的web服务器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用8位字节的编码格式将字节流安全的转换
- 下一篇: 毕业后的第一个五年