目錄
官方安裝手冊:https://nginx.org/en/docs/install.html
針對不同的操作系統平台,nginx支持不同的安裝方式,如:通過包管理器安裝,從源碼編譯安裝。
通過包管理器安裝
在Linux系統上安裝nginx時,最方便的方式是通過包管理器進行安裝,如:yum,apt。
針對不同的Linux系統,在執行nginx安裝之前,需要進行一定的配置,詳見:https://nginx.org/en/linux_packages.html 。
如下以在CentOS上通過yum方式安裝nginx為例進行說明。
安裝nginx
第一步:安裝必要的基礎工具“yum-utils”
$ sudo yum install yum-utils
第二步:在文件“/etc/yum.repos.d/nginx.repo”中配置nginx的yum源
$ sudo vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
第三步:通過yum安裝nginx
$ sudo yum install nginx
特別說明: 默認情況下,通過“sudo yum install nginx”安裝的是穩定版本的nginx,如果希望安裝最新版本的nginx可以使用如下命令:
$ sudo yum-config-manager --enable nginx-mainline
安裝完畢之后默認是不會啟動nginx的:
$ ps uax |grep nginx
root 11455 0.0 0.0 112708 988 pts/0 S+ 17:00 0:00 grep --color=auto nginx
通過這種方式安裝nginx完畢之后,nginx可執行文件默認已經添加到/usr/sbin目錄下,可以直接執行nginx命令啟動nginx。
$ whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
# 啟動nginx
$ nginx
$ ps uax |grep nginx
root 11457 0.0 0.0 46440 980 ? Ss 17:01 0:00 nginx: master process nginx
nginx 11458 0.0 0.1 46852 1932 ? S 17:01 0:00 nginx: worker process
root 11473 0.0 0.0 112708 988 pts/0 S+ 17:01 0:00 grep --color=auto nginx
使用不帶參數的命令行選項”-t“查看nginx進程當前使用的配置文件:
# 使用命令行選項-t查看nginx進程當前使用的配置文件
# 默認情況下nginx使用的配置文件是:/etc/nginx/nginx.conf
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
通過命令行選項“-s”停止nginx:
# 停止nginx
$ nginx -s stop
$ ps uax |grep nginx
root 11502 0.0 0.0 112708 988 pts/0 S+ 17:10 0:00 grep --color=auto nginx
卸載nginx
不同Linux發行版的包管理器卸載軟件的方式各不相同,如下以在CentOS上通過yum卸載軟件進行說明。
通過yum安裝的nginx,卸載時非常方便,只需要通過yum命令即可完成nginx的卸載。
# 卸載之前查看下nginx的安裝情況
$ which nginx
/usr/sbin/nginx
# 通過yum命令刪除nginx
$ yum remove nginx
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.16.1-1.el7.ngx will be erased
--> Finished Dependency Resolution
Dependencies Resolved
......
Removed:
nginx.x86_64 1:1.16.1-1.el7.ngx
Complete!
# 卸載完成之后再次查看nginx的安裝情況
# 顯然,卸載nginx之后就找不到nginx的安裝信息了
$ which nginx
/usr/bin/which: no nginx in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
值得注意的是,通過yum命令卸載nginx,僅僅是卸載了nginx軟件本身,對於之前使用nginx配置文件將會被保留下來。
# 顯然,卸載nginx之后之前使用的配置文件被保留了,並且換了一個文件名后綴表明這是通過yum卸載時保存的配置文件
$ ll /etc/nginx/
total 4
drwxr-xr-x. 2 root root 51 Nov 16 12:57 conf.d
-rw-r--r--. 1 root root 1025 Nov 14 23:40 nginx.conf.rpmsave
$ ll /etc/nginx/conf.d/
total 8
-rw-r--r--. 1 root root 1098 Nov 9 17:55 default.conf.rpmsave
可以手動刪除之前使用的nginx配置文件:
$ rm -rf /etc/nginx
從源碼編譯安裝
nginx是C語言開發的應用程序,支持從源碼編譯安裝,這也是nginx常用的安裝方式之一。
如下是在Linux環境(CentOS 7.6)下編譯安裝nginx的步驟:
准備安裝環境
在進行nginx編譯之前需要安裝一些基礎的工具環境,如:gcc,openssl等。
# 安裝gcc
$ yum install -y gcc-c++
# 安裝prce
# PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。
# 注:pcre-devel是使用pcre開發的一個二次開發庫,nginx也需要此庫。
$ yum install -y pcre pcre-devel
# 安裝zlib
# zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
$ yum install -y zlib zlib-devel
# 安裝openssl
# OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。
# nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
$ yum install -y openssl openssl-devel
執行編譯安裝
nginx源碼文件下載:https://nginx.org/en/download.html
# 下載指定版本的nginx源碼包
$ wget http://nginx.org/download/nginx-1.16.1.tar.gz
$ tar xzvf nginx-1.16.1.tar.gz
$ cd nginx-1.16.1
$ ./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
$ make
$ make install
執行上述編譯安裝之后,nginx將會被安裝到目錄“/usr/local/nginx”下。
$ cd /usr/local/nginx/
$ ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
$ ./sbin/nginx
$ ps uax |grep nginx
root 3975 0.0 0.0 20556 620 ? Ss 21:33 0:00 nginx: master process ./sbin/nginx
nobody 3976 0.0 0.1 21008 1576 ? S 21:33 0:00 nginx: worker process
root 3996 0.0 0.0 112708 988 pts/0 R+ 21:36 0:00 grep --color=auto nginx
此時使用的配置文件默認為“/usr/local/nginx/conf/nginx.conf”,進入到“/usr/local/nginx”,執行“./sbin/nginx”即可啟動nginx。
注意:
上邊將臨時文件目錄指定為/var/temp/nginx,在啟動nginx之前需要在/var下創建temp及nginx目錄(mkdir -p /var/temp/nginx
),否則將會報錯:“nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)”。
編譯安裝的nginx卸載非常方便,直接將nginx的安裝目錄“/usr/local/nginx”刪除即可,同時可以把nginx使用的日志目錄和臨時目錄一並刪除。
刪除編譯安裝的nginx:
$ rm -rf /usr/local/nginx
$ rm -rf /var/log/nginx
$ rm -rf /var/temp/nginx
制作nginx免安裝包
有時候需要在內網環境使用nginx,如果內網無法訪問外網,那么就無法通過包管理器工具安裝nginx了。
而使用源碼編譯安裝的方式同樣需要先安裝一些依賴工具包,這又將是一個非常繁瑣的過程。
因此,如果有一個已經編譯好的nginx二進制安裝包,那該多么方便啊!遺憾的是,nginx官方並沒有為Linux平台提供這樣的二進制包,只提供了一個Windows平台二進制包。
實際上,可以通過源碼編譯方式制作一個免安裝的nginx二進制包,這樣就可以非常方便地使用nginx了。
如下是制作nginx免安裝二進制包的詳細步驟:
# 安裝基礎的依賴工具
$ yum -y install gcc-c++ make wget
# 下載nginx模塊依賴的軟件包和nginx源碼包,並解壓
$ wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
$ wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
$ wget https://zlib.net/zlib-1.2.11.tar.gz
$ wget http://nginx.org/download/nginx-1.16.1.tar.gz
# 開始編譯nginx
$ cd nginx-1.16.1
$ ./configure \
--with-openssl=../openssl-1.0.2s \
--with-pcre=../pcre-8.43 \
--with-zlib=../zlib-1.2.11 \
--with-pcre-jit --user=root \
--prefix=/root/nginx \
--with-http_ssl_module \
--with-http_v2_module
# 輸出如下信息說明編譯配置沒問題
Configuration summary
+ using PCRE library: ../pcre-8.43
+ using OpenSSL library: ../openssl-1.0.2s
+ using zlib library: ../zlib-1.2.11
nginx path prefix: "/root/nginx"
nginx binary file: "/root/nginx/sbin/nginx"
nginx modules path: "/root/nginx/modules"
nginx configuration prefix: "/root/nginx/conf"
nginx configuration file: "/root/nginx/conf/nginx.conf"
nginx pid file: "/root/nginx/logs/nginx.pid"
nginx error log file: "/root/nginx/logs/error.log"
nginx http access log file: "/root/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"
$ make
$ make install
如上所示,編譯之后nginx將會被安裝到“/root/nginx”路徑下。
[root@localhost ~]# pwd
/root
[root@localhost ~]# ll nginx
total 4
drwxr-xr-x. 2 root root 4096 Nov 16 16:46 conf
drwxr-xr-x. 2 root root 40 Nov 16 16:46 html
drwxr-xr-x. 2 root root 6 Nov 16 16:46 logs
drwxr-xr-x. 2 root root 19 Nov 16 16:46 sbin
nginx的二進制文件在sbin目錄下,配置文件在conf目錄下。
將編譯之后的整個nginx目錄進行打包:tar czvf nginx.tar.gz nginx
,打包之后的“nginx.tar.gz”就可以作為免安裝的nginx二進制包進行使用。
將“nginx.tar.gz”拷貝到目標主機,解壓之后就可以使用。
$ tar xzvf nginx.tar.gz
$ cd nginx
# 啟動nginx,默認使用的配置文件為conf目錄下的nginx.conf
$ ./sbin/nginx
關於nginx免安裝包的制作,可以直接使用腳本工具,已經在Ubuntu和CentOS上驗證過。
$ git clone https://github.com/nuccch/nginx-portable
$ cd nginx-portable
$ bash compile <nginx-version>
執行完畢之后將會在build目錄下生成一個已經編譯好的nginx文件:nginx-<version>.tar.gz
,直接拷貝到目標主機解壓即可使用。
【參考】
https://nginx.org/en/linux_packages.html nginx: Linux packages
https://blog.csdn.net/fdipzone/article/details/77199042 nginx快速查看配置文件的方法
https://nginx.org/en/docs/switches.html Nginx Command-line parameters
https://mp.weixin.qq.com/s/SctV1U_IXfHKpChT4gvaFg Nginx 的介紹及安裝
https://juejin.im/post/5d31ec3b5188253a8f27ef5b Linux安裝Nginx正確方式