Linux安装nginx(亲测)

安装nginx相关依赖软件

1.选定源码目录 选定目录 /usr/local/ 
cd /usr/local/

2.安装PCRE库

yum install pcre-8.21

3.安装zlib库

cd /usr/local/
wget http://www.zlib.net/zlib-1.2.11.tar.gz    
tar -xvf zlib-1.2.11.tar.gz 
cd zlib-1.2.8
./configure
make
make install

4.安装ssl

cd /usr/local/
wget https://www.openssl.org/source/openssl-1.0.2t.tar.gz
tar -xvf openssl-1.0.2t.tar.gz 
cd openssl-1.0.2t
./config
make
make install

5.安装nginx Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤: 
cd /usr/local/
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -xvf nginx-1.14.0.tar.gz 
 cd nginx-1.14.0/
./configure --prefix=/usr/local/nginx
make
make install

  使用 /usr/local/nginx/sbin/nginx -t可查看应用nginx.conf的位置

6.the HTTP rewrite module requires the PCRE library报错

./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=<path> option.

  安装pcre-devel解决问题

 yum -y install pcre-devel
7.启动 确保系统的 80 端口没被其他程序占用
/usr/local/nginx/sbin/nginx

  检查是否启动成功:

netstat -ano|grep 80 有结果输入说明启动成功

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

  8.重启

/usr/local/nginx/sbin/nginx –s reload

9.修改配置文件

cd /usr/local/nginx/conf
vi nginx.conf
#user  nobody;
worker_processes  8;

error_log  /home/nginx/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  /home/nginx/logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;

    #gzip  on;
    
    client_max_body_size 35M;
    client_header_buffer_size 64k;
    large_client_header_buffers 4 64k;
    client_body_buffer_size 20m;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 128k;
    fastcgi_busy_buffers_size 256k;
    gzip_buffers 16 8k;
    proxy_buffer_size 64k;
    proxy_buffers 4 128k;
    proxy_busy_buffers_size 256k;

    keepalive_timeout 240;
    fastcgi_connect_timeout 600;
    fastcgi_send_timeout 600;
    fastcgi_read_timeout 600;

    proxy_connect_timeout 600s;
    proxy_send_timeout 1200;
    proxy_read_timeout 1200;

    proxy_http_version 1.1;
    proxy_set_header Connection "";

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

    server {
        listen       80;
        server_name  www.test.com;

        server_name_in_redirect off;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        fastcgi_connect_timeout 1200;
        location / {
           proxy_pass http://127.0.0.1:9010/;                                             
        }  
    }

    server {
        listen       80;
        server_name  www.test.cn;

        server_name_in_redirect off;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        fastcgi_connect_timeout 1200;
        location / {
           proxy_pass http://127.0.0.1:9010/;                                             
        }  
    }

}

 

 

 

 

 

 

上一篇:PHP安装及配置


下一篇:Ubuntu18.04搭建本地Git服务器(支持ssh和http协议,支持gitweb)