1、下載安裝包
進入官網(http://nginx.org/en/download.html)下載指定版本的安裝包,這里下載的是最新的nginx-1.18.0.tar.gz:
1 http://nginx.org/download/nginx-1.18.0.tar.gz
2、解壓壓縮文件
將下載下來的壓縮文件進行解壓安裝:
1 [develop@fxb-ah softs]$ tar zxf nginx-1.18.0.tar.gz 2 [develop@fxb-ah softs]$ cd nginx-1.18.0
3、編譯安裝nginx
3.1 環境的安裝
安裝nginx之前,需要安裝相應的環境,讓nginx可以正常的允許:
- gcc環境:基本運行環境
- pcre:用於nginx的http模塊解析正則表達式
- zlib:用戶進行gzip壓縮
- openssl:用於nginx https協議的傳輸
直接通過yum進行安裝即可:
1 [develp@fxb-ah nginx-1.18.0]$ sudo yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
3.2 編譯安裝nginx
1 [develop@fxb-ah nginx-1.18.0]$ sudo useradd -M -s /sbin/nologin nginx 2 [develop@fxb-ah nginx-1.18.0]$ sudo ./configure --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --prefix=/usr/local/nginx --user=nginx 3 [develop@fxb-ah nginx-1.18.0]$ sudo make && sudo make install 4 [develop@fxb-ah nginx-1.18.0]$ cd /usr/local/nginx/ 5 [develop@fxb-ah nginx]$ ll 6 total 16 7 drwxr-xr-x 2 root root 4096 Jun 30 14:21 conf 8 drwxr-xr-x 2 root root 4096 Jun 30 14:21 html 9 drwxr-xr-x 2 root root 4096 Jun 30 14:21 logs 10 drwxr-xr-x 2 root root 4096 Jun 30 14:21 sbin
安裝完成!
4、設置服務
將nginx制作成服務,並設置開機啟動(centos7):
1 [develop@fxb-ah nginx]$ sudo vim /usr/lib/systemd/system/nginx.service 2 [Unit] 3 Description=nginx 4 After=network.target 5 6 [Service] 7 Type=forking 8 ExecStart=/usr/local/nginx/sbin/nginx 9 ExecReload=/usr/local/nginx/sbin/nginx -s reload 10 ExecStop=/usr/local/nginx/sbin/nginx -s quit 11 PrivateTmp=true 12 13 [Install] 14 WantedBy=multi-user.target 15 [developer@iZuf6ai62xce7yu53epv2jZ redis-6.0.5]$ sudo systemctl daemon-reload 16 [developer@iZuf6ai62xce7yu53epv2jZ redis-6.0.5]$ sudo systemctl start nginx.service 17 [developer@iZuf6ai62xce7yu53epv2jZ redis-6.0.5]$ sudo systemctl enable nginx.service
低版本centos下服務制作:
1 [develop@fxb-ah nginx]$ sudo vi /etc/init.d/nginx 2 #!/bin/bash 3 # nginx Startup script for the Nginx HTTP Server 4 # it is v.0.0.2 version. 5 # chkconfig: - 85 15 6 # description: Nginx is a high-performance web and proxy server. 7 # It has a lot of features, but it's not for everyone. 8 # processname: nginx 9 # pidfile: /var/run/nginx.pid 10 # config: /usr/local/nginx/conf/nginx.conf 11 nginxd=/usr/local/nginx/sbin/nginx 12 nginx_config=/usr/local/nginx/conf/nginx.conf 13 nginx_pid=/var/run/nginx.pid 14 RETVAL=0 15 prog="nginx" 16 # Source function library. 17 . /etc/rc.d/init.d/functions 18 # Source networking configuration. 19 . /etc/sysconfig/network 20 # Check that networking is up. 21 [ ${NETWORKING} = "no" ] && exit 0 22 [ -x $nginxd ] || exit 0 23 # Start nginx daemons functions. 24 start() { 25 if [ -e $nginx_pid ];then 26 echo "nginx already running...." 27 exit 1 28 fi 29 echo -n $"Starting $prog: " 30 daemon $nginxd -c ${nginx_config} 31 RETVAL=$? 32 echo 33 [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx 34 return $RETVAL 35 } 36 # Stop nginx daemons functions. 37 stop() { 38 echo -n $"Stopping $prog: " 39 killproc $nginxd 40 RETVAL=$? 41 echo 42 [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid 43 } 44 # reload nginx service functions. 45 reload() { 46 echo -n $"Reloading $prog: " 47 #kill -HUP `cat ${nginx_pid}` 48 killproc $nginxd -HUP 49 RETVAL=$? 50 echo 51 } 52 # See how we were called. 53 case "$1" in 54 start) 55 start 56 ;; 57 stop) 58 stop 59 ;; 60 reload) 61 reload 62 ;; 63 restart) 64 stop 65 start 66 ;; 67 status) 68 status $prog 69 RETVAL=$? 70 ;; 71 *) 72 echo $"Usage: $prog {start|stop|restart|reload|status|help}" 73 exit 1 74 esac 75 exit $RETVAL 76 [develop@fxb-ah nginx]$ sudo chmod a+x /etc/init.d/nginx 77 [develop@fxb-ah nginx]$ sudo chkconfig --add /etc/init.d/nginx 78 [develop@fxb-ah nginx]$ service nginx status 79 nginx is stopped 80 [develop@fxb-ah nginx]$ sudo chkconfig nginx on