阅读 96

nginx安装部署-linux篇

nginx安装部署-linux篇

ps:这边是使用CentOS8系统进行安装部署。
系统安装部署不在此讲述了。

前期准备

nginx主目录:/usr/nginx

mkdir /usr/nginx

安装依赖环境:

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

linux下编译安装nginx

linux下的安装较为复杂,需要先安装一个linux发行版,然后安装环境,然后进行源码编译。

下载:
下载:, 下载的是tar.gz的压缩包。

上传到服务器
使用ftp工具上传到服务器如filezilla等软件。

解压文件

# 使用tar命令进行解压
# 目录为/usr
tar -zxvf nginx-1.21.1.tar.gz

# 解压之后目录如下,里面包含了所有的源代码等文件
[root@localhost nginx-1.21.1]# ls -la
总用量 792
drwxr-xr-x.  8 1001 1001    158 7月   6 10:59 .
drwxr-xr-x. 13 root root    164 8月  23 21:24 ..
drwxr-xr-x.  6 1001 1001   4096 8月  23 21:18 auto
-rw-r--r--.  1 1001 1001 313379 7月   6 10:59 CHANGES
-rw-r--r--.  1 1001 1001 478586 7月   6 10:59 CHANGES.ru
drwxr-xr-x.  2 1001 1001    168 8月  23 21:18 conf
-rwxr-xr-x.  1 1001 1001   2590 7月   6 10:59 configure
drwxr-xr-x.  4 1001 1001     72 8月  23 21:18 contrib
drwxr-xr-x.  2 1001 1001     40 8月  23 21:18 html
-rw-r--r--.  1 1001 1001   1397 7月   6 10:59 LICENSE
drwxr-xr-x.  2 1001 1001     21 8月  23 21:18 man
-rw-r--r--.  1 1001 1001     49 7月   6 10:59 README
drwxr-xr-x.  9 1001 1001     91 8月  23 21:18 src
[root@localhost nginx-1.21.1]# 
# src文件夹包含nginx所有源代码,man包含了nginx的帮助文档,html是静态网页内容,conf是配置文件,auto是脚本文件,configure是自动脚本程序。

编译安装需要用到configure脚本文件生成Makefile文件以及一些常用的可选选项。

--prefix= # 指定nginx的安装目录
--lock-path= # nginx.lock文件的目录
--user= # 指定运行nginx的用户,默认为nobody,不限制
--group= # 指定运行nginx的组,默认nobody,不限制
--add-module= # 指定第三方模块路径
--with-poll_module # 声明启用poll模块
--without-poll_module # 声明禁用poll模块
--with-http_ssl_module # 声明启用ssl模块,需要安装openssl库,这样服务器可以使用https请求
# 一般使用with启用模块,without禁用模块,指定nginx的安装目录即可。后期如果需要扩展需要重新编译。

创建用户,一般建议使用专门的用户创建程序和进程。

groupadd -f nginx  # 创建组
useradd -g nginx nginx # 创建组以及设置用户
usermod -s /sbin/nologin nginx #修改用户默认shell /sbin/nologin

使用configure生成Makefile文件

./configure --user=nginx --group=nginx --prefix=/usr/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_gzip_static_module --with-http_sub_module

# 指定用户nginx,用户组ngin,指定安装目录/usr/nginx以及一些模块
# 命令执行完成之后出现安装完成的目录,可以稍微留意下。

编译和安装:

# 使用make编译,时间较长
make
# 安装,使用make install安装
make install

# 安装完成之后到nginx主目录查看下,只有4个文件夹
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# ls -la
总用量 4
drwxr-xr-x.  6 root root   54 8月  24 04:27 .
drwxr-xr-x. 14 root root  177 8月  24 04:27 ..
drwxr-xr-x.  2 root root 4096 8月  24 04:27 conf
drwxr-xr-x.  2 root root   40 8月  24 04:27 html
drwxr-xr-x.  2 root root    6 8月  24 04:27 logs
drwxr-xr-x.  2 root root   19 8月  24 04:27 sbin
[root@localhost nginx]# 
# conf是配置文件,包含nginx.conf主配置文件和其他文件
# html是网页文件夹
# logs包含日志文件和pid
# sbin是可执行文件目录,即nginx主程序

nginx服务的启动和停止

nginx的启动

# 直接找到sbin下面的主程序文件
cd /sbin
./nginx  # 后面不加参数直接运行
./nginx # 后面可以加可选参数
# 如下:
[root@localhost sbin]# ./nginx -h
nginx version: nginx/1.21.1
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  # nginx程序测试启动
  -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/nginx/)
  -e filename   : set error log file (default: logs/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

查看nginx的进程

# 通过pid文件查看进程号
cd /logs
cat nginx.pid

# 通过 ps 查看进程
ps aux | grep nginx

关闭nginx进程服务

kill 进程号

使用systemctl管理服务进程

# 在/usr/lib/systemd/system目录下 新建nginx.service文件,修改对应的目录
[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target
Wants=network.target
[Service]
PIDFile=/usr/nginx/logs/nginx.pid
WorkingDirectory=/usr/nginx
ExecStart=/usr/nginx/sbin/nginx
Restart=on-abnormal
RestartSec=5s
KillMode=mixed
StandardOutput=null
StandardError=syslog
[Install]
WantedBy=multi-user.target

# 保存文件
# 重启服务
systemctl daemon-reload 
systemctl start nginx # 启动nginx
systemctl stop nginx  # 停止nginx
systemctl enable nginx # 开机启动nginx

测试访问

现在服务启动了,进行访问测试,直接打开浏览器输入服务器地址,发现无法访问。
这时需要把服务器的防火墙放通端口。

# 查看防火墙状态
systemctl status firewalld
# 放通80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 刷新防火墙状态
firewall-cmd --reload

现在可以访问了。

总结:现在只是最简单的安装,把基本功能完成了,后面开始服务器的配置以及一些高级功能。

原文:https://www.cnblogs.com/chuck-study/p/15176885.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐