1 安装环境
yum -y install gcc-c++
yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
2安装
解压:把Nginx压缩包传输到linux服务器上,如:/soft目录下,解压到/usr/local目录。
tar -xvf nginx-1.15.3.tar.gz -C /usr/local
解压后会在/usr/local下出现nginx-1.15.3目录,在nginx-1.15.3目录下,执行configure文件
cd /usr/local/nginx-1.15.3
./configure
在nginx-1.15.3的解压目录下编译与安装nginx。
make PREFIX=/usr/local/nginx install
进入 nginx/sbin目录启动nginx服务器
cd /usr/local/nginx/sbin
./nginx
#开放80端口
firewall-cmd --zone=public --list-ports 查看端口是否设置成功
firewall-cmd --zone=public --add-port=80/tcp --permanent
systemctl restart firewalld
3 启动与关闭的命令说明
在cd /usr/local/nginx/sbin 目录下
./nginx 启动nginx服务器**
./nginx -s reload 重新加载新的配置文件**
./nginx -s stop 关闭服务器
3 开机自启动
1 vi /lib/systemd/system/nginx.service
写入下面代码
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
刷新文件生效,然后设置开机自启。
# 设置开机自启。
systemctl enable nginx.service
4 nginx上部署静态网站
进入cd /usr/local/nginx/conf 配置目录修改 nginx.conf文件,
vi /usr/local/nginx/conf/nginx.conf
35行,每行以分号结束。
nginx.conf 参数名功能
listen 监听的端口号
server_name 服务器的域名或IP地址
root 默认访问根目录
index 首页,如果有多个文件使用空格隔开
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
5反向代理
进入cd /usr/local/nginx/conf 配置目录修改 nginx.conf文件,nginx的配置 nginx.conf。
vi /usr/local/nginx/conf/nginx.conf
//用于配置代理服务器的地址和端口号
upstream 代理名 {
server 服务器地址和端口号
//server localhost:8080;
}
// 注释了root html,不再访问html目录下面的文件,增加proxy_pass定义
server {
listen 80;
server_name localhost;
// 指定访问的根目录是代理服务器
location / {
# root html;
index index.html index.htm;
# 访问tomcat
proxy_pass http://代理名;
// proxy_pass http://itheima;
}
}
重启nginx,
配置文件变动后需要重启才能生效,在cd /usr/local/nginx/sbin 目录下
./nginx -s reload
6 读取多个配置文件
1读取制定目录下的nginx 配置文件,首先在主配置文件中添加配置文件的路径
,如图:
1 vi /usr/local/nginx/conf/nginx.conf
添加如下图 的配置文件的路径
2 配置文件的配置如下
3 然后重启nginx生效
cd /usr/local/nginx/sbin
nginx -s reload