Nginx教程

Linux 安装 Nginx

本文主要是介绍Linux 安装 Nginx,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Step 1:下载 Nginx

Nginx 的官方网站为: http://nginx.org,打开源码可以看到如下的页面内容

Nginx 的官方下载网站为 http://nginx.org/en/download.html,当然你也可以直接在首页选中右边的 download 进入版本下载网页。在下载页面我们会看到如下内容,点击下载

Step 2:上传至服务器

# 创建文件保存目录
mkdir -p /software/nginx

# 解压缩
tar -xzvf nginx-1.20.2.tar.gz 

Step 3:准备安装相关依赖

# 安装 GCC 编译器
yum install -y gcc
# 查看是否安装成功
gcc --version

# 安装 PCRE
yum install -y pcre pcre-devel
# 查看是否安装成功
rpm -qa pcre pcre-devel


# 安装 zlib
yum install -y zlib zlib-devel
# 查看是否安装成功
rpm -qa zlib zlib-devel

# 安装 OpenSSL
yum install -y openssl openssl-devel
# 查看是否安装成功
rpm -qa openssl openssl-devel

# 一条命令来进行安装
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

Step 4:进入解压目录编译和安装

cd /software/nginx/nginx-1.20.2/

# http_ssl_module 方便配置 ssl  stream 方便配置四层代理
./configure --with-http_ssl_module --with-stream

make && make install

Step 5:进入安装目录,并启动

cd /usr/local/nginx

sbin/nginx

到这一步已经安装成功啦,为了方便以后的操作,配置成系统服务

Step 6:配置系统服务

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.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
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=default.target
chmod 755 /usr/lib/systemd/system/nginx.service

systemctl start nginx

# 停止
systemctl stop nginx

# 重启
systemctl restart nginx

# 重新加载配置文件
systemctl reload nginx

# 查看 nginx 状态
systemctl status nginx

# 开机启动
systemctl enable nginx

Step 7:配置到系统环境

vim /etc/profile

# 在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin

source /etc/profile

nginx -V
这篇关于Linux 安装 Nginx的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!