CentOS7下Nginx的安裝與配置

Nginx("engine x")是一個高性能的HTTP反向代理web服務器,同時也可以作爲IMAP/POP3/SMTP的代理服務器來使用。

Nginx安裝

安裝工具庫

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

安裝 PCRE

PCRE(Perl Compatible Regular Expressions,perl語言兼容的正則表達式), Nginx的Rewrite功能中需要使用到 PCRE 來對路由地址進行正則匹配。

[root@hadoop39 ~]# cd /usr/local/src/
[root@hadoop39 src]# wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.44/pcre-8.44.tar.gz
[root@hadoop39 src]# tar -zxvf pcre-8.44.tar.gz
[root@hadoop39 src]# cd pcre-8.44/
[root@hadoop39 pcre-8.44]# ./configure --prefix=/usr/local/pcre-8.44
[root@hadoop39 pcre-8.44]# make && make install
[root@hadoop39 pcre-8.44]# pcre-config --version
8.44

下載Nginx安裝包

Nginx官網下載網址:http://nginx.org/en/download.html ,如圖所示,推薦下載Nginx最新的穩定版安裝包。

nginx-1.18.0 的下載鏈接爲:http://nginx.org/download/nginx-1.18.0.tar.gz

如果需要獲取Nginx的其他版本,可以訪問 http://nginx.org/download/

[root@hadoop39 ~]# cd /usr/local/src/
[root@hadoop39 src]# wget http://nginx.org/download/nginx-1.18.0.tar.gz

解壓安裝包

[root@hadoop39 src]# tar -zxvf nginx-1.18.0.tar.gz
[root@hadoop39 src]# cd nginx-1.18.0/

編譯安裝

[root@hadoop39 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@hadoop39 nginx-1.18.0]# make
[root@hadoop39 nginx-1.18.0]# make install
[root@hadoop39 nginx-1.18.0]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
[root@hadoop39 nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
[root@hadoop39 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@hadoop39 html]# nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

缺省配置

#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;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

啓動Nginx

[root@hadoop39 ~]# nginx

啓動成功之後,通過IP訪問Nginx服務器。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章