腾讯云服务centOS配置nginx服务

64 阅读5分钟

这里用到的nginx版本号是 nginx 1.21.0

安装nginx

1、根目录下安装依赖

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

2、下载解压包到指定位置,比如我放在了data下面

#切换目录
[root@localhost ~]# cd /data
#创建一个文件夹
[root@localhost ~]# mkdir nginx   
#切换目录
[root@localhost ~]# cd nginx
#下载
[root@localhost nginx]# wget http://nginx.org/download/nginx-1.21.0.tar.gz
#解压
[root@localhost nginx]# tar -xvf nginx-1.21.0.tar.gz

3、安装nginx

#进入nginx目录
[root@localhost nginx]# cd /data/nginx
#进入目录
[root@localhost nginx]# cd nginx-1.21.0
#执行命令 指明h2模块 配置: listen 443 ssl http2 default_server;
[root@localhost nginx-1.21.0]# ./configure --prefix=/usr/local/nginx --with-http_v2_module
#执行make命令
[root@localhost nginx-1.21.0]# make
#执行make install命令
[root@localhost nginx-1.21.0]# make install

4、配置nginx.conf

#备份Nginx配置文件
[root@localhost conf]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
#打开配置文件,也可用vscode直接编辑该文件
[root@localhost conf]# vi /usr/local/nginx/conf/nginx.conf

5.运行以下命令,进入Nginx的sbin目录,然后启动Nginx

[root@localhost nginx]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx

6.nginx常用命令

cd /usr/local/nginx/sbin      首先进入 sbin 目录
./nginx              启动 Nginx
./nginx -s stop      停止 Nginx
./nginx -s reload    重新加载 Nginx
./nginx -v           查看 Nginx 版本

7.查看nginx是否成功启动了

[root@localhost sbin]# ps -ef | grep nginx
root       4338      1  0 16:48 ?        00:00:00 nginx: master process ./nginx
nobody     4339   4338  0 16:48 ?        00:00:00 nginx: worker process
root       4341   1549  0 16:48 pts/0    00:00:00 grep --color=auto nginx

8.配置文件修改后,需要指定配置文件进行重启

#定配置文件进行重启
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
#检测文件是否配置正确
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
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

9.创建一个软链接启动nginx

#建立软链接
[root@localhost sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
#启动
[root@localhost sbin]# nginx
#查看nginx版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.21.0

10.访问服务器ip+端口查看

http://你的ip

image.png

配置https服务

1.打开nginx的配置文件,nginx.conf

可以申请免费证书,免费使用一年,到期后重新申请。有需要的话可以买ssl证书


# 修改成root权限后,nginx的服务访问就不会出现403了
user  root;
worker_processes  1;

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

    gzip  on;

    # HTTPS server
    #
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        # listen       443 ssl;
        			#域名名称
        server_name  _;
								#证书位置(我这是在conf下新建了一个ssl文件放这两个文件)
        ssl_certificate      ssl/证书名字.crt;
        						#密钥位置
        ssl_certificate_key  ssl/证书名字.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers PROFILE=SYSTEM;
        # ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        # 资源根路径
        root         /usr/share/nginx/html/dist;
        location / {
        }
        location /static/ {
            alias /usr/share/nginx/html/static/;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}


如果nginx没有web目录的权限,会出现403 可以把user改成root,或者每次手动修改web目录权限

chmod -R 777 /data/你的目录

配置开机自动启动

11.配置nginx开机自启动

i. 创建一个nginx.service,按i键进入编辑模式,修改配置文件,按Esc键,然后输入:wq并按Enter键以保存

#进入目录
[root@localhost system]# cd /usr/lib/systemd/system/
#创建文件
[root@localhost system]# touch nginx.service
#编辑文件内容
[root@localhost system]# vi /usr/lib/systemd/system/nginx.service
#赋予可执行的权限
[root@localhost system]# chmod +x /usr/lib/systemd/system/nginx.service

ii. 编辑文件内容

[Unit]                                                                                      #对服务的说明
Description=nginx - high performance web server              #描述服务
After=network.target remote-fs.target nss-lookup.target   #描述服务类别

[Service]                                                                                 #服务的一些具体运行参数的设置
Type=forking                                                                         #后台运行的形式
PIDFile= /usr/local/nginx/logs/nginx.pid                               #PID文件的路径
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf   #启动准备
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf           #启动命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload                                                 #重启命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop                                                       #停止命令
ExecQuit=/usr/local/nginx/sbin/nginx -s quit                                                        #快速停止
PrivateTmp=true                                                                  #给服务分配临时空间

[Install]
WantedBy=multi-user.target                                               #服务用户的模式

iii. 查看文件内容

[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile= /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

iiii. 启动服务

#在启动服务之前,需要先重载systemctl命令
[root@localhost ~]# systemctl daemon-reload
#启动服务或者使用systemctl start nginx
[root@localhost ~]# systemctl start nginx.service
#运行以下命令设置Nginx服务开机自启动
[root@localhost ~]# systemctl enable nginx
#查看nginx
[root@localhost ~]# ps -ef | grep nginx
root      14869      1  0 17:37 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    14871  14869  0 17:37 ?        00:00:00 nginx: worker process
root      14893   1549  0 17:37 pts/0    00:00:00 grep --color=auto nginx

*配置systemctl之后的启动方式

systemctl status nginx  #状态
systemctl start nginx  #启动
systemctl stop nginx   #停止
systemctl restart nginx  #重启

iiiii. 使用reboot命令重启后,查看nginx是否成功的自启动了

[root@localhost ~]# reboot
Connection to 192.168.0.197 closed by remote host.
Connection to 192.168.0.197 closed.

C:\Users\beauty>ssh root@192.168.0.197
root@192.168.0.197's password:
Last login: Sat Jun  5 16:09:35 2021 from 192.168.0.194
[root@localhost ~]# ps -ef | grep nginx
root       1081      1  0 17:39 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     1084   1081  0 17:39 ?        00:00:00 nginx: worker process
root       1567   1548  0 17:40 pts/0    00:00:00 grep --color=auto nginx

引用 zhuanlan.zhihu.com/p/378409850