Apache测试准备

1.关闭防火墙与selinux

systemctl stop firewalld
setenforce 0

2.准备测试软件包httpd

yum -y install httpd
systemctl start httpd
netstat -nltp | grep httpd (默认端口为80端口,端口可修改)

3.

认识主配置文件:
# vim /etc/httpd/conf/httpd.conf
ServerRoot “/etc/httpd” #工作目录
Listen 80 #监听端口
Listen 192.168.2.8:80 #指定监听的本地网卡 可以修改
User apache # 子进程的用户,有可能被人改称www账户
Group apache # 子进程的组
ServerAdmin root@localhost # 设置管理员邮件地址
DocumentRoot “/var/www/html” # 发布网站的默认目录,想改改这里。
IncludeOptional conf.d/.conf # 包含conf.d目录下的.conf文件

设置DocumentRoot指定目录的属性
# 网站容器开始标识
Options Indexes FollowSymLinks # 找不到主页时,以目录的方式呈现,并允许链接到网站根目录以外
AllowOverride None # 对目录设置特殊属性:none不使用.htaccess控制,all允许
Require all granted # granted表示运行所有访问,denied表示拒绝所有访问
# 容器结束
DirectoryIndex index.html # 定义主页文件,当访问到网站目录时如果有定义的主页文件,网站会自动访问

虚拟主机的搭建与配置

测试前准备

Nginx的搭配与创建

Nginx测试准备

nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。
1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)
2、基于ip的虚拟主机, (一块主机绑定多个ip地址)
3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)
1.安装Nginx
yum -y install nginx
2.关闭selinux 与firewalld
systemctl stopfirewalld
setenforce 0
3.网页输入ip
一、基于端口
1、创建/port /port1 并在其目录下创建index.html文件
mkdir /port /port1
echo “this is port 81” >/port/index.html
echo “this is port 82” >/port1/index.html
2、编写子配置文件
server{
listen 81;
server_name localhost;
location / {
root /port;
index index.html;
}
}
server {
listen 82;
server_name localhost;
location / {
root /port1;
index index.html;
}
}
2、重启nginx,并用客户端浏览器分别访问192.168.159.161:81/192.168.159.161:82观察其结果
nginx -t //检测配置文件语法错误
systemctl restart nginx
二、基于ip
1、创建/ip/ip1 并在其目录下创建index.html文件
mkdir /ip /ip1
echo “192.168.159.161” >/ip/index.html
echo “192.168.159.168” >/ip1/index.html
2、编写子配置文件
server{
listen 81;
server_name 192.168.159.161;
location / {
root /ip;
index index.html;
}
}
server {
listen 81;
server_name 192.168.159.168;
location / {
root /ip1;
index index.html;
}
}
或者
server{
listen 192.168.159.161:81;
location / {
root /ip;
index index.html;
}
}
server {
listen 192.168.159.168:81;
location / {
root /ip1;
index index.html;
}
}
2、重启nginx,并用客户端浏览器分别访问192.168.159.161:81/192.168.159.168:81观察其结果
nginx -t //检测配置文件语法错误
systemctl restart nginx
三、基于域名
1、创建/url /url1 并在其目录下创建index.html文件
mkdir /url /url1
echo “www.url.com” >/port/index.html
echo “www.url1.com” >/port1/index.html
2、编写子配置文件
首先在添加本地DNS解析
server{
listen 81;
server_name www.url.com;
location / {
root /url;
index index.html;
}
}
server {
listen 81;
server_name www.url1.com;
location / {
root /port1;
index index.html;
}
}
2、重启nginx,并用客户端浏览器分别访问www.url.com:81/www.url1.com:81观察其结果
nginx -t //检测配置文件语法错误
systemctl restart nginx