一.安裝依賴包
安裝 Nginx 常用的依賴包
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
- gcc : GCC——GNU編譯器集合;
- pcre-devel : PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式;
- zlib : zlib 庫提供了很多種壓縮和解壓縮的方式;
- openssl : OpenSSL 是一個強大的安全套接字層密碼庫, 安裝用於支持 HTTPS;
二. 下載安裝包
去 Nginx 官網 找到最新發布版本的, 對應包, 下載
# 編譯包位置
mkdir /usr/local/nginx
# 源碼包位置
cd /home
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxvf nginx-1.14.2.tar.gz
三. 安裝Nginx
將源碼包的信息編譯
cd /home/nginx-1.14.2
./configure
make
make install
四. 配置 nginx.conf
根據自己的需要配置, 默認使用80端口 HTTP 協議
vim /usr/local/nginx/conf/nginx.conf
五. 啓動 Nginx
指定配置文件的方式啓動
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
六. 常用配置
1. nginx 添加到服務中
在 /etc/init.d 中新建 nginx 腳本, 內容如下
#!/bin/bash
#
# chkconfig: - 85 15
# description: Nginx is a World Wide Web server.
# processname: nginx
nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
start)
echo -n "Starting Nginx"
$nginx -c $conf
echo " done"
;;
stop)
echo -n "Stopping Nginx"
killall -9 nginx
echo " done"
;;
test)
$nginx -t -c $conf
;;
reload)
echo -n "Reloading Nginx"
ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
echo " done"
;;
restart)
$0 stop
$0 start
;;
show)
ps -aux|grep nginx
;;
*)
echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
;;
esac
設置可執行文件
# 設置可執行權限
chmod +x /etc/init.d/nginx
# 註冊服務
chkconfig --add nginx
添加完成後, 對應命令如下
service nginx start
service nginx stop
service nginx restart
service nginx reload
2. Nginx 添加環境變量
vim /etc/profile
# 添加
export PATH=$PATH:/usr/local/nginx/sbin
# 刷新環境變量
source /etc/profile