1.系統要求html
nginx是C語言開發,建議在linux上運行,本教程使用Centos6.5做爲安裝環境。linux
1-1 安裝 GCCnginx
源碼安裝nginx須要依賴gcc環境,須要安裝gcc:c++
yum install gcc-c++
1-2 安裝 PCREweb
PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,因此須要在linux上安裝pcre庫。正則表達式
yum install -y pcre pcre-devel 注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也須要此庫。
1-3 安裝 zlib算法
zlib庫提供了不少種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,因此須要在linux上安裝zlib庫。shell
yum install -y zlib zlib-devel
1-4 openssl瀏覽器
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。安全
nginx不只支持http協議,還支持https(即在ssl協議上傳輸http),因此須要在linux安裝openssl庫。
yum install -y openssl openssl-devel
2. 編譯安裝
建立臨時文件夾
mkdir -p /var/temp/nginx/client
下載nginx文件: https://files.cnblogs.com/files/ftl1012/nginx-1.8.0.tar.gz
將nginx-1.8.0.tar.gz拷貝至linux服務器。
解壓並進入目錄:
tar -zxvf nginx-1.8.0.tar.gz; cd nginx-1.8.0
3. configure安裝
./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 echo "-------------------------------------------------------" make;make install
4. 安裝成功查看安裝目錄
cd /usr/local/nginx
5. 啓動nginx
echo "cd /usr/local/nginx/sbin/;./nginx" >> /etc/rc.local
注意:執行./nginx啓動nginx,這裏能夠-c指定加載的nginx配置文件,以下: ./nginx -c /usr/local/nginx/conf/nginx.conf 若是不指定-c,nginx在啓動時默認加載conf/nginx.conf文件,此文件的地址也能夠在編譯安裝nginx時指定./configure的參數(--conf-path= 指向配置文件(nginx.conf))
6. 查看服務狀態
ps -axu |grep --color=auto nginx # 4879是主進程,4880是工做的進程
7. 關閉nginx
方式1,快速中止:此方式至關於先查出nginx進程id再使用kill命令強制殺掉進程。
cd /usr/local/nginx/sbin; ./nginx -s stop
方式2,完整中止(建議使用):此方式中止步驟是待nginx進程處理任務完畢進行中止。
cd /usr/local/nginx/sbin;./nginx -s quit
8. 測試是否成功
nginx安裝成功,啓動nginx,便可訪問虛擬機上的nginx:
瀏覽器輸入VM的IP便可
9. 開機自啓動nginx(編寫shell腳本)
9-1 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.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/var/run/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
9-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就默認啓動了
9-3 加入到rc.local文件中
echo "/etc/init.d/nginx start " >>/etc/rc.local
【更多參考】
1.2. Ngnix基於域名的配置_server
1.3. Ngnix基於域名的配置_include
1.4. Nginx基於虛擬主機別名的設置
1.5. Nginx增長日誌選項
1.6. NGINX的重定向rewrite
1.7. Nginx性能問題
1.8. Nginx的留底下載文件