譯:Centos7 編譯安裝Nginx 教程

相信通過上篇博文的學習,聰明的你已經學會了如何在Centos7 上經過yum 方式安裝Nginx ,可是有時候有些場景或者特俗狀況下,咱們每每須要經過編譯源碼方式安裝,以便於更靈活地定製咱們的Nginx.html

這節課咱們將一塊兒學習如何在centos7 上使用編譯源碼的方式安裝Nginx.nginx

本博文翻譯自Youtube, 看英文原版請移步  YouTube視頻資料   文檔資料 c++


 在開始以前,咱們先來看看源碼編譯安裝的優缺點,這樣以便於咱們更好地理解何時用哪一種安裝方式比較好。git

要知道,源碼編譯安裝並非件容易的事情,因此這種方式有一些缺點,以下所示:github

  • 所用的時間。
  • 單獨安裝軟件的全部依賴項。
  • 完整掌握軟件編譯。

可是在如下狀況下從新編譯是有用的:(優勢)windows

  • 編譯你的有用模塊而不是所有。
  • 使用最新版本而不是舊版本。
  • 可使用您本身的對您的項目有用的配置。
  • 建立一個新的Centos機器:

 本博文教程在Centos7 上演示,因此沒有Linux 環境的能夠參考鄙人的另一篇博文  VMWare Workstation虛擬機 安裝Centos7 圖文指南centos

1. 安裝前咱們最好使用yum更新下瀏覽器

yum -y update 

2. 咱們仍是最好退一個層級目錄服務器

 由於當咱們打開終端,默認是這樣的,tcp

因此咱們最好退一個目錄,不然可能運行命令會失敗

cd ..

Tips: cd 和.. 之間要有空格哦

2. 安裝Nginx 編譯所需全部依賴項

yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel

3. 編譯咱們須要下載源代碼。

有兩個地方均可以進行下載

下載一: http://nginx.org/download/     

centos7 下載命令

wget http://nginx.org/download/nginx-1.9.15.tar.gz

 下載成功後如圖所示:

下載二: https://github.com/nginx/nginx

git clone git@github.com:nginx/nginx.git

4. 下載完畢後咱們須要解壓他們

tar -xzf nginx-1.9.15.tar.gz

5. 進入nginx-1.9.15.tar.gz 文件夾

cd nginx-1.9.15

6.選擇安裝的模塊

 nginx是很是大的軟件,它內部有不少模塊。 在這裏你必須選擇哪一個模塊對你的網站有用。 您能夠查看下面的列表。 您也能夠按照如下命令經過本身的系統進行檢查:

./configure \
--user=nginx \
--group=nginx \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module

 執行完上面的指令後會看到這樣的回顯

7.執行編譯指令,輸入如下命令

make

 8.執行成功後再輸入安裝指令

make install

 編譯安裝成功後會有以下回顯

 

9. 咱們須要在init.d文件夾中建立nginx啓動文件。 這樣每次服務器從新啓動init進程都會自動啓動咱們的Web服務器

cd /etc/init.d

成功進入後會有這樣的回顯

8. 建立nginx 配置文件

touch nginx

執行先後如圖所示:

10. 編輯這個文件

nano nginx

 打開成功的話能夠看到這樣

11. 複製如下內容到這個編輯器中

編輯文件內容以下:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# pidfile:     /var/run/nginx.pid
# user:        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

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

lockfile=/var/run/nginx.lock

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
View Code

 

備用下載地址一  備用下載地址二

 12. Ctrl +O 快捷鍵保存,看到下面提示,按下回車鍵

13. 而後再按下 Ctrl + X 離開

14. 會到上層目錄

cd

 執行先後效果如圖所示

15. 建立一個用戶

useradd -r nginx

16. 校驗配置文件依次輸入下列命令

chkconfig --add nginx
chkconfig --level 345 nginx on

執行成功後如圖所示:

17. 進入init.d 文件夾

cd /etc/init.d/

 當前文件所在目錄

18. 給這個文件添加執行權限

chmod +x nginx

 執行成功後看到這樣的回顯

19.退出當前目錄

cd ..

20. 打開Nginx 配置文件

nano /etc/nginx/nginx.conf

21. 在配置文件中default_type 下面添加這兩行內容

types_hash_bucket_size 64;
server_names_hash_bucket_size 128;

效果如圖所示:

Ctrl +O 寫入,而後鍵盤快捷鍵Ctrl+X 退出編輯器

有時候因爲Linux 服務器上防火牆會端口攔截,因此咱們須要在防火牆中開放80 端口,方便咱們待會在物理機上查看

firewall-cmd --permanent --add-port=80/tcp --zone=public

 執行成功看到回顯

從新加載防火牆配置

firewall-cmd --reload

 防火牆配置從新加載成功回看到這個

 22. 啓動nginx服務

service nginx start

23. 查看當前IP地址輸入命令:

ifconfig

執行成功後能夠看到咱們的Linux 操做系統IP地址是 192.168.233.129

而後在咱們的windows 主機Chrome 瀏覽器上輸入IP地址:

http://192.168.233.129

能夠看到結果

 中止Nginx服務

systemctl stop nginx

(本篇完~)

相關文章
相關標籤/搜索