1. 程式人生 > >Linux下NGINX安裝

Linux下NGINX安裝

Linux版本: CentOS 6.5 64位

一. 安裝編譯工具及庫檔案

# yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

二. 安裝PCRE,讓NGINX支援Rewrite功能

# yum install pcre

三. 安裝NGINX

- 3.1 下載NGINX

# wget http://nginx.org/download/nginx-1.13.7.tar.gz

- 3.2 解壓

# tar -zxvf nginx-1.13.7.tar.gz

- 3.3 進入安裝包目錄

# cd nginx-1.13.7

- 3.4 編譯安裝

# ./configure && make && make clean

- 3.5 檢視安裝路徑並進入

[root@localhost nginx-1.13.7]# whereis nginx
nginx: /usr/local/nginx
[root@localhost nginx-1.13.7]# cd /usr/local/nginx

- 3.6 啟動NGINX

[root@localhost nginx]# sbin/nginx

啟動後可在瀏覽器上訪問 host:port,如 127.0.0.1:80 。若開啟後出現如下頁面則安裝完成
安裝完成

四. 安裝過程中遇到的問題

- 4.1 使用make編譯時報錯

提示資訊:

在包含自 src/core/ngx_core.h71 的檔案中,
                 從 src/core/nginx.c9:
src/core/ngx_regex.h:15:18: 錯誤:pcre.h:沒有那個檔案或目錄
In file included from src/core/ngx_core.h:71,
                 from src/core/nginx.c:9:
src/core/ngx_regex.h:24: 錯誤:expected specifier-qualifier-list before ‘pcre’

原因及解決方法:
未安裝pcre-devel

# yum install pcre-devel

更改後再次執行出錯,提示資訊:

cd /srv/pcre2-10.21 \
        && make libpcre.la
make[2]: Entering directory `/srv/pcre2-10.21'
make[2]: *** 沒有規則可以建立目標“libpcre.la”。 停止。
make[2]: Leaving directory `/srv/pcre2-10.21'
make[1]: *** [/srv/pcre2-10.21/.libs/libpcre.a] 錯誤 2
make[1]: Leaving directory `/srv/nginx-1.13.7'

原因及解決方法:
將pcre安裝成了pcre2,NGINX不支援pcre2,換成pcre

- 4.2 啟動NGINX失敗

提示資訊:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

原因及解決方法:
NGINX所使用的埠號被佔用,預設埠為80,可以在配置檔案中修改或者關閉佔用埠的程序

NGINX配置檔案如下:

[root@localhost conf]# vi nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
"nginx.conf" 117L, 2656C written

將其中的 server 中的 listen 後的數值更改為想使用的埠號即可更改NGINX所使用的埠