Linux下nginx的yum安裝、原始碼安裝、OpenResty的原始碼安裝

2020-10-11 00:00:06

Linux下nginx的安裝

安裝環境:

# cat /proc/version
Linux version 3.10.0-123.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) ) #1 SMP Mon Jun 30 12:09:22 UTC 2014

# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

yum安裝

  1. 設定yum倉庫地址vi /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
  1. 使用yum安裝:
# yum install -y nginx

安裝完成後組態檔所在目錄為/etc/nginx

  1. nginx的啟動與停止:
  • 啟動nginx:nginxsystemctl start nginx
  • 停止nginx:nginx -s stopsystemctl stop nginx
  1. 檢視nginx是否啟動成功。
# ps -ef|grep nginx
root       1830      1  0 22:45 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      1831   1830  0 22:45 ?        00:00:00 nginx: worker process
root       1835   1638  0 22:45 pts/0    00:00:00 grep --color=auto nginx

# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-10-07 22:45:12 EDT; 22s ago
     Docs: http://nginx.org/en/docs/
  Process: 1817 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=1/FAILURE)
  Process: 1829 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nginx.service
           ├─1830 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─1831 nginx: worker process

Oct 07 22:45:12 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
Oct 07 22:45:12 localhost.localdomain systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
Oct 07 22:45:12 localhost.localdomain systemd[1]: Started nginx - high performance web server.
  1. 存取網站http://localhost/

  2. 解除安裝nginx。

# yum list nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * centos-sclo-rh: mirrors.163.com
 * centos-sclo-sclo: mirrors.ustc.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
Installed Packages
nginx.x86_64                                                                                                                 1:1.18.0-1.el7.ngx                                                                                                                  @nginx-stable

# yum remove nginx.x86_64
Loaded plugins: fastestmirror
... ...

原始碼安裝

原始碼安裝的方式方便對nginx進行自定義的設定以及安裝外掛。

  1. 下載原始碼,下載地址:http://nginx.org/en/download.html,這裡使用的版本為nginx-1.18.0.tar.gz

  2. 解壓nginx-1.18.0.tar.gz

# tar xf nginx-1.18.0.tar.gz
  1. 安裝可能需要的依賴:
# yum -y install autoconf automake make
# yum -y install gcc gcc-c++
# yum -y install pcre pcre-devel    
# yum -y install zlib zlib-devel
# yum install -y openssl openssl-devel
  1. 設定:
# cd nginx-1.18.0
# ./configure
  1. 安裝:
# make && make install

安裝完成後的二進位制檔案和組態檔在/usr/local/nginx目錄下。

  1. 設定環境變數:
# vi /etc/profile
最後增加 export PATH=$PATH:/usr/local/nginx/sbin

# source /etc/profile
  1. 安裝成為服務:
# vi nginx.service 
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

# systemctl daemon-reload

OpenResty的安裝

OpenResty是一個基於Nginx與Lua的高效能Web平臺,其內部整合了大量精良的Lua庫、第三方模組以及大多數的依賴項。

  1. 下載OpenResty,下載地址:https://openresty.org/download/openresty-1.17.8.2.tar.gz

  2. 安裝:

# tar xf openresty-1.17.8.2.tar.gz
# cd openresty-1.17.8.2
# ./configure
# make && make install
  1. 設定環境變數:
# vi /etc/profile
最後追加 export PATH=$PATH:/usr/local/openresty/bin

# cd /usr/local/openresty/bin
# ll | grep openresty
lrwxrwxrwx. 1 root root    37 Oct  9 14:09 openresty -> /usr/local/openresty/nginx/sbin/nginx

openresty是一個指向/usr/local/openresty/nginx/sbin/nginx的軟連結,其實openresty內建了一個nginx,使用方法與nginx一樣。

# openresty -V
nginx version: openresty/1.17.8.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.1 --add-module=../echo-nginx-module-0.62 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.17 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.8 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_ssl_module