① 检查是否安装www服务器对应的rpm软件 rpm -qa | grep httpd
② 如果没有安装软件,那么就要重新安装,rpm软件一般在第一或者第二张系统盘中 rpm -ivh 软件名
③ 如果安装成功,那么就对主配置文件httpd.conf进行配置,主配置文件默认在/etc/httpd/conf文件夹下
④ 打开主配置文件,该文件中的大部分的信息都已经被注释,如果想创建一个简单的www服务器,那么只需要开启www服务器,不需要对主配置文件做任何修改
⑤ 测试 将linux系统和客户端系统的网卡都设置成桥接方式连接,给linux系统和客户端系统创建静态ip地址,并且ip都在和宿主机相同的网段
⑥ 开启linux系统中的www服务器 servcie httpd start
⑦ 在客户端的浏览器中输入www服务器的ip地址,显示如下:
⑧ 创建个人用户主页
打开主配置文件,查找<IfModule mod_userdir.c>容器字段,里面有两个参数,默认情况下第一个UserDir参数值为disable,第二个UserDir值是注释的,修改为将第一个参数注释,第二个参数前面的#号去掉。申请linux用户,一般申请后在home文件夹下有主目录文件夹,例如申请用户jsj,会出现/home/jsj文件夹,然后在jsj文件夹下创建public_html文件夹,命令是mkdir /home/jsj/public_html,在public_html下面创建index.html首页,之后输入要测试时显示的内容,命令是echo "this is jsj`s html" >> /home/jsj/public_html/index.html,设置个人主页文件夹的访问权限,chmod 705 /home/jsj,最后将个人主页的访问权限开启,即将主配置文件的<Directory /home/*/public_html>容器的注释去掉,保存主配置文件之后重启www服务器,在浏览器中输入ip地址/~jsj(波浪线后面的这个是用户名)/,出现内容是this is jsj`s html的网页,测试成功。
⑨ 创建虚拟目录
创建设置的虚拟目录站点 mkdir /var/www/html,设置虚拟目录的主页echo "this is virtual Directory sample" >> /var/www/html/index.html,设置默认首页的访问权限,如果设定为可读权限,那就是chmod 705 /va/www/html/index.html,接着设置主配置文件,在主配置文件中添加一条记录,Alias /test "/var/www/html",这句话的意思是将/var/www/html虚拟站点设置别名为test。之后在测试机上访问test就是访问虚拟目录,别名可设置成别的名字,客户端浏览器中访问的是ip地址/虚拟目录别名/
⑩ 目录设置
打开主配置文件,查找<Directory “”>容器字段,里面有参数设置,规定了访问者的要求,在容器字段的双引号中添加需要设定的文件或者文件夹的地址和名字,如/var/www/html这个文件夹允许192.168.0.0/24网段的客户端访问,但是不允许192.168.0.100访问,设置为<Directory "var/www.html"> Order allow,deny Allow from 192.168.0.0/24 Deny from 192.168.0.100 </Directory> ,设置完主配置文件之后保存关闭主配置文件,如果设置的是文件,那么改的时候将Directory改为Files
II 设置虚拟主机
⑴ 基于ip地址的虚拟主机的配置
.⒈ 以/var/www/ip1和/vr/www/ip2两个站点举例,分别创建/var/www/ip1和/var/www/ip2两个主目录和默认文件
mkdir /var/www/ip1 /var/www/ip2
echo "this is 192.168.0.2`s web." >> /var/www/ip1/index.html
echo "this is 192.168.0.3`s web." >> /var/www/ip2/index.html
⒉ 修改httpd.conf文件,修改内容如下:
//设置基于ip地址为192.168.0.2的虚拟主机
<Virtualhost 192.168.0.2>
DocumentRoot /var/www/ip1 //设置该虚拟主机的主目录
DirectoryIndex index.html //设置默认文件的文件名
ServerAdmin root@sales.com //设置管理员的邮件地址
ErrorLog logs/ip1-eeor_log //设置错误日志的存放位置
CustomLog logs/ip1-access_log common //设置访问日志的存放位置
</Virtualhost>
<Virtualhost 192.168.0.3>
DocumentRoot /var/www/ip2 //设置该虚拟主机的主目录
DirectoryIndex index.html //设置默认文件的文件名
ServerAdmin root@sales.com //设置管理员的邮件地址
ErrorLog logs/ip2-eeor_log //设置错误日志的存放位置
CustomLog logs/ip2-access_log common //设置访问日志的存放位置
</Virtualhost>
⒊ 重新启动httpd服务
⒋ 在客户端浏览器中可以看到http://192.168.0.2和http://192.168.0.3两个网站的浏览效果。
⑵ 基于域名的虚拟主机的配置
⒈ 假设在linux系统的dns中设置两个站点www.smile.com.和www.long.com.对应的ip地址都是本机的ip地址,本机的ip地址设置成192.168.0.3,分别创建/var/www/smile和/var/www/long两个主目录和默认文件
mkdir /var/www/smile /var/www/long
echo "this.is www.smile.com`s web" >> /var/www/smile/index.html
echo "this is www.long.com`s web" >> /var/www/long/index.hml
⒉ 修改httpd.conf文件,修改内容如下:
NameVirtualhost 192.168.0.3 //指定虚拟主机所使用的ip地址,该ip地址将对应多个域名
<Virtualhost 192.168.0.3> //Virtualhost后面可以跟ip地址或者域名
DocumnetRoot /var/www/smile
DirectoryIndex index.html
ServerName www.smile.com
ServerAdmin root@smile.com
ErrorLog logs/www.smile.com-error_log
CustomLog logs/www.smile.com-access_log common
</Virtualhost>
<Virtualhost 192.168.0.3>
DocumnetRoot /var/www/long
DirectoryIndex index.html
ServerName www.long.com //指定该虚拟主机的FQDN
ServerAdmin root@long.com
ErrorLog logs/www.long.com-error_log
CustomLog logs/www.long.com-access_log common
</Virtualhost>
⒊ 重新启动httpd服务
⑶ 基于端口号的虚拟主机的配置
⒈ 分别创建/var/www/port8080和/var/www/port8090两个主目录和默认文件
mkdir /var/www/port8080 /vr/www/port8090
echo "this is 8000 ports web." >> /var/www/port8080/index.html
echo "this is 8800 ports web." >> /var/www/port8090/index.html
⒉ 修改httpd.conf文件,修改文件如下:
Listen 8080 //设置监听端口
Listten 8090
<VirtualHost 192.1638.0.3:8080> //VirtualHost后面跟着ip地址和端口号,二者之间用冒号分隔
DocumentRoot /var/www/port8080
DirectoryIndex index.html
EoorLog logs/port8080-error_log
CustomLog logs/port8080-access_log common
</VirtualHost>
<VirtualHost 192.1638.0.3:8090>
DocumentRoot /var/www/port8090
DirectoryIndex index.html
EoorLog logs/port8090-error_log
CustomLog logs/port8090-access_log common
</VirtualHost>
⒊ 重新启动httpd服务。