Nginx服務(wù)器的安裝和卸載

Nginx的安裝(Linux版本的安裝)

1. 環(huán)境準(zhǔn)備

為了編譯Nginx源代碼,我們需要標(biāo)準(zhǔn)的GCC編譯器。GCC的全稱為GNUCompiler Collection,其由GNU開(kāi)發(fā),并以GPL及LGPL許可證發(fā)行,是自由的類UNIX及蘋果電腦Mac OS X操作系統(tǒng)的標(biāo)準(zhǔn)編譯器。因?yàn)镚CC原本只能處理C語(yǔ)言,所以原名為GNU C語(yǔ)言編譯器,后來(lái)得到快速擴(kuò)展,可處理C++、Fortran、Pascal、Objective-C、Java以及Ada等其他語(yǔ)言。

除此之外,我們還需要Automake工具,以完成自動(dòng)創(chuàng)建Makefile的工作。

由于Nginx的一些模塊還需要依賴其他第三方庫(kù),通常有pcre庫(kù)(支持rewrite模塊)、zlib庫(kù)(支持gzip模塊)和openssl庫(kù)(支持ssl模塊)等。所以在編譯Nginx源代碼前還需要安裝這些這些庫(kù)。

#gcc安裝
yum -y install gcc-c++
#pcre安裝
yum  -y install  pcre pcre-devel
#zlib安裝
yum -y install zlib zlib-devel
#OpenSSL安裝
yum -y install openssl openssl-devel

前面3個(gè)就不說(shuō)了,很多軟件都會(huì)依賴到的包。需要安裝OpenSSL是因?yàn)楹笃趎ginx可能需要配置https,因此最好提前準(zhǔn)備好。

在進(jìn)行上面的安裝之前,最好使用下面的命令看下這些軟件包是否已經(jīng)安裝過(guò)了。

yum list installed | grep ***

到此安裝環(huán)境就準(zhǔn)備好了。

2. 源代碼編譯

Nginx的編譯安裝很簡(jiǎn)單,在Linux下建議下載tar包進(jìn)行安裝。

[root@localhost ~]# cd

#下載tar包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
#解壓tar包
[root@localhost ~]# tar -zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2
#配置安裝路徑等其他配置,默認(rèn)安裝目錄是/usr/local/nginx
[root@localhost nginx-1.12.2]# ./configure 
checking for OS
 + Linux 3.10.0-957.21.3.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
.....
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@localhost nginx-1.12.2]# make 
....
objs/src/http/modules/ngx_http_upstream_least_conn_module.o \
objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
        -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/root/nginx-1.12.2'

[root@localhost nginx-1.12.2]# make install

        '/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \
        || cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
        '/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
        || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/root/nginx-1.12.2'
#查看版本
[root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.12.2

至此,Nginx的安裝就已經(jīng)結(jié)束了。其實(shí)在上面的configure命令執(zhí)行時(shí)我們是可以配置很多參數(shù)的,上面的安裝中我們?nèi)渴褂玫氖荖ginx的默認(rèn)配置。下面列舉幾個(gè)configure命令的常見(jiàn)配置,比較完整的配置項(xiàng)的中文含義請(qǐng)參見(jiàn)這篇博客。

配置項(xiàng) 含義
--prefix=PATH 定義服務(wù)器文件的完整路徑,該路徑同時(shí)也是configure命令設(shè)置的 相對(duì)路徑(除類庫(kù)源文件外)以及nginx.conf文件定義的相對(duì)路徑的基準(zhǔn)路徑。其默認(rèn) 值是/usr/local/nginx。
--sbin-path=PATH 設(shè)置nginx可執(zhí)行文件的完整路徑,該路徑僅在安裝期間使用, 默認(rèn)路徑為*prefix*/sbin/nginx。
--conf-path=PATH 設(shè)置配置文件nginx.conf的完整路徑。如有必要,總是可以 在nginx啟動(dòng)時(shí)通過(guò)命令行參數(shù)-cfile指定一個(gè)不同的配置文件路徑。 默認(rèn)路徑為*prefix*/conf/nginx.conf。
--error-log-path=PATH 設(shè)置記錄主要錯(cuò)誤、警告以及調(diào)試信息日志的完整路徑。安裝完成后, 該路徑總是可以在nginx.conf文件中用 error_log 指令來(lái)修改。 默認(rèn)路徑為*prefix*/logs/error.log
--pid-path=PATH 設(shè)置nginx.pid文件的完整路徑,該文件存儲(chǔ)了主進(jìn)程的進(jìn)程ID。安裝完成后, 該路徑總是可以在nginx.conf文件中用 pid指令來(lái)修改。 默認(rèn)路徑為*prefix*/logs/nginx.pid。

Configure命令還有很多配置參數(shù),可以通過(guò)./configure --help查看。其中:

  • with開(kāi)頭的表示該模塊默認(rèn)是未開(kāi)啟的,可以使用--with開(kāi)啟。
  • without開(kāi)頭的表示該模塊默認(rèn)是啟用的,可以使用--without禁用。
  • 第三方模塊使用--add-module=PATH添加。如果支持動(dòng)態(tài)加載,使用--add-dynamic-module=PATH添加。

一個(gè)配置命令的列子如下:

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-4.4 --with-zlib=../zlib-1.1.3

3.一些常用命令

#使用-c可以自定義指定Nginx的配置問(wèn)價(jià),默認(rèn)的是安裝目錄下的配置
nginx -c /usr/local/nginx/conf/nginx.conf
nginx -s stop
nginx -s quit
kill -9 PID
#重新加載配置
nginx -s reload 

Nginx卸載

1.首先輸入命令 ps -ef | grep nginx檢查一下nginx服務(wù)是否在運(yùn)行。

[root@localhost /]# ps -ef |grep nginx
root       3163   2643  0 14:08 tty1     00:00:00 man nginx
root       5427      1  0 14:50 ?        00:00:00 nginx: master process nginx
nginx      5428   5427  0 14:50 ?        00:00:00 nginx: worker process
root       5532   2746  0 14:52 pts/0    00:00:00 grep --color=auto nginx

2.停止Nginx服務(wù)

[root@localhost /]# /usr/local/nginx/sbin/nginx -s stop
[root@localhost /]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      3403/redis-server 0 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1044/sshd           
tcp        0      0 0.0.0.0:8442            0.0.0.0:*               LISTEN      15788/java          
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      19829/java          
tcp6       0      0 :::3306                 :::*                    LISTEN      13523/mysqld 

3.查找、刪除Nginx相關(guān)文件

  • 查看Nginx相關(guān)文件:whereis nginx
[root@localhost /]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz /usr/share/man/man3/nginx.3pm.gz
  • find查找相關(guān)文件
[root@localhost /]# find / -name nginx
/usr/lib64/perl5/vendor_perl/auto/nginx
/usr/lib64/nginx
/usr/share/nginx
/usr/sbin/nginx
/etc/logrotate.d/nginx
/etc/nginx
/var/lib/nginx
/var/log/nginx
  • 依次刪除find查找到的所有目錄:rm -rf /usr/sbin/nginx

4.再使用yum清理

[root@localhost /]# yum remove nginx
依賴關(guān)系解決

======================================================================================================
 Package                              架構(gòu)            版本                       源              大小
======================================================================================================
正在刪除:
 nginx                                x86_64          1:1.12.2-3.el7             @epel          1.5 M
為依賴而移除:
 nginx-all-modules                    noarch          1:1.12.2-3.el7             @epel          0.0  
 nginx-mod-http-geoip                 x86_64          1:1.12.2-3.el7             @epel           21 k
 nginx-mod-http-image-filter          x86_64          1:1.12.2-3.el7             @epel           24 k
 nginx-mod-http-perl                  x86_64          1:1.12.2-3.el7             @epel           54 k
 nginx-mod-http-xslt-filter           x86_64          1:1.12.2-3.el7             @epel           24 k
 nginx-mod-mail                       x86_64          1:1.12.2-3.el7             @epel           99 k
 nginx-mod-stream                     x86_64          1:1.12.2-3.el7             @epel          157 k

事務(wù)概要
======================================================================================================
移除  1 軟件包 (+7 依賴軟件包)

安裝大?。?.9 M
是否繼續(xù)?[y/N]:

Nginx 配置

創(chuàng)建 Nginx 運(yùn)行使用的用戶 www:

[root@ localhost ~]# /usr/sbin/groupadd www 
[root@ localhost ~]# /usr/sbin/useradd -g www www

配置nginx.conf ,將/usr/local/nginx/conf/nginx.conf替換為以下內(nèi)容

user www www;
worker_processes 2; #設(shè)置值和CPU核心數(shù)一致
error_log /usr/local/nginx/logs/error.log error; #日志位置和日志級(jí)別
#錯(cuò)誤日志級(jí)別有:
#   debug    調(diào)試,將記錄詳細(xì)的大量調(diào)試信息,適合開(kāi)發(fā)人員開(kāi)啟
#   info     信息,記錄更多的通知信息,不重要的
#   notice   通知,記錄通知信息,不重要的
#   warn     警告,記錄警告信息
#   error    錯(cuò)誤,記錄錯(cuò)誤信息
#   crit     嚴(yán)重,只記錄非常嚴(yán)重的錯(cuò)誤信息

pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

#==事件配置==#
events
{
  #use epoll;
  worker_connections 65535;
#設(shè)置每個(gè)Worker進(jìn)程可處理的并發(fā)連接數(shù)量,可根據(jù)需求合理配置此值。建議
#與"worker_rlimit_nofile"指令的值一致或更小。
}

#==核心配置==#
http
{
  include mime.types;
  default_type application/octet-stream;
  types_hash_max_size 2048; #設(shè)置類型哈希表的大小,單位為字節(jié)。用于將MIME類型的數(shù)據(jù)通過(guò)哈希后緩存到內(nèi)存中,以提高對(duì)MIME類型映射表的讀取效率。

  log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
  #定義日志格式,“main”表示該日志格式名,用于下方“access_log”訪問(wèn)日志中調(diào)用
    #   $remote_addr        遠(yuǎn)程訪問(wèn)地址
    #   $remote_user        遠(yuǎn)程訪問(wèn)用戶
    #   $time_local     訪問(wèn)時(shí)間
    #   $request        請(qǐng)求的URL與HTTP協(xié)議
    #   $status         請(qǐng)求狀態(tài),成功為200
    #   $body_bytes_sent    發(fā)送給客戶端文件主機(jī)內(nèi)容大小
    #   $http_referer       從哪個(gè)頁(yè)面連接訪問(wèn)過(guò)來(lái)的
    #   $http_user_agent    客戶端瀏覽器的相關(guān)信息
    #   $http_x_forwarded_for   遠(yuǎn)程訪問(wèn)地址,與remote_addr相同
  
  charset UTF-8; #設(shè)置字符集編碼,防止NGINX對(duì)于中文返回的顯示亂碼。
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;#啟用或禁用"sendfile()"函數(shù)的調(diào)用,"on" or "off","on"表示啟用,"off"表示禁用。當(dāng)線程響應(yīng)時(shí),"sendfile()"函數(shù)會(huì)報(bào)告線程數(shù)據(jù)不在內(nèi)存中而是在硬盤中,則線程直接去硬盤拿到響應(yīng)數(shù)據(jù)直接傳送給用戶,而無(wú)需調(diào)用內(nèi)存去硬盤拿響應(yīng)數(shù)據(jù),省去了調(diào)用內(nèi)存的步驟,在小型WEB項(xiàng)目中可以提高請(qǐng)求響應(yīng)效率,若是重量級(jí)WEB項(xiàng)目,為了平衡磁盤IO則不建議開(kāi)啟。我們也可以將此指令稱之為高效傳輸模式。
  tcp_nopush on;#啟用或禁用TCP_NOPUSH套接字選項(xiàng),"on" or "off","on"表示啟用,啟用此項(xiàng)的前提是必須開(kāi)啟"sendfile","off"表示禁用。有時(shí)候在傳輸一個(gè)響應(yīng)數(shù)據(jù)時(shí),可能會(huì)產(chǎn)生多個(gè)小塊數(shù)據(jù)包傳出,可能這個(gè)小塊數(shù)據(jù)包頭部大小為30字節(jié),而真正數(shù)據(jù)信息只有1字節(jié),在高并發(fā)環(huán)境下會(huì)導(dǎo)致網(wǎng)絡(luò)擁塞、帶寬不夠用問(wèn)題。開(kāi)啟此項(xiàng)則傳出的數(shù)據(jù)包會(huì)積累一下在傳出,可以防止網(wǎng)絡(luò)擁塞,減少帶寬的占用。
  keepalive_timeout 60;#設(shè)置保持客戶端連接活躍狀態(tài)的超時(shí)時(shí)間,單位為秒。
  send_timeout 60s;#設(shè)置服務(wù)器將響應(yīng)發(fā)送給客戶端的超時(shí)時(shí)間,單位為秒。  
  tcp_nodelay on;#啟用或禁用TCP_NODELAY套接字選項(xiàng),"on" or "off","on"表示啟用,"off"表示禁用。此項(xiàng)與"tcp_nopush"的功能剛好相反,若開(kāi)啟此項(xiàng)則對(duì)于小塊數(shù)據(jù)包不等待立即傳輸,有時(shí)候一個(gè)WEB應(yīng)用期望發(fā)送小塊數(shù)據(jù)時(shí),則建議開(kāi)啟,當(dāng)“tcp_nopush”和“tcp_nodelay”同時(shí)開(kāi)啟時(shí),NGINX會(huì)平衡這兩個(gè)功能的使用。
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  #include /etc/nginx/conf.d/*.conf;
  #設(shè)置還包含其他配置文件,我們可以將一些其他配置分離到另外一個(gè)文件中處理,避免主配置文件因?yàn)榕渲锰?  #多導(dǎo)致混亂不方便管理,比如下面的"server {}",在NGINX中我們可以配置多個(gè)"server {}"則我們可以將
  #每個(gè)"server {}"分離到另外一個(gè)配置文件中,即一個(gè)配置文件對(duì)應(yīng)一個(gè)WEB站點(diǎn)。
 
  #limit_zone crawler $binary_remote_addr 10m;

#==WEB站點(diǎn)配置==#
 server
  {
    listen 80;#監(jiān)聽(tīng)端口
    server_name xxx.test.com;#設(shè)置域名綁定,綁定一個(gè)域名。
    index index.html index.htm index.php;
    root /usr/local/webserver/nginx/html;#站點(diǎn)目錄
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }

}

nginx基礎(chǔ)命令:

nginx -t                         # 查看nginx狀態(tài)

nginx -s reload            # 重新載入配置文件

nginx -s reopen           # 重啟 Nginx

nginx -s stop               # 停止 Nginx

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容