-----Nginx服务器的详细安装步骤
【Nginx 介绍】
- Nginx 是一款 高性能、轻量型的 web 服务软件 ,也是 HTTP 和 反向代理服务器。
- Nginx 特点:稳定性高、系统资源消耗低、对 http 并发连接的处理能力高,单台物理服务器可支持 30000-50000个并发请求。
- 做为 web 服务器:Nginx 相比于 Apache 服务器,它使用更少的资源,支持更多的并发连接,体现更高的效率 ,这点使 Nginx 更受欢迎。
- 支持 rewrite 重写规则:能够根据域名、URL的不同,将 http 请求分发到不同的后端服务器群组。
- 内置的健康检查功能:如果 Nginx Proxy后端的后台 web 服务器宕机了,不会影响前端访问。
- 节省宽带:支持 gzip 压缩,可以添加浏览器本地缓存的 Header 头。
- 稳定性高:用于反向代理,宕机的概率很低。
【安装 Nginx 服务器】
一、搭建环境
Linux 操作系统 如果安装有 httpd 服务,请先关闭,否则在安装的过程中将会遇到 80端口 被占用的报错
1、Nginx 的配置及运行需要 pcre (提供库文件)、zlib(提供头文件) 软件包的支持,(devel:开发包),先行安装。
[root@localhost~]# yum -y install pcre-devel zlib-devel
2、创建专门的用户账号,以便更准确的控制其访问权限,增加灵活性、降低安全风险。
[root@localhost~]# useradd -M -s /sbin/nologin nginx
3、编译安装 Nginx
这里需要一个 Nginx 压缩包,你可以去网上下载一个 将其上传至 /opt 目录下。 ( 我的是 nginx-1.15.9.tar.gz 版本的)
[root@localhost opt]# tar zxvf nginx-1.15.9.tar.gz
[root@localhost opt]#cd nginx-1.15.9/
[root@localhost nginx-1.15.9]#
./configure \
--prefix=/usr/local/nginx \ #######安装路径/usr/local/nginx
--user=nginx \ #######运行用户为nginx
--group=nginx \ #######运行用户组为nginx
--with-http_stub_status_module \ ########启用这个模块以支持状态统计,便于查看服务器的连接信息
(这边建议 先将 gcc gcc-c++ make 编译语言一起安装下,因为我的是装过的了,怕你们会报错)
[root@localhost nginx-1.15.9]# make -j3 ######make编译,核心数为3(加载速度快些)
[root@localhost nginx-1.15.9]# make install ######make 安装
4、为主程序 Nginx 创建软连接文件,方便管理员直接执行 “nginx”命令就可以调用 Nginx 的主程序。
[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.15.9]# ls -l /usr/local/sbin/nginx
(####问题报错:可能用户账号没创建 或者 make没编译好,需要重新make编译)
二、Nginx 运行与控制
5、Nginx 主程序也提供了“-t”选项来对配置文件进行检查,以便找出不当或错误的配置,(/usr/local/nginx/conf/nginx.conf)
若要检查位于其他位置的配置文件,可以使用“-c”选项来指定路径。
[root@localhost nginx-1.15.9]# nginx -t
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
[root@localhost nginx-1.15.9]# nginx ######直接运行 nginx 及可启动 nginx 服务器
(注意这里:若服务器里有 httpd 等其他 web 服务软件,可以修改端口、停用或卸载服务,要不然会冲突。)
6、通过检查 Nginx 程序的监听状态,或者在浏览器中访问此 web 服务(默认页面将显示“Welcone to nginx!”),可以确认 Nginx 服务 是否在正常运行。
[root@localhost nginx-1.15.9]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 76079/nginx: master
[root@localhost nginx-1.15.9]# yum -y install elinks ######安装elinks浏览器
[root@localhost nginx-1.15.9]#elinks http://localhost ######使用elinks浏览器
1)://会显示: “Welcone to nginx!” 页面,表明 nginx 服务在正常运行
2):// 在你可以在真机上访问域名时,再在 linux 操作系统上输入 “elinks http://localhost ”时,将会显示你的域名
7、使用killall 命令,重载配置、停止服务的操作(通过“-s”选项指定信号种类),信号(HUP 重载配置 ;QUIT 退出进程 ;KILL 杀死进程)
[root@localhost nginx-1.15.9]# killall -s HUP nginx #####重载配置
[root@localhost nginx-1.15.9]# killall -s QUIT nginx #####退出进程
8、基于 Nginx 服务脚本使用 systemctl 工具来进行管理
[root@localhost nginx-1.15.9]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.15.9]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost nginx-1.15.9]# systemctl enable nginx.service
安装完毕
【配置基于域名的虚拟主机】
1、修改windos客户机的C:\Windows\System32\drivers\etc\hosts文件,加入 www.51xit.top和www.52xit.top这两个域名,它们都指向同一个服务器IР地址,用于实现不同的域名访问不同的虚拟主机。
列如:
20.0.0.33 www.53com
20.0.0.33 www.553xit.com
2、准备各个网站的目录和测试首页
[root@localhost~]# mkdir -p /var/www/html/53xit/ ####创建www.53xit.com的根目录
[root@localhost ~]# mkdir -p /var/www/html/553xit/ ####创建www.553xit.com的根目录
[root@localhost~]# echo "www.53xit.top" >> /var/www/html/53xit/index.html
[root@localhost~]#echo "www.553xit.top" >> /var/www/html/553xit/index.html
3、编辑配置文件 nginx.conf
[root@localhost nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf
#user nobody; #####运行用户
worker_processes 1; #####工作进程数量
#error_log logs/error.log; #####错误日志文件的位置
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #####PID文件的位置
events {
use epoll; #####使用 epoll 模型
worker_connections 1024; #####每个进程处理1024个连接
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main; #####访问日志位置
sendfile on; #####支持文件发送(下载)
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #####连接保持超时
#gzip on;
# server { #####web 服务的监听配置
# listen 80; #####监听端口及地址
# server_name localhost; #####网站名称(FQDN)
# charset utf-8; #####网页的默认字符集
# #access_log logs/host.access.log main;
# location / { #####根目录配置
# root html; #####网站根目录的位置
# index index.html index.htm; #####默认首页(索引页)
# }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html; #####内部错误的反馈界面
# location = /50x.html { #####错误页面配置
# root html;
# }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
。。。。。。。 #####省略内容,以下全 # 号注释
。。。。
#location ~ /\.ht {
# deny all;
#}
# }
server { #####直接插入添加(第一个域名)
listen 80;
server_name www.53xit.com;
charset utf-8;
access_log logs/www.53xit.com.access.log;
location /{
root /var/www/html/53xit;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server { #####直接插入添加(第二个域名)
listen 80;
server_name www.553xit.com;
charset utf-8;
access_log logs/www.553xit.com.access.log;
location /{
root /var/www/html/553xit;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
........(省略内容)
4、启动Nginx
[root@localhost nginx-1.15.9]# systemctl restart nginx
5、在真机浏览器中输入域名 进行测试
【配置基于 ip地址 的虚拟主机】
1、在虚拟机上添加一块网卡,设置ip,测试ping通
添加 自定义:VM1 网卡;
查看 PPID号:nmcli connection
复制 ens36的 PPID号
cd /etc/sysconf/network-scripts/
cp ifcfg-ens33 ifcfg-ens36
vi ifcfg-ens36
… (更改里面的内容 ,注意改 PPID 号)
重启网卡:systemctl restart network
2、将 nginx.conf 配置文件中的 子界定标记里的配置 修改一下
vi /usr/local/nginx/conf/nginx.conf
。。。。。。(省略内容,只需要改下面几个参数)
erver {
listen 192.168.100.33:80; #####修改成ip
server_name 192.168.100.33:80; #####修改成ip
charset utf-8;
access_log logs/www.53xit.com.access.log;
location / {
root /var/www/html/53xit;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 20.0.0.33:80; #####修改成ip
server_name 20.0.0.33:80; #####修改成ip
charset utf-8;
access_log logs/www.553xit.com.access.log;
location /{
root /var/www/html/553xit;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
重启 nginx 服务 :systemctl restart nginx
3、在真机浏览器中输入 IP地址 进行测试
【配置基于 端口号 的虚拟主机】
1、相同的 ip地址,不同的端口号
vi /usr/local/nginx/conf/nginx.conf
。。。。。(省略以下内容,更改如下几个参数)
server {
listen 20.0.0.33:6666;
server_name 20.0.0.33:6666;
charset utf-8;
access_log logs/www.53xit.com.access.log;
location / {
root /var/www/html/53xit;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 20.0.0.33:8888;
server_name 20.0.0.33:8888;
charset utf-8;
access_log logs/www.553xit.com.access.log;
location /{
root /var/www/html/553xit;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
重启 nginx 服务 :systemctl restart nginx
2、在真机浏览器中输入 IP地址+端口号