Ubuntu nginx安装、配置(16.04、18.04)

命令行安装方式

sudo apt install nginx
#nginx配置默认目录 /etc/nginx/sites-available

其他安装方式

安装nginx依赖库

#安装gcc g++的依赖库
apt-get install build-essential
apt-get install libtool
#安装pcre依赖库
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
#安装zlib依赖库
apt-get install zlib1g-dev
#安装ssl依赖库
apt-get install openssl

安装nginx

#下载最新版本
wget http://nginx.org/download/nginx-1.11.3.tar.gz
#解压:
tar -zxvf nginx-1.11.3.tar.gz
#进入解压目录:
cd nginx-1.11.3
#配置:
./configure --prefix=/usr/local/nginx 
#编辑nginx
make
#linux18.04报错,解决办法参考[https://blog.csdn.net/u010889616/article/details/82867091](https://blog.csdn.net/u010889616/article/details/82867091)
#安装nginx
make install
#启动nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
#查看nginx进程
ps -ef|grep nginx

配置nginx.conf(要跑的程序放在Nginx安装目录下的html文件夹里即可,也可在下面的nginx.conf中配置访问路径)

cd /usr/local/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;
        }

        #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;
        #}
    }
    
    server {
        listen       8080;
        server_name  ****;  #需要做反向代理的域名

        location / {
        
      root   html;
            
      index  index.html index.htm index.js index.php;
            
      proxy_redirect off;  

      #proxy_set_header Host $host;  

      proxy_set_header X-Real-IP $remote_addr;  

      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  

      proxy_pass ****;  #阿里云服务器地址
        }
    }


    # 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负载均衡

upstream serv{
    server ip1:80 weight=1;
    server ip2:80 weight=1;
}

server {
    listen 80;

    server_name  ip;

    charset utf8;

    location / {
        proxy_pass        http://serv;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;
    }
}

nginx基本命令

#启动nginx服务器
/usr/local/nginx/sbin/nginx
service nginx start
#重启nginx服务器
/usr/local/nginx/sbin/nginx -s reopen
service nginx restart
#重新加载配置文件
/usr/local/nginx/sbin/nginx -s reload
service nginx reload
#停止nginx服务器
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s quit
service nginx stop
#查看nginx进程
ps -ef|grep nginx
#杀进程
kill -9 进程号
#杀掉所有nginx进程
sudo killall nginx

你可能感兴趣的:(Ubuntu nginx安装、配置(16.04、18.04))