升級系統全部軟件html
yum -y update
安裝Nginx編譯所須要的依賴項linux
yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel
下載Nginx源碼nginx
#方式一: wget http://nginx.org/download/nginx-1.9.15.tar.gz #方式二: git clone git@github.com:nginx/nginx.git
解壓 並 進入解壓後的文件夾c++
tar -xzf nginx-1.9.15.tar.gz cd nginx-1.9.15
選擇安裝的模塊git
./configure \ --user=nginx \ --group=nginx \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-pcre \ --with-file-aio \ --with-http_realip_module \ --without-http_scgi_module \ --without-http_uwsgi_module \ --without-http_fastcgi_module
編譯並安裝github
make -j 4 && make install
在init.d文件夾中建立nginx啓動文件shell
cd /etc/init.d vim nginx #添加如下內容 #!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # pidfile: /var/run/nginx.pid # user: nginx # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" lockfile=/var/run/nginx.lock start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
將上面寫的腳本添加到系統服務中(關於chkconfig請參考:Linux下chkconfig命令詳解)vim
chkconfig --add nginx chkconfig --level 345 nginx on # 給腳本執行的權限 chmod +x nginx
Linux 防火牆開放80端口tcp
firewall-cmd --permanent --add-port=80/tcp --zone=public
Nginx 開啓關閉命令this
service nginx start/restart/stop
注:本文轉自譯:Centos7 編譯安裝Nginx 教程,如需轉載請註明出處,https://www.cnblogs.com/zhuchenglin/p/11773815.html。