Nginx系列问题之Centos7 安装配置,
Nginx系列问题之Centos7 安装配置,
一、下载安装Nginx
1. 创建文件夹
cd /root/
mkdir downloads #在root目录下创建downloads 文件夹
2.打开官网
浏览器打开:https://nginx.org/en/download.html下载最新的稳定版本.
选中红框单击右键,选择复制链接地址。
3.下载编译安装Nginx
下载&解压-在终端输入:
wget https://nginx.org/download/nginx-1.17.1.tar.gz #下载文件
tar -zxvf nginx-1.17.1.tar.gz #解压文件
ls #得到nginx-1.17.1文件夹
编译安装
cd nginx-1.17.1
./configure --prefix=/usr/local/nginx #安装到/usr/local/nginx目录
# 报错
./configure: error: the HTTP rewrite module requires the PCRE library
# 解决-安装pcre-devel
yum install pcre-devel -y
# 报错
./configure: error: the HTTP gzip module requires the zlib library
# 解决-安装zlib-devel
yum install zlib-devel -y
# 重新执行
./configure --prefix=/usr/local/nginx
make && make install # 编译安装完成
启动Nginx
cd /usr/local/nginx/sbin/
./ nginx # 启动Nginx
在浏览器输入http://你的-ip
。 若出现welcome to Nginx
字样。说明安装成功。
二、常见配置
一、无法访问
- 如果是在和通数据库,京东云等国内云主机购买的机器,请放行相应的安全组端口。
- 国内云主机如果没有备案,则无法访问80端口。需要修改
Nginx
的配置文件。修改端口之后,需要在安全组放行。
vim /usr/local/nginx/conf/nginx.conf # Nginx配置文件
server {
listen xxxxx; # 修改端口号
server_name localhost;
cd /usr/local/nginx/sbin/
./ nginx -s reload # 重载使生效
- 修改防火墙的设置
firewall-cmd --zone=public --add-port=xxxxx/tcp --permanent
# --permanent永久生效,没有此参数重启后失效
firewall-cmd --reload # 重载配置
firewall-cmd --list-ports # 查看放行端口
二、使用systemctl 管理Nginx
- 终端输入:
vim /usr/lib/systemd/system/nginx.service
# 编辑内容
[Unit]
Description=nginx
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.targeta
按ESC
--:
--wq
保存退出。
- systemctl 常用命令
systemctl start nginx.service # 启动一个服务
systemctl stop nginx.service # 关闭一个服务
systemctl restart nginx.service # 重启一个服务
systemctl status nginx.service # 显示服务状态
systemctl enable nginx.service # 开机自启用
systemctl disable nginx.service # 禁用开机启动
systemctl is-enabled nginx.service # 查看服务是否开机启动
三、创建软链接
- 终端输入:
ln -s /usr/local/nginx/sbin/nginx /usr/bin/python
# 在 /usr/bin/目录下创建 nginx软链接(快捷方式)
# ln -s /这里写命令实际所在目录 /要在哪个目录创建软链接(快捷方式)
- Nginx常用命令
nginx -t # 验证nginx配置文件
nginx -s reload # 使配置生效
nginx -v # 版本信息
nginx -V # 详细版本信息,包括编译参数
nginx -c filename # 指定配置文件
四、域名重定向
我想将域名www.aaa.com
跳转到www.bbb.com
终端输入
cd /usr/local/nginx/conf/
vim nginx.conf # Nginx默认配置文件
server {
listen xxxxx; # 填端口号
server_name www.aaa.com;
rewrite ^/(.*)$ http://www.bbb.com:xxx/$1 permanent;
# rewrite 用来域名跳转,后面规则是正则表达式。端口号跟在域名后面
#charset koi8-r;
#access_log logs/host.access.log main;
# 保存退出。
nginx -t # 检测配置文件是否正确
nginx -s reload # 重载配置使生效
需要将www.aaa.com这个域名解析到nginx服务器所在ip。在域名服务商里面操作。
五、Nginx配置一个ip绑定多个域名
我想让这台Nginx服务器绑定两个域名,用端口号来区分。
终端输入
cd /usr/local/nginx/conf/
vim nginx.conf
server {
listen 10001;
server_name www.aaa.com;
location / {
root html;
index index.html index.htm;
}
}
# 配置第二个域名
server {
listen 10001;
server_name www.bbb.com;
location / {
index index.htm index.html index.php; #默认文件
root /var/www/; #网站根目录
}
#include location.conf; #调用其他规则,也可去除
}
# 注意大括号的完整性
# 保存退出
nginx -t # 检测配置文件是否正确
nginx -s reload # 重载配置使生效
将www.bbb.com
解析到Nginx服务器的ip。
在浏览器输入www.aaa.com:10000
和www.bbb.com:10001
进行测试。
有错误的地方请帮忙指出,菜鸟一枚努力学习中
评论暂时关闭