Centos 7 安装 解压版本 Nginx

1、环境

系统版本:Centos 7.9.2009

Nginx版本为:nginx-1.20.2.tar.gz

在安装Nginx之前,我们需要确保安装Nginx所依赖的其他程序,执行下面的命令,安装或更新相应的程序

 1 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel 

执行完成后,如果之前未安装的,则会自动安装,如果之前已经安装有旧的版本,则会被新的版本代替

 

 

2、下载

下载地址:http://nginx.org/download/nginx-1.20.2.tar.gz

假设,我们把文件下载到 /root 下面

cd /root/        #进入到/root目录
wget http://nginx.org/download/nginx-1.20.2.tar.gz   #将文件下载到当前目录

 

3、解压

 1 tar zxvf nginx-1.20.2.tar.gz -C /usr/local/  #将压缩包解压到 /usr/local  

使用命令进入到 解压的目录

1 cd /usr/local/          #进入到 /usr/local/ 目录 
2 ls          #查看解压后的名称,可以查看到 多了一个目录为 nginx-1.20.2 的目录 
3 cd nginx-1.20.2       #进入 名称为 nginx-1.20.2 的目录

 

4、安装

再使用 ls 可以看到 目录下有 configure 的可执行文件,这个文件的作用,就是根据你系统的情况,生成makefile的,以便于下一步的编译和安装

 1 ./configure  # 不带参数,默认会安装到 /usr/local/nginx 目录,也可以 指定参数,如:./configure --prefix=/usr/data/nginx  则会 在安装的时候,安装到 /usr/data/nginx 的目录  

执行完成后,下一步便是编译和安装了

1 make
2 make install   
3 #这两行可以分开执行,也可以在同一行执行
4 #同一行执行,则是:make && make install

到此便已经是安装完成了。

 

5、自启动

很多时候,我们为了方便管理,在服务器重启后,需要nginx自动启动,那么我们可以添加 nginx 的服务

 1 vi /lib/systemd/system/nginx.service #创建 nginx 服务文件    

nginx 的服务文件配置可参考如下:

 1 [Unit]
 2 Description=nginx
 3 After=network.target
 4 
 5 [Service]
 6 Type=forking
 7 ExecStart=/usr/local/nginx/sbin/nginx
 8 ExecReload=/usr/local/nginx/sbin/nginx -s reload
 9 ExecStop=/usr/local/nginx/sbin/nginx -s stop
10 PrivateTmp=true
11 
12 [Install]
13 WantedBy=multi-user.target

 

完成后,按ESC键,输入:wq 保存并退出,上面的nginx 相应的目录,需要改为你自己的目录。

服务文件配置好了,接下来要把它添加到服务里面

 1 systemctl enable nginx.service   

执行完后,系统会在下方提示:

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

看到这个,nginx 的服务 就已经完成添加,但这个时候,还没有启动的,我们可以通过下面的命令来操作nginx

 1 systemctl status nginx # 查看运行状态  

 

如果是正在运行,则会输出:

● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2022-02-23 13:53:56 CST; 42min ago
Process: 15270 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 15271 (nginx)
CGroup: /system.slice/nginx.service
├─15271 nginx: master process /usr/local/nginx/sbin/nginx
└─15272 nginx: worker process

2月 23 13:53:56 cloudback systemd[1]: Starting nginx...
2月 23 13:53:56 cloudback systemd[1]: Started nginx.

 

其他命令

systemctl start nginx      #启动nginx 
systemctl stop nginx         #停止nginx 
systemctl reload nginx     #重新加载 nginx如 

如果重新修改 nginx.service 配置文件后,则需要使用下面的命令来重新加载服务的配置文件

 1 systemctl daemon-reload #重新加载服务的配置文件  

 

6、nginx 配置

使用这种安装方式,nginx的配置文件,通常会在 /usr/local/nginx/conf/nginx.conf 这个位置,我们可以通过 vi 或 vim 修改

 1 vim /usr/local/nginx/conf/nginx.conf #打开配置文件  

 

我这里对日志的目录和站点的配置,作了修改:

 1 #user  nobody;
 2 worker_processes  1;
 3 
 4 error_log   /var/log/nginx/error.log warn;               #日志目录
 5 #error_log  logs/error.log  notice;
 6 #error_log  logs/error.log  info;
 7 
 8 pid         /var/run/nginx.pid;
 9 
10 
11 events {
12     worker_connections  1024;
13 }
14 
15 
16 http {
17     include       /usr/local/nginx/conf/mime.types;
18     default_type  application/octet-stream;
19 
20     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
21                       '$status $body_bytes_sent "$http_referer" '
22                       '"$http_user_agent" "$http_x_forwarded_for"';
23 
24     access_log   /var/log/nginx/access.log  main;                #访问日志
25 
26     sendfile        on;
27     #tcp_nopush     on;
28 
29     #keepalive_timeout  0;
30     keepalive_timeout  65;      #长连接时间,秒数
31 
32     gzip  on;    #开启压缩
33 
34     include /usr/local/nginx/conf.d/*.conf;           #其他站点的配置,可以分多个文件,有利于管理
35 
36 }

 

conf.d 目录下的配置文件 示例:

 1 # /usr/local/nginx/conf.d/demo.conf
 2 server {
 3     listen 80;
 4     client_max_body_size 10240M;
 5     location / {
 6         root /var/www/app/web/;
 7         proxy_pass http://localhost:8080;
 8         proxy_http_version 1.1;
 9         proxy_set_header Upgrade $http_upgrade;
10         proxy_set_header Connection keep-alive;
11         proxy_set_header Host $host;
12         proxy_cache_bypass $http_upgrade;
13         proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
14         proxy_set_header   X-Forwarded-Proto $scheme;
15         proxy_connect_timeout       3600;
16         proxy_send_timeout          3600;
17         proxy_read_timeout          3600;
18         send_timeout                3600;
19     }
20 }

 

 

欢迎讨论,欢迎指正,谢谢。

 

posted @ 2022-02-23 15:21  鹅城小将  阅读(711)  评论(0编辑  收藏  举报