第一次寫博客,肯能會有很多不足,主要記錄自己的一點點成長。
我用的是阿里雲 所以基本上的編譯依賴都自帶安裝好了。如果沒有安裝編譯所需的依賴,那么就自己手動安裝一下。
我們約定所有源碼包放在 /usr/local/src/ 目錄下,編譯安裝在 /usr/local/ 目錄下。
#編譯前准備,更新yum源包
yum update
#創建用戶組www,創建用戶www 所屬組 www 設置不能登錄
groupadd www && useradd -g www www -s /bin/false
#安裝nginx編譯所需依賴包
#http_image_filter_module模塊需要gd庫
#這里是安裝二進制包,也可以下載源碼包編譯安裝
yum -y install zlib zlib-devel openssl* pcre pcre-devel gd-devel
#進入下載源碼包目錄
#下載源碼包
#解壓源碼包
cd /usr/local/src wget http://nginx.org/download/nginx-1.12.2.tar.gz tar -zxvf nginx-1.12.2.tar.gz
#創建安裝目錄
mkdir /usr/local/nginx
#進入解壓好的源碼包准備編譯安裝
cd nginx-1.12.2
#編譯nginx
#因為上面的依賴包是二進制安裝 所以--with-pcre --with-zlib --with-openssl 不需要加到參數里,如果非二進制包則要指定路徑 --with-pcre=/usr/local/pcre/
./configure --prefix=/usr/local/nginx/ \ --user=www \ --group=www \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_sub_module \ --with-http_realip_module \ --with-http_image_filter_module
需要什么模塊功能可以自己選擇 ./configure --help 命令可以查看所有模塊
注意一下配置時有沒有報錯,報錯的話缺什么補什么
#如果沒有報錯 則進行編譯安裝
make && make install
#安裝成功后,進入安裝目錄
#備份原配置
#修改nginx.conf
cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.bak
vim nginx.conf
修改前: 修改后:
注意每行結束的 “;”分號,不然開啟的時候會報錯。
#配置Nginx控制腳本
vim /etc/rc.d/init.d/nginx
寫入以下腳本:
注意安裝路徑不一樣的話,需要自己更改腳本
ps:腳本代碼是從別人那里拿回來的

#!/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: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/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/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:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 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 } 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
#設置腳本權限
#設置腳本開機啟動
#啟動nginx
chmod 775 /etc/rc.d/init.d/nginx chkconfig nginx on /etc/rc.d/init.d/nginx start
以后可以直接使用命令 service nginx (start|stop|restart)
#把nginx加入系統變量
echo 'export PATH=$PATH:/usr/local/nginx/sbin'>>/etc/profile && source /etc/profile
最后,打開瀏覽器,輸入服務器IP地址,出現下圖說明安裝成功
謝謝!