天天看點

Linux下nginx的安裝與部署和實踐詳解

安裝所需環境

Nginx 是 C語言 開發,建議在 Linux 上運作,本篇則使用 CentOS 7 作為安裝環境。

(1) 登陸linux:

下載下傳nginx:    wget http://nginx.org/download/nginx-1.8.0.tar.gz
下載下傳openssl : wget http://www.openssl.org/source/openssl-fips-2.0.9.tar.gz
下載下傳zlib    : wget http://zlib.net/zlib-1.2.11.tar.gz 
           

以上安裝包,也可以在單獨下載下傳後,上傳至CentOS ,然後編譯安裝;

一. gcc 安裝

安裝 nginx 需要先将官網下載下傳的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:

通過yum install gcc-c++ -y完成安裝

二.perl安裝

解壓源碼包

[[email protected] /]# tar -xzf perl-5.16.1.tar.gz

編譯并安裝,目錄可以自定義

[[email protected] /]# cd perl-5.16.1
[[email protected] perl-5.16.1]# ./Configure -des -Dprefix=/usr/local/perl

[[email protected] perl-5.16.1]# make
[[email protected] perl-5.16.1]# make test
[[email protected] perl-5.16.1]# make install

如果系統以前已安裝了舊版本的perl的話,替換系統原有的版本。
[[email protected] perl-5.16.1]# mv /usr/bin/perl /usr/bin/perl.bak
[[email protected] perl-5.16.1]# ln -s /usr/local/perl/bin/perl /usr/bin/perl

再看看是不是最新的版本

[[email protected] perl-5.16.1]# perl -v

This is perl 5, version 16, subversion 1 (v5.16.1) built for x86_64-linux

Copyright 1987-2012, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using “man perl” or “perldoc perl”. If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
如果看到以上資訊,就證明perl安裝成功了


           

三、然後開始安裝:openssl :

[[email protected]] tar zxvf openssl-fips-2.0.9.tar.gz

[[email protected]] cd openssl-fips-2.0.9

[ro[email protected]] ./config && make && make install

           

四、安裝pcre:

[[email protected]] tar zxvf pcre-8.36.tar.gz

[[email protected]] cd pcre-8.36

[[email protected]]  ./configure && make && make install 
           

五、開始安裝:zlib:

[[email protected]]tar zxvf zlib-1.2.8.tar.gz

[[email protected]] cd zlib-1.2.8

[[email protected]]  ./configure && make && make install
           

六、最後安裝nginx

[[email protected]]tar zxvf nginx-1.8.0.tar.gz

[[email protected]] cd nginx-1.8.0

[[email protected]]  ./configure && make && make install
           

七、 啟動nginx

/usr/local/nginx/sbin/nginx
           

然後測試一下:

使用ps -ef | grep nginx

八、防火牆關掉

方法:

systemctl stop firewalld.service
systemctl disable firewalld.service
           

然後前台通路: 使用 http://localhost(請換成您機器的ip)

九、 如果想要停止,請使用:

pkill -9 nginx 

           

十、重新開機和停止

1、驗證nginx配置檔案是否正确

方法一:進入nginx安裝目錄sbin下,輸入指令./nginx -t

看到如下顯示
nginx.conf syntax is ok

nginx.conf test is successful
           

2、重新開機Nginx服務

進入nginx可執行目錄sbin下,輸入指令./nginx -s reload 即可