centos7 安装 Nginx

181 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

卸载 centos7 yum安装的nginx

停止Nginx软件

service nginx stop [root@BizMsTestAlpha core]# service nginx stop Redirecting to /bin/systemctl stop nginx.service

删除Nginx的自动启动

chkconfig nginx off [root@BizMsTestAlpha core]# chkconfig nginx off Note: Forwarding request to 'systemctl disable nginx.service'. Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service.

从源头删除Nginx rm -rf /usr/sbin/nginx rm -rf /etc/nginx rm -rf /etc/init.d/nginx 再使用yum清理 yum remove nginx 删除安装的内容 rm -rf /usr/local/nginx (注:yum安装的应该是安装在 /usr/local 下) 查找删除一些残留文件 [root@BizMsTestAlpha core]# find / -name "nginx" /usr/lib64/nginx /usr/share/nginx /var/lib/nginx /var/log/nginx [root@BizMsTestAlpha core]#

删除上面目录文件 如果搜索到docker容器里面的nginx信息 就不要去删了

一、Nginx 的 SSL 模块安装 查看 nginx 是否安装 http_ssl_module 模块。 $ /usr/local/nginx/sbin/nginx -V 如果出现 configure arguments: –with-http_ssl_module, 则已安装(下面的步骤可以跳过,进入 nginx.conf 配置)。 下载 Nginx 安装包,当然是要去 Nginx 官网下载。

下载安装包到 src 目录

yum install wget -y cd/usr/local/srccd /usr/local/src wget nginx.org/download/ng… 解压安装包。 $ tar -zxvf nginx-1.15.9.tar.gz

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel openldap-clients openldap-servers libxslt-devel libevent-devel ntp libtool-ltdl bison libtool vim-enhanced

配置 SSL 模块。 cdnginx1.15.9cd nginx-1.15.9 ./configure --prefix=/usr/local/nginx --with-http_ssl_module 使用 make 命令编译(使用make install会重新安装nginx),此时当前目录会出现 objs 文件夹。 make & make install 用新的 nginx 文件覆盖当前的 nginx 文件。 cp ./objs/nginx /usr/local/nginx/sbin/ 再次查看安装的模块(configure arguments: –with-http_ssl_module说明ssl模块已安装)。 $ /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.15.9 ... configure arguments: --with-http_ssl_module 二、SSL 证书部署 这里使用的是阿里云的免费证书,期限为1年,申请地址在此。 下载申请好的 ssl 证书文件压缩包到本地并解压(这里是用的 pem 与 key 文件,文件名可以更改)。 在 nginx 目录新建 cert 文件夹存放证书文件。 cd/usr/local/nginxcd /usr/local/nginx mkdir cert 将这两个文件上传至服务器的 cert 目录里。 这里使用 mac 终端上传至服务器的 scp 命令(这里需要新开一个终端,不要使用连接服务器的窗口): scp/Users/yourname/Downloads/ssl.pemroot@xxx.xx.xxx.xx:/usr/local/nginx/cert/scp /Users/yourname/Downloads/ssl.pem root@xxx.xx.xxx.xx:/usr/local/nginx/cert/ scp /Users/yourname/Downloads/ssl.key root@xxx.xx.xxx.xx:/usr/local/nginx/cert/ scp [本地文件路径,可以直接拖文件至终端里面] [<服务器登录名>@<服务器IP地址>:<服务器上的路径>]

三、Nginx.conf 配置 编辑 /usr/local/nginx/conf/nginx.conf 配置文件: 配置 https server。注释掉之前的 http server 配置,新增 https server:

server {
    # 服务器端口使用443,开启ssl, 这里ssl就是上面安装的ssl模块
    listen       443 ssl;
    # 域名,多个以空格分开
    server_name  hack520.com www.hack520.com;
    
    # ssl证书地址
    ssl_certificate     /usr/local/nginx/cert/ssl.pem;  # pem文件的路径
    ssl_certificate_key  /usr/local/nginx/cert/ssl.key; # key文件的路径
    
    # ssl验证相关配置
    ssl_session_timeout  5m;    #缓存有效期
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;    #加密算法
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    #安全链接可选的加密协议
    ssl_prefer_server_ciphers on;   #使用服务器端的首选算法

    location / {
        root   html;
        index  index.html index.htm;
    }
}
将 http 重定向 https。
server {
    listen       80;
    server_name  hack520.com www.hack520.com;
    return 301 https://$server_name$request_uri;
}

四、重启 nginx $ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 如果 80 端口被占用,用kill [id]来结束进程:

查看端口使用

$ netstat -lntp

Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21307/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3072/sshd
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 21307/nginx: master

结束 80 端口进程

kill21307再次重启nginxkill 21307 再次重启 nginx: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 或者: service nginx restart

CentOS7.5 设置 Nginx 开机自启动

centos 7开机自启动有两种不同的设置方式,如果是yum直接安装的软件服务,则直接开启即可,如果是源码编译安装的,则需要在系统服务(system)创建service文件,然后才能设置。

一、 前言 centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。关于Systemd的详情介绍在这里。 二、yum直接安装Nginx服务 Systemd服务文件以.service结尾,比如现在要建立nginx为开机启动,如果用yum install命令安装的,yum命令会自动创建nginx.service文件,直接用命令:

systemcel enable nginx.service

三、源码编译安装 在这里我是用源码编译安装的,所以要手动创建nginx.service服务文件。开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即:

/lib/systemd/system/

1、创建nginx.service文件 在系统服务目录里创建nginx.service文件

vim /lib/systemd/system/nginx.service

写入内容如下:

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking是后台运行的形式 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3 保存退出。 2、设置开机启动

systemctl enable nginx.service

3、查看nginx状态

systemctl status nginx.service

很奇怪,明明启动成功了,为什么显示Active: inactive (dead)?

(base) [root@cssc system]# systemctl status nginx.service
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: inactive (dead)

杀死nginx重启nginx

pkill -9 nginx
ps aux | grep nginx
systemctl start nginx

再次查看状态,变成了active,搞定。

(base) [root@css system]# systemctl status nginx.service
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-05-17 13:44:25 CST; 9s ago
  Process: 114106 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 114107 (nginx)
   CGroup: /system.slice/nginx.service
           ├─114107 nginx: master process /usr/local/nginx/sbin/nginx
           └─114108 nginx: worker process

May 17 13:44:25 cssbjqnffcsvic systemd[1]: Starting nginx...
May 17 13:44:25 cssbjqnffcsvic systemd[1]: Started nginx.

4、其他命令 启动nginx服务

systemctl start nginx.service 

设置开机自启动

systemctl enable nginx.service

停止开机自启动

systemctl disable nginx.service

查看服务当前状态

systemctl status nginx.service

重新启动服务

systemctl status nginx.service

查看所有已启动的服务

systemctl list-units --type=service

5、Systemd 命令和 sysvinit 命令的对照表 在这里插入图片描述


#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;
    #上传大小限制
    client_max_body_size 5000m;
    #下载大小限制
    proxy_max_temp_file_size 5000m;
    #设置为on表示启动高效传输文件的模式
    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   /data/build/;
            add_header Cache-Control no-store;
            add_header Cache-Control no-cache;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
        
         location ^~ /static/ {
            alias   /data/build/static/;
        }
        
        location ~ /group[1-9]/M0[0-9] {    
             ngx_fastdfs_module;  
        }

        #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:vue路由使用history模式刷新404

当vue-router使用history模式时,当我们刷新页面或直接访问路径的时候就会返回404,如图。 在这里插入图片描述 在history模式下,只是动态的通过js操作window.history来改变浏览器地址栏里的路径,并没有发起http请求,但是当我们直接在浏览器输入这个地址的时候,就会对服务器发起http请求,但是这个目标在服务器上又不存在,所以会返回404。

我们可以通过把所有请求都转发到首页上来解决这个问题。

nginx配置:

location /{
    root  /website/wenfu_zszk/;
    index index.html;
 
    #解决404
    try_files $uri $uri/ /index.html;
  }

解释:

try_files:按选项所指定的顺序去检查用户请求的文件是否存在,如果本地存在的话则返回该请求;不存在的话将该请求转发到指定的其它路径。

$uri:这个变量指当前的请求URI,不包括任何参数(见$args)。

所以try_files uriuri uri/ /index.html表示查找当前请求地址对应的文件是否存在,不存在则查找请求地址对应的目录是否存在,还是不存在则重定向到/index.html页面。如:

test.com/test就会先查找te…

Nginx常用命令:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf_aiit_website

/usr/local/nginx/sbin/nginx -s reload

/usr/local/nginx/sbin/nginx -s stop

启动:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf_aiit_website 全部重启:/usr/local/nginx/sbin/nginx -s reload 退出:kill -QUIT 端口号 查看在运行的nginx:netstat -tlnp | grep nginx 查看在运行的nginx:ps -ef | grep nginx

端口号被占用的话 lsof -i:8080 // a、查看被占用的端口号8080 kill -9 63170 // b、关闭a执行后看到的服务号是63170