Nginx安装与配置

1.下载安装包

官网下载地址:nginx: download

Nginx安装与配置_第1张图片

        可以先将安装包下载到本地再传到服务器,或者直接用wget命令将安装包下载到服务器,这里我们直接将安装包下载到服务器上。未安装wget命令的需要先安装wget,yum install -y wget

[root@reader app]# cd /usr/soft/
[root@reader soft]# wget -c https://nginx.org/download/nginx-1.24.0.tar.gz
--2023-11-03 22:35:48--  https://nginx.org/download/nginx-1.24.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1112471 (1.1M) [application/octet-stream]
Saving to: ‘nginx-1.24.0.tar.gz’

nginx-1.24.0.tar.gz                            100%[===================================================================================================>]   1.06M  1.45MB/s    in 0.7s    

2023-11-03 22:35:50 (1.45 MB/s) - ‘nginx-1.24.0.tar.gz’ saved [1112471/1112471]

[root@reader soft]# 

解压安装包

[root@reader soft]# tar -xvf nginx-1.24.0.tar.gz 

Nginx安装与配置_第2张图片

2.安装C语言编译环境及相关依赖

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

3.执行./configure配置编译环境

        如果我们要指定nginx的安装路径(默认路径:/usr/local/nginx)或是需要添加相关nginx模块,在./configure 命令后需要添加相关参数,可以同时带多个参数。

        可以使用 ./configure --help 命令查看相关参数/指令:

[root@reader nginx-1.24.0]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory
.....

        我这里需要添加 --with-threads --with-http_ssl_module --with-http_gzip_static_module,大家根据个人需要进行添加,如果首次安装未添加,后续还是可以重新编译的(见另一篇博文)。

[root@reader nginx-1.24.0]# ./configure --prefix=/usr/local/nginx  --with-threads  --with-http_ssl_module  --with-http_gzip_static_module

4.编译安装nginx

make & make install

5.将nginx命令添加到全局命令(按需设置)

[root@reader etc]# vim /etc/profile

# /etc/profile
  
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc


·····
export PATH=$PATH:${JAVA_PATH}

export MYSQL_HOME="/usr/local/mysql"
export PATH="${MYSQL_HOME}/bin:${PATH}"

export PATH=$PATH:/usr/local/nginx/sbin

添加完成后执行如下命令使配置生效

source /etc/profile

6.启动nginx

root@reader etc]# nginx
[root@reader etc]# ps -ef | grep nginx
root      7156     1  0 23:45 ?        00:00:00 nginx: master process nginx
nobody    7157  7156  0 23:45 ?        00:00:00 nginx: worker process
root      7161  4040  0 23:48 pts/0    00:00:00 grep --color=auto nginx

        如果未添加全局命令,就进入安装目录的sbin目录下执行./nginx

[root@reader sbin]# cd /usr/local/nginx/sbin/
[root@reader sbin]# ./nginx 
[root@reader sbin]# ps -ef | grep nginx
root      7175     1  0 Nov03 ?        00:00:00 nginx: master process ./nginx
nobody    7176  7175  0 Nov03 ?        00:00:00 nginx: worker process
root      7178  4040  0 00:00 pts/0    00:00:00 grep --color=auto nginx
[root@reader sbin]#

7.访问nginx

浏览器输入IP访问nginx,如果现实下图,说明配置并启动成功。

Nginx安装与配置_第3张图片

8.nginx 常用命令

[root@reader sbin]# nginx -h
nginx version: nginx/1.24.0
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit    # 查看版本号
  -V            : show version and configure options then exit    # 查看版本及配置
  -t            : test configuration and exit              # 检查配置文件是否有问题
  -T            : test configuration, dump it and exit     # 检查配置文件是否有问题并转储
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)   #设置前缀路径(默认是:/usr/share/nginx/)
  -e filename   : set error log file (default: logs/error.log)   #设置/指定error_log输出位置,默认位置安装目录下的 logs/error.log
  -c filename   : set configuration file (default: conf/nginx.conf)  #设置/指定配置文件配置文件,默认配置文件在安装目录下的 conf/nginx.conf
  -g directives : set global directives out of configuration file  #在配置文件之外设置全局指令

[root@reader sbin]# 

-s signal 解释

nginx -s reload  # 重新加载配置文件,关闭旧进程,启用新进程
nginx -s reopen	 # 重新打开日志文件
nginx -s stop    # 强制停止nginx
nginx -s quit    # 等待所有请求都处理完后再关闭nginx

        注意:单独说明 nginx -s reopen命令,执行该命令时nginx会先检查原日志文件(error.log 或 access.log)是否存在,如果存在,日志还是正常写入原文件中。如果原日志文件不存在或名称已修改,这时nginx才会重新生成一个空日志文件并将日志写入。亲测结果。

[root@reader sbin]# cd ../logs/
[root@reader logs]# ll
total 12
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  378 Nov  4 00:40 error.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[root@reader logs]# nginx -s reopen & ll
[1] 7320
total 12
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  378 Nov  4 00:40 error.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[root@reader logs]# nginx -s reopen & ll
[2] 7322
total 12
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  441 Nov  4 00:41 error.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[1]   Done                    nginx -s reopen
[root@reader logs]# nginx -s reopen & ll
[3] 7324
total 12
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  504 Nov  4 00:42 error.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[2]   Done                    nginx -s reopen
[root@reader logs]# mv error.log error_1.log & ll
[4] 7330
total 12
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[3]   Done                    nginx -s reopen
[4]+  Done                    mv -i error.log error_1.log
[root@reader logs]# ll
total 12
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[root@reader logs]# nginx -s reopen & ll
[1] 7334
total 12
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[root@reader logs]# ll
total 16
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 nobody root   63 Nov  4 00:43 error.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[1]+  Done                    nginx -s reopen
[root@reader logs]# 
[root@reader logs]# ll
total 16
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 nobody root   63 Nov  4 00:43 error.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[root@reader logs]# ll
total 16
-rw-r--r-- 1 nobody root 1890 Nov  4 00:37 access.log
-rw-r--r-- 1 nobody root  567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 nobody root   63 Nov  4 00:43 error.log
-rw-r--r-- 1 root   root    5 Nov  3 23:59 nginx.pid
[root@reader logs]# 
[root@reader logs]# 
[root@reader logs]# rm -rf access.log 
[root@reader logs]# ll
total 12
-rw-r--r-- 1 nobody root 567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 nobody root  63 Nov  4 00:43 error.log
-rw-r--r-- 1 root   root   5 Nov  3 23:59 nginx.pid
[root@reader logs]# nginx -s reopen
[root@reader logs]# ll
total 12
-rw-r--r-- 1 nobody root   0 Nov  4 00:43 access.log
-rw-r--r-- 1 nobody root 567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 nobody root 126 Nov  4 00:43 error.log
-rw-r--r-- 1 root   root   5 Nov  3 23:59 nginx.pid
[root@reader logs]# 
[root@reader logs]# mv access.log access_1.log
[root@reader logs]# ll
total 12
-rw-r--r-- 1 nobody root   0 Nov  4 00:43 access_1.log
-rw-r--r-- 1 nobody root 567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 nobody root 126 Nov  4 00:43 error.log
-rw-r--r-- 1 root   root   5 Nov  3 23:59 nginx.pid
[root@reader logs]# nginx -s reopen
[root@reader logs]# 
[root@reader logs]# ll
total 12
-rw-r--r-- 1 nobody root   0 Nov  4 00:43 access_1.log
-rw-r--r-- 1 nobody root   0 Nov  4 00:44 access.log
-rw-r--r-- 1 nobody root 567 Nov  4 00:42 error_1.log
-rw-r--r-- 1 nobody root 189 Nov  4 00:44 error.log
-rw-r--r-- 1 root   root   5 Nov  3 23:59 nginx.pid
[root@reader logs]# 

9.设置nginx开机自启动

        源码解压编译安装的需要手动在/lib/systemd/system目录下创建nginx.service文件。

        nginx.service文件内容如下

[Unit]
Description=nginx service
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
上述文件各行解释说明
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

        创建完成后,执行如下命令,设置开机启动。

systemctl enable nginx.service

        查看开机启动项,检查是否设置成功。

systemctl list-unit-files | grep nginx
[root@reader logs]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@reader logs]# 
[root@reader logs]# 
[root@reader logs]# 
[root@reader logs]# systemctl list-unit-files | grep nginx
nginx.service                               enabled  
[root@reader logs]# 

其它命令

启动nginx服务 :  systemctl start nginx.service
停止nginx服务 :  systemctl stop nginx.service
设置开机自启动 :  systemctl enable nginx.service
停止开机自启动 :  systemctl disable nginx.service
查看服务当前状态: systemctl status nginx.service
重新启动服务   :  systemctl restart nginx.service
查看所有已启动服务:systemctl list-units --type=service

你可能感兴趣的:(nginx,linux,服务器)