Nginx 服务器的安装部署

1、安装 Nginx 服务器和基本配置

 在线安装所需工具:

yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel

 在文件系统的“/usr/local/nginx-1.16.1”目录下新建nginx-1.16.1目录,最后会把编译好的Nginx安装到此目录中。同时,在此目录中新建nginx-1.6.3-compile,用来编译Nginx 软件

   使用以下命令配置并生成Makefile 文件:

[root@localhost nginx-1.16.1]# pwd
/usr/local/nginx/nginx-compile-1.16.1/nginx-1.16.1

[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx/nginx-1.16.1

 生成的 Nginx 软件的Makefile文件就保存在当前的工作目录。

 得到 Nginx 的 Makefile 文件后,在nginx-1.16.1目录下使用 make 文件进行编译:

[root@localhost nginx-1.16.1]# make

    使用 make 的 install命令安装 nginx软件:

[root@localhost nginx-1.16.1]# make install

 对 Nginx服务器安装后的全部资源进行查看:

[root@localhost nginx-1.16.1]# ll
total 4
drwxr-xr-x. 2 root root 4096 Mar  3 18:59 conf
drwxr-xr-x. 2 root root   40 Mar  3 18:59 html
drwxr-xr-x. 2 root root    6 Mar  3 18:59 logs
drwxr-xr-x. 2 root root   19 Mar  3 18:59 sbin
  • conf 目录存放了所有配置文件。其中,nginx.conf文件是 Nginx服务器的主配置文件,其它配置文件是用来配置Nginx 的相关功能的。在此目录下,所有的配置文件都提供以.default结尾的默认配置文件,方便我们将配置过的.conf文件恢复到初始状态。
  • html 目录存放了Nginx服务器在运行过程中调用的一些 html 文件。
  • logs 目录用来存放Nginx服务器日志的。
  • sbin 目录,目前只有nginx一个文件,这就是Nginx服务器的主程序

Nginx 服务的启动

  启动 Nginx 服务器直接运行安装目录下 sbin目录中的二进制文件,Nginx 服务启动命令:

[root@localhost nginx-1.16.1]# ./sbin/nginx 
[root@localhost nginx-1.16.1]# ps aux | grep nginx
root      37493  0.0  0.0  20556   616 ?        Ss   19:17   0:00 nginx: master process ./sbin/nginx
nobody    37494  0.0  0.0  23092  1396 ?        S    19:17   0:00 nginx: worker process

Nginx 服务的停止

  停止 Nginx 有两种方法:一种是快速停止;一种是平缓停止。快速停止是指立即停止当前Nginx 服务正在处理的所有网络请求,马上丢弃连接,停止工作;平缓停止是指允许 Nginx服务将当前正在处理的网络请求处理完成,但不再接受新的请求,之后关闭连接,停止工作。

[root@localhost nginx-1.16.1]# ps aux | grep nginx
root      37493  0.0  0.0  20556   616 ?        Ss   19:17   0:00 nginx: master process ./sbin/nginx
nobody    37494  0.0  0.0  23092  1396 ?        S    19:17   0:00 nginx: worker process
root      37541  0.0  0.0 112712   960 pts/0    R+   19:17   0:00 grep --color=auto nginx
[root@localhost nginx-1.16.1]# kill -INT 37493   / kill -TERM 37493    /  kill -QUIT 37493

     TERM 和 INT 信号用于快速停止,QUIT 用于平缓停止。

Nginx 服务的重启

  平滑重启是这样一个过程,Nginx服务进程接受到信号后,首先读取新的Nginx 配置文件,如果配置语法正确,则启动新的 Nginx 服务,然后平滑关闭旧的服务进程;如果新的 Nginx 配置有问题,将显示错误,仍然使用旧的Nginx进程提供服务。

  使用以下命令实现Nginx服务的平滑重启:
# ./sbin/nginx -g HUP [-c newConfFile]

  HUP 信号用于发送平滑重启信号。
  newConfFile,可选项,用于指定新配置文件的路径。
  或者,使用新的配置文件代替了旧的配置文件后,使用

# kill HUP `/Nginx/logs/nginx.pid`

  也可以实现平滑重启。

nginx.conf的内容和基本配置方法

  默认的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;        #在 events部分中生效
}


http {
    include       mime.types;        #以下指令在 http部分中生效
    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 {       #以下指令在http 的server部分中生效
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {        #以下指令在http/server的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;
    #    }
    #}

}

   参考:

   https://www.jianshu.com/p/320a48fcef57

posted on 2022-04-02 10:35  溪水静幽  阅读(810)  评论(0)    收藏  举报