Centos7.4下源碼安裝nginx并附shell安裝腳本/配置
Nginx是一款輕量級(jí)的網(wǎng)頁(yè)服務(wù)器柿菩、反向代理服務(wù)器。相較于Apache雨涛、lighttpd具有占有內(nèi)存少枢舶,穩(wěn)定性高等優(yōu)勢(shì)。它最常的用途是提供反向代理服務(wù)替久。
一凉泄、yum 安裝的刪除方法
Nginx雖然好用,但是一旦關(guān)鍵配置文件被修改蚯根,想要卸載重裝卻是相當(dāng)困難后众。本人因?yàn)椴捎脃um方式安裝后又源碼安裝了Nginx,結(jié)果出現(xiàn)沖 突颅拦,卸載不了蒂誉,安裝不上,很是蛋疼距帅。主要的問題還是Nginx卸載的時(shí)候右锨,沒有完全清除關(guān)聯(lián)關(guān)系,也沒有刪除對(duì)應(yīng)文件或者文件夾碌秸。
比較靠譜的解決辦法是:root權(quán)限下命令行敲入如下命令:
rm -rf /etc/nginx/
rm -rf /usr/sbin/nginx
rm /usr/share/man/man1/nginx.1.gz
yum remove nginx*
二绍移、源碼安裝的刪除方法
編譯時(shí)的路徑如果指定了--prefix /usr/local/xxx?直接rm -rf /usr/local/xxx即可悄窃。
沒指定路徑,那就到源碼路徑執(zhí)行make uninstall蹂窖。
如果源碼刪了 自己到?/usr/bin?/etc?/usr/sbin?/usr/lib找到相關(guān)文件手動(dòng)刪除
三轧抗、首先安裝必要的庫(kù)
nginx 中g(shù)zip模塊需要 zlib 庫(kù),rewrite模塊需要 pcre 庫(kù)瞬测,ssl 功能需要openssl庫(kù)横媚。選定/usr/local為安裝目錄,以下具體版本號(hào)根據(jù)實(shí)際改變涣楷,這里全yum安裝
#yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
源碼下載地址如下:
1.安裝PCRE庫(kù)
$ cd /usr/local/
$ wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
$ tar -zxvf pcre-8.36.tar.gz
$ cd pcre-8.36
$ ./configure
$ make
$ make install
2.安裝zlib庫(kù)
$ cd /usr/local/
$ wgethttp://zlib.net/zlib-1.2.8.tar.gz
$ tar -zxvf zlib-1.2.8.tar.gz
$ cd zlib-1.2.8
$ ./configure
$ make
$ make install
3.安裝ssl
$ cd /usr/local/
$ wgethttp://www.openssl.org/source/openssl-1.0.1j.tar.gz
$ tar -zxvf openssl-1.0.1j.tar.gz
$ ./config
$ make
$ make install
四分唾、創(chuàng)建一個(gè)文件夾并下載nginx-1.15.4
cd /usr/localmkdir nginx
Nginx cache purge模塊(可選)(暫不選)
# wgethttp://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz
#tar -xzfngx_cache_purge-1.3.tar.gz
五、編譯安裝nginx
$ cd /usr/local/
$ wgethttp://nginx.org/download/nginx-1.15.4.tar.gz
$ tar -zxvf nginx-1.15.4.tar.gz
$ cd nginx-1.15.4
$? ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --add-module=../ngx_cache_purge-1.3[暫不選]
--prefix=/usr/local/nginx-1.0.6?? #?安裝路徑
--with-http_stub_status_module #?啟用nginx狀態(tài)模塊
--with-http_ssl_module #?啟用SSL模塊
--with-http_realip_module #?啟用realip模塊(將用戶IP轉(zhuǎn)發(fā)給后端服務(wù)器)
--add-module=../ngx_cache_purge-1.3 #?添加緩存清除擴(kuò)展模塊[暫不選]
前面庫(kù)包源碼安裝的話狮斗,則在--prefix后面接以下命令:
--with-pcre=/usr/local/pcre-8.36 指的是pcre-8.36 的源碼路徑。
--with-zlib=/usr/local/zlib-1.2.8 指的是zlib-1.2.8 的源碼路徑弧蝇。
?# make
# make install
附:shell安裝腳本
腳本中可以選定版本號(hào)碳褒,支持庫(kù)包是以yum方式在線安裝的
#!/bin/bash
#--------------------------------------------------------
# Function: Install nginx for CentOS7
# Date: 2018-1-06
# Author: Anwar Wong
#--------------------------------------------------------
#Print debug information
NGINX_VER="$?"
NGINX_SOFT="nginx-${NGINX_VER}.tar.gz"
NGINX_URL="http://nginx.org/download"
NGINX_DIR="/usr/local/nginx"
NGINX_SRC=`echo $NGINX_SOFT| sed 's/.tar.*//g'`
NGINX_YUM="yum install -y"
NGINX_ARG="--user=www --group=www --with-http_stub_status_module --with-http_ssl_module"
if [$? -eq 0]; then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 1.2.3|1.12.2}\033[0m"
exit 0
fi
#Installing dependencies
$NGINX_YUM wget make tar gcc gcc-c++ glibc zlib zlib-devel
$NGINX_YUM perl perl-devel pcre pcre-devel openssl openssl-devel
#Downloading
wget -c $NGINX_URL/$NGINX_SOFT
tar -xzf $NGINX_SOFT
cd $NGINX_SRC
#Creating user and group
useradd -s /sbin/nologin www
#Starting install nginx
./configure --prefix=$NGINX_DIR/$NGINX_ARG
#Compile nginx
make -j4
make -j4 install
#Starting Nginx
$NGINX_DIR/sbin/nginx
#Show nginx status
ps -ef |grep nginx
netstat -tnlp |grep nginx
六、啟動(dòng)
$ /usr/local/nginx/sbin/nginx
檢查是否啟動(dòng)成功:
打開瀏覽器訪問此機(jī)器的 IP看疗,如果瀏覽器出現(xiàn) Welcome to nginx! 則表示 Nginx 已經(jīng)安裝并運(yùn)行成功沙峻。
部分命令如下:
重啟:
$ /usr/local/nginx/sbin/nginx –s reload
停止:
$ /usr/local/nginx/sbin/nginx –s stop
測(cè)試配置文件是否正常:
$ /usr/local/nginx/sbin/nginx –t
強(qiáng)制關(guān)閉:
$ pkill nginx
錯(cuò)誤的原因是沒有創(chuàng)建www這個(gè)用戶,應(yīng)該在服務(wù)器系統(tǒng)中添加www用戶組和用戶www两芳,如下命令:
#/usr/sbin/groupadd -f www
#/usr/sbin/useradd -g www www