1. 程式人生 > >centos6.5 安裝nginx1.10.2

centos6.5 安裝nginx1.10.2

Nginx 官方網站 www.nginx.org

第一步:在/usr/local下建立nginx目錄並進入
(1 ) mkdir /usr/local/nginx
(2)cd /usr/local/nginx
下載nginx相應的版本,wget 版本路徑例如:
(3)wget http://nginx.org/download/nginx-1.10.2.tar.gz

第二步:解壓:
(4)tar zxvf nginx-1.10.2.tar.gz
第三步執行配置初始化:進入解壓資料夾 nginx
(5)[[email protected] local]# cd nginx-1.10.2
(6)[[email protected]

nginx-1.10.2]#./configure --help 可以檢視configure 的引數寫法以及相應的引數
(7) [[email protected] nginx-1.10.2]#./configure --prefix=/usr/local/nginx 配置nginx的安裝路徑
執行configure配置不成功遇到的問題:報錯如下:
checking for OS
 + Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler … not found
  
./configure: error: C compiler cc is not found

出現這個錯誤。那麼就是gcc 包沒有安裝。
1.3.1 安裝gcc
檢視gcc命令
[[email protected] nginx-1.10.2]# whereis gcc
gcc:
安裝gcc
[[email protected] nginx-1.10.2]# yum -y install gcc
安裝成功後再次檢視
[[email protected] nginx-1.10.2]# whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/libexec/gcc /usr/share/man/man1/gcc.1.gz

gcc安裝好了。
1.3.2 繼續執行./configure
[

[email protected] nginx-1.10.2]# ./configure
checking for OS
 + Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler … found

checking for PCRE library … not found
checking for PCRE library in /usr/local/ … not found
checking for PCRE library in /usr/include/pcre/ … not found
checking for PCRE library in /usr/pkg/ … not found
checking for PCRE library in /opt/local/ … not found
  
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
出現如上錯誤。安裝pcre-devel
[[email protected] nginx-1.10.2]# yum install pcre-devel
1.3.3 再次執行./configure
error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib= option.
如果有這個錯誤 那麼執行
yum install zlib-devel
1.3.4 執行./configure後沒有報錯
[[email protected] nginx-1.10.2]# ./configure
checking for OS
 + Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler … found
 + using GNU C compiler
 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)

Configuration summary
 + using system PCRE library
 + OpenSSL library is not used
 + md5: using system crypto library
 + sha1: using system crypto library
 + using system zlib library
  
 nginx path prefix: “/usr/local/nginx”
 nginx binary file: “/usr/local/nginx/sbin/nginx”
 nginx modules path: “/usr/local/nginx/modules”
 nginx configuration prefix: “/usr/local/nginx/conf”
 nginx configuration file: “/usr/local/nginx/conf/nginx.conf”
 nginx pid file: “/usr/local/nginx/logs/nginx.pid”
 nginx error log file: “/usr/local/nginx/logs/error.log”
 nginx http access log file: “/usr/local/nginx/logs/access.log”
 nginx http client request body temporary files: “client_body_temp”
 nginx http proxy temporary files: “proxy_temp”
 nginx http fastcgi temporary files: “fastcgi_temp”
 nginx http uwsgi temporary files: “uwsgi_temp”
 nginx http scgi temporary files: “scgi_temp”
1.4 如果你想使用openssl 功能,sha1 功能。 那麼安裝openssl ,sha1 吧
[[email protected] nginx-1.10.2]# yum install openssl openssl-devel
[[email protected] nginx-1.10.2]# install perl-Digest-SHA1.x86_64
1.4.1 開啟ssl 模組 執行./configure –with-http_ssl_module
[[email protected] nginx-1.10.2]# ./configure --with-http_ssl_module
1.4.2 啟用“server+status”頁,執行./configure –with-http_stub_status_module
1 [[email protected] nginx-1.10.2]# ./configure --with-http_stub_status_module
上面兩個命令同時啟動可以
1 [[email protected] nginx-1.10.2]# ./configure --with-http_stub_status_module --with-http_ssl_module
1.5 上面configure就通過了
執行make 命令,執行make install 命令
[[email protected] nginx-1.10.2]# make
[[email protected] nginx-1.10.2]# make install
至此,nginx 執行成功了
1.6 配置環境變數
在/etc/profile 中加入配置 
開啟配置檔案
1 [[email protected] nginx-1.10.2]# vi /etc/profile
在配置檔案中加入
1
2
3 #nginx configure
export NGINX_HOME=/usr/local/nginx-1.10.2
export PATH= P A T H : PATH: NGINX_HOME/sbin
我開始像上面填寫,結果nginx -v的時候查詢不到。注意到上面我的nginx_home配置的地址不對。先找到nginx的安裝地址
1
2 [[email protected] nginx-1.10.2]# whereis nginx
nginx: /usr/local/nginx
還真是地址寫錯了,把上面的改成
1
2
3 #nginx configure
export NGINX_HOME=/usr/local/nginx
export PATH= P A T H : PATH: NGINX_HOME/sbin
編譯完儲存退出並執行
1 [[email protected] nginx-1.10.2]# source /etc/profile

使配置生效。
1.7 檢視nginx版本
1
2 [[email protected] nginx]# nginx -v
nginx version: nginx/1.10.2
整個過程成功了!
2.1 啟動nginx
我的nginx服務在http://10.1.135.67/,配置成功後,現在啟動nginx
[[email protected] nginx]# cd /usr/local/nginx
[[email protected] nginx]# nginx -c conf/nginx.conf
啟動成功,在瀏覽器開啟http://10.1.135.67/,預設埠號80.

如上圖,nginx已經正常工作了。
遇到問題:瀏覽器訪問不通的問題

很多時候,安裝完成後,服務也啟動了
但是訪問不了
看看是不是防火牆開啟了;
本地試下埠是否可訪問telnet 192.168.4.155 80

本地機器測試nginx是否能正常啟動
所謂的本地機器,就是你安裝了nginx軟體的那一臺機器,輸入命令:
curl http://192.168.241.129/
這裡的ip地址為你本機的ip地址然後會有結果:
[[email protected] nginx-1.12.1]# curl http://192.168.241.129/Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

[[email protected] nginx-1.12.1]# 以上驗證了本地是已經安裝成功的了, 但是我們遠端訪問的時候就是訪問不了,一直說拒絕訪問,那就是可能真的跟埠,防火牆有關了。 防火牆、埠開啟設定 開啟80埠: vi /etc/sysconfig/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT CentOS 7預設使用的是firewall作為防火牆,這裡改為iptables防火牆。 firewall: systemctl start firewalld.service#啟動firewall systemctl stop firewalld.service#停止firewall systemctl disable firewalld.service#禁止firewall開機啟動

Iptables stop