Nginx安裝手冊

1、nginx安裝環境
nginx是 c 語言開發的,建議在Linux 上運行。本教程使用的是Centos6.5 作爲安裝環境。
  • gcc
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴gcc 環境,如果沒有gcc 環境,需要安裝gcc:
yun install gcc-c++
  • pcre
pcre 是一個Perl 庫,包括 Perl 兼容的正則表達式庫。nginx 的http 模塊使用 pcre 來解析正則表達式,所以需要在Linux 上安裝pcre 庫。
yum install -y pcre pcre-devel
pcre-devel 是使用pcre 開發的一個二次開發庫,nginx 也需要此庫。
  • zlib
zlib 庫提供了很多種壓縮和解壓縮的方式,nginx 使用zlib 對http 包的內同進行gzip ,所以需要在Linux 上安裝 zlib 庫
yum install -y zlib zlib-devel
  • openssl
OpenSSL 是一個強大的暗鑽套接字層密碼庫,囊括主要的密碼算法、常用的祕鑰和整數封裝管理功能以及SSL 協議,並提供豐富的應用程序供測試或其他目的的使用。
nginx 不僅支持http 協議,還支持 https 所以需要在Linux 安裝openssl 庫。
yum install -y openssl openssl-devel
2、編譯安裝
將nginx-1.8.0.tar.gz 拷貝到Linux 服務器。
解壓:
tar -zxvf nginx-1.8.0.tar.gzcd nginx-1.8.0
1、configure
./configure --help 查詢詳細參數
參數設置如下:
./configure \--prefix=/usr/local/nginx \--pid-path=/var/run/nginx/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
注意:上邊將臨時文件目錄指定爲 /var/temp/nginx ,需要在 /var 下創建 temp 及 nginx 目錄。
2、編譯安裝
make install
安裝成功查看安裝目錄:
ll 命令查看,會有conf,html,sbin 至少三個文件夾。
3、啓動 nginx
cd /usr/local/nginx/sbin/./nginx
查詢nginx進程:
ps aux|grep nginx
15098 是 nginx 主進程的進程id,10599 是nginx 工作進程的進程id
注意:執行./nginx 啓動 nginx, 這裏可以 -c 指定加載的 nginx 配置文件,如下: ./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定 -c,nginx 在啓動時默認加載 conf/nginx.conf 文件,此文件的地址也可以再編譯安裝 nginx 時指定 ./configure 的參數(--conf-path= 指向配置文件(nginx.conf))
4、停止nginx
方式1. 快速停止:
cd /usr/local/nginx/sbin./nginx -s stop
此方法相當於先查出nginx 進程id 在使用 kill 命令強制殺掉進程。
方式2. 完整停止(建議使用
cd /usr/local/nginx/sbin./nginx -s quit
此方式停止步驟是待nginx 進程處理任務完畢進行停止。
5、重啓nginx
1.先停止再啓動(建議使用
對nginx進行重啓相當於先停止nginx再啓動nginx,即先執行停止命令再執行啓動命令。
./nginx -s quit./nginx
2.重新加載配置文件
當nginx的配置文件nginx.conf修改後,要想讓配置生效需要重啓nginx,使用-s reload不用先停止nginx再啓動nginx即可將配置信息在nginx中生效,如下:
./nginx -s reload
6、開機啓動nginx
1. 編寫shell 腳本
這裏使用的是編寫shell 腳本的方式處理
vi /etc/init.d/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: /var/run/nginx.pid# config: /usr/local/nginx/conf/nginx.confnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/var/run/nginx.pidRETVAL=0prog="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 1fi 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" instart) start ;;stop) stop ;;reload) reload ;;restart) stop start ;;status) status $prog RETVAL=$? ;;*) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1esacexit $RETVAL
:wq 保存並退出
2. 設置文件的訪問權限
chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用戶可執行)
這樣在控制檯就很容易的操作nginx 了。查看 nginx 當前狀態,啓動 nginx,停止nginx,重啓nginx。。。
如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加載新的配置文件並運行,可以將此命令加入到rc.local文件中,這樣開機的時候nginx就默認啓動了
3.加入到rc.local 文件中
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 保存並退出,下次重啓生效
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章