在Centos上编译安装nginx
實(shí)驗(yàn)環(huán)境:
????OS: CentOS 6.6?
?? nginx:nginx-1.6.2.tar.gz
前期準(zhǔn)備:
安裝開發(fā)包組件
[root@1inux tmp]# yum -y groupinstall "Development tools" "Server Platform Development"
[root@1inux tmp]# yum -y install pcre-devel
一、 編譯安裝:?
[root@1inux tmp]# useradd -r nginx????//添加nginx系統(tǒng)用戶
[root@1inux tmp]# tar xf nginx-1.6.2.tar.gz?
[root@1inux tmp]# cd nginx-1.6.2
[root@1inux nginx-1.6.2]# ./configure --help?? ??? ?//獲取幫助
[root@1inux nginx-1.6.2]#? mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi,uwsgi}????//創(chuàng)建編譯安裝需要的目錄
| 1 | [root@1inux?nginx-1.6.2]#?./configure?--prefix=/usr/local/nginx?--conf-path=/etc/nginx/nginx.conf?--user=nginx?--group=nginx?--error-log-path=/var/log/nginx/error.log?--http-log-path=/var/log/nginx/access.log?--pid-path=/var/run/nginx/nginx.pid?--lock-path=/var/lock/nginx.lock?--with-http_ssl_module?--with-http_stub_status_module?--with-http_gzip_static_module?--with-http_flv_module?--with-http_mp4_module?--http-client-body-temp-path=/var/tmp/nginx/client?--http-proxy-temp-path=/var/tmp/nginx/proxy?--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi?--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi |
添加path路徑
| 1 | [root@1inux?nginx]#?echo??"export?PATH=/usr/local/nginx/sbin/nginx:$PATH"?>?/etc/profile.d/nginx.sh |
加載:
| 1 | [root@1inux?nginx]#?.?/etc/profile.d/nginx.sh |
啟動(dòng)nginx
| 1 2 3 4 | [root@1inux?nginx]#?/usr/local/nginx/sbin/nginx [root@1inux?nginx]#?ss?-tunlp?|?grep?:80 tcp????LISTEN?????0??????128????????????????????*:80????????????????????*:*??????users:(("nginx",52985,6),("nginx",52986,6)) [root@1inux?nginx]# |
查看nginx啟動(dòng)進(jìn)程情況
| 1 2 3 4 5 | [root@1inux?nginx]#?ps?aux?|?grep?nginx root??????52985??0.0??0.1??45044??1064??????????Ss???03:54???0:00?nginx:?master?process?/usr/local/nginx/sbin/nginx nginx?????52986??0.0??0.1??45472??1636??????????S????03:54???0:00?nginx:?worker?process?????? root??????52991??0.0??0.0?103252???836?pts/8????S+???03:55???0:00?grep?nginx [root@1inux?nginx]# |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@1inux?nginx]#?/usr/local/nginx/sbin/nginx?-h???//查看nginx?選項(xiàng) nginx?version:?nginx/1.6.2 Usage:?nginx?[-?hvVtq]?[-s?signal]?[-c?filename]?[-p?prefix]?[-g?directives] Options: ??-?,-h?????????:?this?help ??-v????????????:?show?version?and?exit ??-V????????????:?show?version?and?configure?options?then?exit ??-t????????????:?test?configuration?and?exit ??-q????????????:?suppress?non-error?messages?during?configuration?testing ??-s?signal?????:?send?signal?to?a?master?process:?stop,?quit,?reopen,?reload ??-p?prefix?????:?set?prefix?path?(default:?/usr/local/nginx/) ??-c?filename???:?set?configuration?file?(default:?/etc/nginx/nginx.conf) ??-g?directives?:?set?global?directives?out?of?configuration?file |
常用配置指令:
?? ?server {
?? ??? ??? ??? ?}? //定義一個(gè)虛擬主機(jī)
?? ??? ??? ??? ?
?? ?2、listen?? //定義監(jiān)聽端口
?? ??? ??? ??? ?listen address[:port];
?? ??? ??? ??? ?listen port;
?? ??? ??? ??? ?
?? ?3、server_name? NAME [...];?? ??? ? 【定義服務(wù)器主機(jī)名----只能用在server中】?? ??? ?
?? ??? ??? ?后可跟多個(gè)主機(jī)名:名稱還可以使用正則表達(dá)式(~)或通配符,檢查標(biāo)準(zhǔn)如下
?? ??? ??? ??? ??? ?(1)先做精確匹配檢查;
?? ??? ??? ??? ??? ?(2)左側(cè)通配符匹配檢查;*.1inux.com?
?? ??? ??? ??? ??? ?(3) 右側(cè)通配符匹配檢查;如 mail.* ?
?? ??? ??? ??? ??? ?(4) 正則表達(dá)式匹配檢查:如 ~^.*\.1inux.com\.com$
?? ??? ??? ??? ??? ?(5) default_server;
?? ??? ??? ??? ?
--------------------------------------------------------
編輯配置文件? 在http {} 中添加如下:
??????? server {
??????????????? listen 888;
??????????????? server_name 888.1inux.com;
??????????????? root "/vhost/888/html/";
??????? }
重新加載
[root@1inux nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@1inux nginx]# ss -tnlp | grep nginx
LISTEN???? 0????? 128?????????????????????? *:888????????????????????? *:*????? users:(("nginx",6568,11),("nginx",7659,11))
LISTEN???? 0????? 128?????????????????????? *:80?????????????????????? *:*????? users:(("nginx",6568,6),("nginx",7659,6))
[root@1inux nginx]#?
添加主頁(yè)面?
?# echo "<h1> This is 888.1inux.com </h1>" > /vhost/888/html/index.html
然后訪問(wèn)如圖
--------------------------------------------------------
?? ?4、root path;
?? ??? ??? ?設(shè)置資源路徑映射;用于指明請(qǐng)求的URL所對(duì)應(yīng)的資源所在的文件系統(tǒng)上的起始路徑;
?? ??? ??? ??? ?【其使用范圍:http, server, location,?? if in location ?? ??? ??? ??? ?location 內(nèi)的 優(yōu)先級(jí)高于server】
?? ?5、location [? = | ~ | ~* | ^~ ] uri { ... }?
?? ??? ??? ??? ?location @name { ... }?
?? ??? ??? ?使用范圍: server, location?
?? ??? ??? ??? ?
?? ??? ??? ?功能:允許根據(jù)用戶請(qǐng)求的URL來(lái)匹配定義的各location; 匹配到時(shí),此請(qǐng)求將被響應(yīng)的location配置塊中的配置所處理,例如做訪問(wèn)控制等功能
?? ??? ??? ??? ?
?? ??? ??? ??? ?= : 精確匹配檢查;
?? ??? ??? ??? ?^~: RUI的前半部分匹配,不支持正則表達(dá)式;
?? ??? ??? ??? ?~ : 正則表達(dá)式模式匹配檢查,區(qū)分字符大小寫;
?? ??? ??? ??? ?~*: 正則表達(dá)式模塊匹配檢查,不區(qū)分字符大小寫;
?? ?匹配的優(yōu)先級(jí):?? 精確匹配(=)?? ^~???? ~??? ~*?? 不帶任何符號(hào)的location;
-------------------------
eg:
創(chuàng)建目錄及文件?
[root@1inux /]# mkdir /vhost/{www,images/img} -pv
mkdir: created directory `/vhost'
mkdir: created directory `/vhost/www'
mkdir: created directory `/vhost/images'
mkdir: created directory `/vhost/images/img'
[root@1inux /]# echo "<h1> This is www.1inux.com </h1>" >/vhost/www/index.html
[root@1inux vhost]# tree /vhost
/vhost
|-- images
|?? `-- img
|?????? |-- 1.jpg
|?????? |-- 2.jpg
|?????? `-- mylinux2.jpg
`-- www
??? `-- index.html
在配置文件中添加如下:
| 1 2 3 4 5 6 7 8 9 10 | ????????server?{ ????????????????listen?888; ????????????????server_name?www.1inux.com; ????????????????location?/?{ ????????????????????????root?"/vhost/www/"; ????????????????????????} ????????????????location?/img/?{ ????????????????????????root?"/vhost/images/"; ????????????????????????} ????????} |
?????? ?
| 1 2 3 4 5 | [root@1inux?nginx]#?/usr/local/nginx/sbin/nginx?-s?reload [root@1inux?nginx]#?ss?-tnlp?|?grep?"nginx" LISTEN?????0??????128???????????????????????*:888??????????????????????*:*??????users:(("nginx",18499,11),("nginx",18803,11)) LISTEN?????0??????128???????????????????????*:80???????????????????????*:*??????users:(("nginx",18499,6),("nginx",18803,6)) [root@1inux?nginx]# |
注意 此時(shí) 使用root? 定義? 訪問(wèn) http://www.1inux.com:888/img/1.jpg? 實(shí)際訪問(wèn)的Web服務(wù)器路徑是:/vhost/images/目錄下的/img/1.jpg
--------------------------------
?? ?6、alias path;
?? ??? ?用于location配置段,定義路徑別名
??????????????? location /img/ {
??????????????????????? root "/vhost/images/";
??????????????????????? }
?????????????????????? ?
??????? //http://www.1inux.com:888/img/2.jpg?? ??? ?====》? /vhost/images/img/2.jpg?
??????? //即? 訪問(wèn)路徑中的/img/對(duì)應(yīng)的是?? Web本地/vhost/images/目錄下的目錄
?????????????????????? ?
??????????????? location /pic/ {
??????????????????????? alias "/vhost/picture/";
??????????????????????? }
?? ??? ??? ??? ?//http://www.1inux.com:888/pic/2.jpg??????? ====》? /vhost/picture/2.jpg
?? ??? ??? ??? ?//即? 訪問(wèn)路徑中的/pic/目錄對(duì)應(yīng)的是?? Web本地/vhost/picture/目錄
?? ?7、index file;
?? ??? ??? ??? ?默認(rèn)主頁(yè)面;
?? ??? ??? ??? ??? ?index index.php index.html;
本文轉(zhuǎn)自 1inux 51CTO博客,原文鏈接:http://blog.51cto.com/1inux/1659050
總結(jié)
以上是生活随笔為你收集整理的在Centos上编译安装nginx的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Nis服务器主从安装配置
- 下一篇: Centos(6、7) —— yum源的