-
Linux(CentOS7)下Nginx安装
记录一下 Linux(CentOS7) 下 Nginx 安装过程
作者:IT王小二
博客:https://itwxe.com
一、准备工作
版本说明:
- Linux版本:CentOS 7 64位
- Nginx版本:nginx-1.20.0
1. 下载安装文件
采用源码包方式安装,当然使用 yum 方式安装也可以,此处使用源码包安装。
进入目录(个人习惯/usr/local),下载安装文件,如果云服务器下载速度过慢也可本地下载后上传。
cd /usr/local
wget http://nginx.org/download/nginx-1.20.0.tar.gz
2. 安装Nginx所需要的依赖
# 安装gcc、gcc-c++
yum -y install gcc
yum -y install gcc-c++
# 安装pcre 、zilb
yum -y install pcre*
yum -y install zlib*
# 安装openssl(若需要支持 https 协议)
yum -y install openssl
yum -y install openssl-devel
二、安装及配置Nginx
1. 安装Nginx
1、解压安装包。
tar -zxvf nginx-1.20.0.tar.gz
2、为编译安装做准备,进入解压目录。
cd nginx-1.20.0
3、为编译安装做准备。
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre
注:
--prefix:设置安装路径。
--with-http_stub_status_module:支持nginx状态查询。
--with-http_ssl_module:支持https。
--with-pcre:为了支持rewrite重写功能,必须制定pcre。
4、编译安装。
make && make install
5、指定配置文件启动 Nginx。
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
在浏览器访问 ip,看到如下信息则安装成功。
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
2. 一些命令
修改自己的配置之后,验证配置是否正确,重启 Nginx 命令
# 进入目录
cd /usr/local/nginx/sbin
# 验证配置是否正确
./nginx -t
# 如果看到如下内容, 那么配置正确, 可以重启Nginx
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 重启Nginx, 之后就可以通过域名访问了, 哈哈
./nginx -s reload
3. Nginx开机自启
# 新建文件
vim /lib/systemd/system/nginx.service
# 添加内容
[Unit]
Description=nginx.server
After=network.target
[Service]
Type=forking
PIDFILE=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecRepload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
然后使用 systemctl enable nginx.service
开启 nginx 开机自启,重启 CentOS 查看效果。
# 启动nginx服务
systemctl start nginx.service
# 停止nginx服务
systemctl stop nginx.service
# 重启nginx服务
systemctl restart nginx.service
# 查看nginx服务当前状态
systemctl status nginx.service
# 设置nginx服务开机自启动
systemctl enable nginx.service
# 停止nginx服务开机自启动
systemctl disable nginx.service
结语:当然Nginx除了反向代理,还有负载均衡等功能,这个就不在本文章范围内了。
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比