歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux操作系統安裝nginxs

Linux操作系統安裝nginxs

日期:2017/3/3 12:49:24   编辑:Linux技術
什麼是Nginx?
Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器,在高連接並發的情況下Nginx 是 Apache 服務器不錯的替代品.其特點是占有內存少,並發能力強,事實上nginx的並發能力確實在同類型的網頁服務器中表現較好.目前中國大陸使用nginx網站用戶有:新浪、網易、 騰訊,另外知名的微網志Plurk也使用nginx。
Nginx 作為 負載均衡 服務器:
Nginx 既可以在內部直接支持 Rails 和 PHP 程序對外進行服務,也可以支持作為 HTTP 代理服務器對外進行服務。 Nginx 采用 C 進行編寫, 不論是系統資源開銷還是 CPU 使用效率都比 Perlbal 要好很多。作為郵件代理服務器:
Nginx 同時也是一個非常優秀的郵件代理服務器(最早開發這個產品的目的之一也是作為郵件代理服務器), Last.fm 描述了成功並且美妙的使用經驗。Nginx 是一個安裝非常簡單,配置文件非常簡潔(還能夠支持perl語法), Bugs非常少的服務器: Nginx 啟動特別容易,並且幾乎可以做到 7*24 不間斷運行,即使運行數個月也不需要重新啟動。 你還能夠不間斷服務的情況下進行軟件版本的升級。
下載
cd /user/local  
wgethttp://nginx.org/download/nginx-1.8.0.tar.gz

安裝依賴項
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel


環境配置
<span >cd nginx-1.8.0  
./configure  </span>
安裝
make && make install

啟動查看
cd /usr/local/nginx/sbin  
./nginx
ps -aux|grep nginx


關閉重啟
./nginx -s stop 
./nginx -s reload

查看狀態
./nginx -t


如何設置成開機啟動
<span >cd /etc/init.d  
touch nginx  這是創建文件(當文件不存在的時候)</span>
新建完之後編輯
<span >vim /etc/init.d/nginx </span>

進入編輯文件之後 按 i 然後就可以插入了,復制下面代碼拷貝進去
<span >#!/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/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  </span>

然後按 Esc 按鈕退出編輯 並且 輸入 :wq 保存並退出
賦予執行權限
<span >chmod +x /etc/init.d/nginx  </span>
添加服務
<span >chkconfig --add nginx  </span>
設置成開機啟動
<span >chkconfig --level 35 nginx on </span>
查看是否設置成功
<span >chkconfig --list | grep nginx </span>


第二種 自己下載安裝
Nginx的安裝
模塊依賴性Nginx需要依賴下面3個包
1. gzip 模塊需要 zlib 庫 ( 下載: http://www.zlib.net/ )
2. rewrite 模塊需要 pcre 庫 ( 下載: http://www.pcre.org/ )
3. ssl 功能需要 openssl 庫 ( 下載: http://www.openssl.org/ )
Nginx包下載: http://nginx.org/en/download.html
依賴包安裝順序依次為:openssl、zlib、pcre, 然後安裝Nginx包.
圖解教程
第一步: 下載安裝所需包
openssl-fips-2.0.2.tar.gz
zlib-1.2.7.tar.gz
pcre-8.21.tar.gz
nginx-1.2.6.tar.gz
第二步:依次安裝openssl-fips-2.0.2.tar.gz, zlib-1.2.7.tar.gz, pcre-8.21.tar.gz, nginx-1.2.6.tar.gz
1.安裝openssl-fips-2.0.2.tar.gz
[root@localhost mrms]# tar -zxvf openssl-fips-2.0.2.tar.gz 

[root@localhost mrms]# cd openssl-fips-2.0.2

[root@localhost openssl-fips-2.0.2]# ./config 

[root@localhost openssl-fips-2.0.2]# make

[root@localhost openssl-fips-2.0.2]# make install

2.安裝zlib-1.2.7.tar.gz
[root@localhost mrms]# tar -zxvf zlib-1.2.7.tar.gz

[root@localhost mrms]# cd zlib-1.2.7

[root@localhost zlib-1.2.7]# ./configure 

[root@localhost zlib-1.2.7]# make

[root@localhost zlib-1.2.7]# make install

3.安裝pcre-8.21.tar.gz
[root@localhost mrms]# tar -zxvf pcre-8.21.tar.gz

[root@localhost mrms]# cd pcre-8.21

[root@localhost pcre-8.21]# ./configure 

[root@localhost pcre-8.21]# make

[root@localhost pcre-8.21]# make install

4.安裝 nginx-1.2.6.tar.gz
[root@localhost mrms]# tar -zxvf nginx-1.2.6.tar.gz 

[root@localhost mrms]# cd nginx-1.2.6

[root@localhost nginx-1.2.6]# ./configure --with-pcre=../pcre-8.21 --with-zlib=../zlib-1.2.7 --with-openssl=../openssl-fips-2.0.2

[root@localhost nginx-1.2.6]# make

[root@localhost nginx-1.2.6]# make install

至此Nginx的安裝完成!
第三步:檢測是否安裝成功
[root@localhost nginx-1.2.6]# cd  /usr/local/nginx/sbin

[root@localhost sbin]# ./nginx -t

出現如下所示提示,表示安裝成功

啟動nginx
[root@localhost sbin]# ./nginx

查看端口
[root@localhost sbin]# netstat -ntlp

結果如下
Copyright © Linux教程網 All Rights Reserved