安裝所需環境
Nginx 是 C語言 開發,建議在 Linux 上運行,當然,也可以安裝 Windows 版本,本篇則使用 CentOS 作爲安裝環境。
--先檢查yum源,可參考:Linux下的yum命令及yum源設定
一. gcc 安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:
yum install gcc-c++
二. 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
三. zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。 命令:
yum install -y zlib zlib-devel
四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。 命令:
yum install -y openssl openssl-devel
官網下載
1.直接下載.tar.gz
安裝包,地址:https://nginx.org/en/download.html
2.使用wget
命令下載(推薦)。
如果wget未安裝請先安裝:
yum install wget
下載nginx
wget -c https://nginx.org/download/nginx-1.14.1.tar.gz
下載的版本根據自己需要進行選擇。
解壓
使用tar命令:
tar -zxvf nginx-1.14.1.tar.gz
cd nginx-1.14.1
配置
其實在 nginx-1.14.1 版本中你就不需要去配置相關東西,默認就可以了。當然,如果你要自己配置目錄也是可以的。
1.使用默認配置
./configure
默認安裝目錄在:/usr/local/
2.自定義配置(不推薦)
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
啓動、停止nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
./nginx -s quit
:此方式停止步驟是待nginx進程處理任務完畢進行停止。
./nginx -s stop
:此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。
查詢nginx進程:
ps aux|grep nginx
或
ps -ef|grep nginx
重啓 nginx
1.先停止再啓動(推薦):
對 nginx 進行重啓相當於先停止再啓動,即先執行停止命令再執行啓動命令。如下:
./nginx -s quit
./nginx
2.重新加載配置文件:
當 ngin x的配置文件 nginx.conf 修改後,要想讓配置生效需要重啓 nginx,使用-s reload
不用先停止 ngin x再啓動 nginx 即可將配置信息在 nginx 中生效,如下:
./nginx -s reload
啓動成功後,在瀏覽器可以看到這樣的頁面,證明啓動成功了:
把nginx配置成系統服務
1. 進入目錄/etc/init.d
cd /etc/init.d
2. 建立文本文件nginx
vim nginx
配置內容
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
#nginx程序路徑
nginxd=/usr/local/nginx/sbin/nginx
#nginx配置文件路徑
nginx_config=/usr/local/nginx/conf/nginx.conf
#nginx pid文件的路徑,可以在nginx的配置文件中找到
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="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
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
3.執行
chkconfig --add /etc/init.d/nginx #設置爲系統服務
chmod 755 /etc/init.d/nginx #添加可執行權限
chkconfig --add nginx #設置爲系統服務
如果有必要設置成開機自啓請執行:
chkconfig nginx on
4.服務命令
nginx啓動、停止、無間斷服務重啓,可選 start | stop | restart | reload | status | help
service nginx start
service nginx stop
service nginx reload
如果能訪問到nginx的歡迎頁就證明服務配置成功了。
後面就可以進行自由配置了。