Nginx安装

环境准备

Nginx官网下载地址:http://nginx.org/en/download.html
Nginx(版本nginx-1.18.0.tar.gz)下载完后,通过xshell或者其他软件上传到服务器/opt/下。

安装gcc依赖的目的–nginx基于c语言开发,编译的过程需要用到gcc环境
        yum install gcc‐c++

nginx的http模块使用pcre来解析正则表达式,需要在linux上安装pcre库
        yum ‐y install pcre pcre‐devel

安装zlib库,nginx使用zlib对http包的内容进行gzip
        yum ‐y install zlib zlib‐devel

安装openssl库,让 nginx 支持 https(即在ssl协议上传输http)
        yum ‐y install openssl openssl‐devel

也可一次性安装
        yum ‐y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

安装步骤

a. 解压nginx-1.18.0.tar.gz到/opt根目录下,在/opt目录执行命令如下:
cd /opt/
tar -zvxf nginx-1.18.0.tar.gz
b. 新建nginx-wolverine文件夹
mkdir -p apps/nginx-wolverine/
c. 进入nginx-1.18.0.tar.gz解压的文件夹nginx-1.18.0
cd nginx-1.18.0/
d. 指定安装目录
./configure --prefix='/opt/apps/nginx-wolverine/
make
make install

./configure常见命令
prefix–指定部署根目录,默认根目录为/usr/local/nginx
sbin-path–可执行文件的路径,默认为/sbin/nginx(相对prefix设置的根目录而言)
conf-path–配置文件路径,默认为/conf/nginx.conf(相对prefix设置的根目录而言)
error-log-path–报错日志存储路径,默认为/logs/error.log(相对prefix设置的根目录而言)
http-log-path–请求日志存放路径,默认为/logs/access.log(相对prefix设置的根目录而言)

服务管理

a. 启动服务
/opt/apps/nginx-wolverine/sbin/nginx
b. 更新服务
/opt/apps/nginx-wolverine/sbin/nginx -s reload
c. 停止服务
/opt/apps/nginx-wolverine/sbin/nginx -s stop

服务验证

  • 查看服务进程,列表显示“nginx : master precess”即服务启动成功;
    ps -ef | grep nginx
  • 浏览器输入ip地址,默认端口80,页面显示“Welcome to nginx!”即安装成功。

遇到的问题

  • 主机访问不了虚拟机中的服务
# 开启端口
firewall-cmd --permanent --add-port=80/tcp
# 重启防火墙
firewall-cmd --reload

部署

将打包好的文件放在/opt/apps/nginx-wolverine/html目录下
修改/opt/apps/nginx-wolverine/conf/nginx.conf配置文件

可能遇到的问题

# ./configure: error: the HTTP rewrite module requires the PCRE library.
yum -y install pcre-devel

# ./configure: error: the HTTP gzip module requires the zlib library.
yum -y install zlib-devel

# ./configure: error: C compiler cc is not found
yum -y install gcc gcc-c++

# ./configure: error: SSL modules require the OpenSSL library.
yum -y install openssl openssl-devel

# Error: Nothing to do
yum -y update

systemctl管理nginx

vi /usr/lib/systemd/system/nginx.service

[Unit]
# 描述服务
Description=nginx-The High-performance HTTP Server
# 前置服务
After=network.target remote-fs.target nss-lookup.target

[Service]
# 后台运行的形式
Type=forking
# PID文件的路径
PIDFile=/opt/nginx/logs/nginx.pid
# 启动准备
ExecStartPre=/opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf
# 启动命令
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
# 重启命令
ExecReload=/opt/nginx/sbin/nginx -s reload
# 停止命令
ExecStop=/opt/nginx/sbin/nginx -s stop
# 给服务分配临时空间
PrivateTmp=true

[Install]
# 服务用户的模式
WantedBy=multi-user.target
posted @ 2022-11-14 18:42  苇草剑  阅读(63)  评论(0)    收藏  举报