[nginx學習之道]linux的nginx安裝

準備:首先要安裝下一些gcc庫用於編譯 和一些nginx的擴展lib包:linux

[root@localhost nginx-1.8.1]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel

1.首先去nginx的官網選擇要安裝的nginx版本並複製其下載的url,並在linux用wget進行下載,tar解壓和進入安裝的源碼目錄:nginx

[root@localhost ~]# wget -c http://nginx.org/download/nginx-1.8.1.tar.gz && tar zxvf ./nginx-1.8.1.tar.gz && cd ./nginx-1.8.1

而後根據本身的狀況以及./configure --help命令查看下一些主要的配置選項:而後進行配置make三部中的一部./configurec++

[root@localhost nginx-1.8.1]# ./configure \
 --prefix=/usr/local/nginx \
 --sbin-path=/usr/local/nginx/sbin/ \
 --conf-path=/usr/local/nginx/conf/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --pid-path=/var/run/nginx/nginx.pid \
 --lock-path=/var/lock/nginx.lock \
 --user=nginx \
 --group=nginx \
 --with-http_ssl_module \
 --with-http_flv_module \
 --with-http_gzip_static_module \
 --http-log-path=/var/log/nginx/access.log \
 --http-client-body-temp-path=/var/tmp/nginx/cilent/ \
 --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
 --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

遇到的一些問題:服務器

問題1:(沒有安裝gcc)測試

checking for OS
 + Linux 3.10.0-327.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found
解決:
[root@localhost nginx-1.8.1]# yum -y install gcc gcc-c++ autoconf automake

問題2:(沒有安裝一些庫,由於在./configure中我用到了 例如ssl等等這個根據本身的實際需求進行安裝)ui

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解決:
[root@localhost nginx-1.8.1]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

好接下來用make 去嘗試編譯下檢測下是否有問題(不少時候咱們習慣用 make && make install直接進編譯安裝,那是由於我在下面已經作了測試沒有問題在搭建生產環境的時候爲了節約時間才這樣作的)url

[root@localhost nginx-1.8.1]# make

若是沒有出現error的字眼就說明make檢測沒有問題。這時候運行make install就會不多有錯誤。spa

[root@localhost nginx-1.8.1]# make install

Ok到此已經安裝完成:code

這個時候我嘗試啓動下:(根據我上面的./configure 的配置sbin-path是在/usr/local/nginx/sbin/nginx 個人默認配置文件在/usr/local/nginx/conf/nginx.conf)blog

[root@localhost nginx-1.8.1]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] getpwnam("nginx") failed

上面錯誤提示我沒有nginx這個用戶和主:兩種方式1.將nginx.conf中的user和group修改爲已經存在的其餘用戶。不建議用root;2.建立我指定的用戶和組命令以下:

[root@localhost nginx-1.8.1]# /usr/sbin/groupadd -f nginx
[root@localhost nginx-1.8.1]# /usr/sbin/useradd -g nginx nginx

在嘗試啓動還有錯誤提示:

[root@localhost nginx-1.8.1]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] mkdir() "/var/tmp/nginx/cilent/" failed (2: No such file or directory)
[root@localhost nginx-1.8.1]# mkdir /var/tmp/nginx/cilent
mkdir: 沒法建立目錄"/var/tmp/nginx/cilent": 沒有那個文件或目錄

解決辦法建立:

[root@localhost nginx-1.8.1]# mkdir -p /var/tmp/nginx/cilent

而後啓動:

[root@localhost nginx-1.8.1]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

OK沒有報錯:確認是否啓動能夠用ps -ef | grep nginx去檢索下(這裏有個技巧,通常nginx的啓動都是這種形式,若是你的linux服務器上有不少的nginx.conf這個命令能夠幫你更快的鎖定如今用的配置文件。)

[root@localhost nginx-1.8.1]# ps -ef | grep nginx
root      42388      1  0 15:58 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     42389  42388  0 15:58 ?        00:00:00 nginx: worker process
root      42391  10803  0 15:58 pts/1    00:00:00 grep --color=auto nginx

解釋下上面的參數   master process 是主進程的意思  pid是 42388 下面的是子進程。

ok這個進程咱們其實還能夠去一個文件中看到,對就是咱們在./configure 的配置的pid文件:cat下這個文件會看到裏面已經有了對應的pid號:

[root@localhost nginx-1.8.1]# cat /var/run/nginx/nginx.pid 
42388

這個pid的做用可讓咱們在操做nginx和方便:好比說重啓 或者中止服務(kill) 或者平滑重啓。

從容中止nginx:
kill - QUIT `/var/run/nginx/nginx.pid`
快速中止:
kill - TERM `/var/run/nginx/nginx.pid`
強制中止:
kill - 9 `/var/run/nginx/nginx.pid`
以上均可以直接指定pid號例如
kill - QUIT 42388
相關文章
相關標籤/搜索