centos 7 編譯安裝nginx

1)卸載nginxphp


若是沒有配置--prefix選項,源碼包也沒有提供make uninstall,則能夠經過如下方式能夠完整卸載:
 
找一個臨時目錄從新安裝一遍,如:
html

$ ./configure --prefix=/tmp/to_remove && make install


 而後遍歷/tmp/to_remove的文件,刪除對應安裝位置的文件便可(由於/tmp/to_remove裏的目錄結構就是沒有配置--prefix選項時的目錄結構)。
linux

2) 先建立用戶和組nginx

# groupadd nginx
# useradd -s /sbin/nologin -g nigix -M nginx

3) 安裝 gccc++

安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝:web


yum install gcc-c++

4)  PCRE pcre-devel 安裝正則表達式

PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,因此須要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也須要此庫。命令:算法

yum install -y pcre pcre-devel

5)  zlib 安裝vim

zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫。centos

yum install -y zlib zlib-devel

6)  OpenSSL 安裝

OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。

nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫。

yum install -y openssl openssl-devel

7)直接下載.tar.gz安裝包,地址:https://nginx.org/en/download.html或者

wget https://nginx.org/download/nginx-1.4.7.tar.gz 
tar -zxvf nginx-1.4.7.tar.gz


6) 編譯安裝, 使用默認配置 ,具體配置: http://nginx.org/en/docs/configure.html

# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
# make
# make install
#指定運行權限的用戶
--user=nginx
#指定運行的權限用戶組
--group=nginx
#指定安裝路徑
--prefix=/usr/local/nginx
#支持nginx狀態查詢
--with-http_stub_status_module
#開啓ssl支持
--with-http_ssl_module
#開啓GZIP功能
--with-http_gzip_static_module

7)添加nginx爲系統服務,非必須


#!/bin/sh
#
## nginx - this script starts and stops the nginx daemon
# 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
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
# 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"
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE=" /usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'`
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=`echo $opt | cut -d "=" -f 2`
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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
    sleep 1
    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



8) 開啓nginx服務

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig  nginx on 
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/selinux/config 
SELINUX=disabled

所以,添加上面腳本後,centos7 中操做nginx的方法有


systemctl is-enabled nginx.service #查詢nginx是否開機啓動
systemctl enable nginx.service #開機運行nginx
systemctl disable nginx.service #取消開機運行nginx
systemctl start nginx.service #啓動nginx
systemctl stop nginx.service #中止nginx
systemctl restart nginx.service #重啓nginx
systemctl reload nginx.service #從新加載nginx配置文件
systemctl status nginx.service #查詢nginx運行狀態
systemctl --failed #顯示啓動失敗的服務


參考博客: 

https://www.cnblogs.com/gotodsp/p/6405106.html


https://blog.csdn.net/xcg132566/article/details/79161756


https://blog.csdn.net/dexter_wang/article/details/55681808


https://blog.csdn.net/xuguokun1986/article/details/74732833

相關文章
相關標籤/搜索