Apache虚拟主机&伪静态配置

Apache基本操作

  • 安装:yum install httpd
  • 启动:systemctl start httpd
  • 查看进程:ps -ef | grep httpd
  • 查看端口:sudo netstat -anpl | grep 'httpd'
  • 停止:systemctl stop httpd

虚拟主机配置

  • 进入进入配置文件http.confsudo
vim /etc/httpd/conf/httpd.conf
  • 在配置文件#Virtual处添加虚拟主机
<VirtualHost *:80>
        ServerName www.llinux.test
        DocumentRoot /data/www
        <Directory "/data/www">
                Options Indexes FollowSymLinks
                AllowOverride none
                Require all granted
        </Directory>
</VirtualHost>

  • 新建/data/www目录,并添加index.html文件
sudo mkdir -p /data/www
sudo vim index.html
#内容可输入'Hello World',保存退出
  • 更改客户端hosts文件,将www.llinux.test定向到指定IP
例如win10(在cmder中操作)
cd \Windows\System32\drivers\etc
vim hosts
#添加一条域名—IP地址

  • 回到服务器,重启apache
sudo systemctl restart httpd
  • 客户端浏览器访问www.llinux.test

虚拟主机配置

  • 进入配置文件http.conf
sudo vim /etc/httpd/conf/httpd.conf
  • 加载rewrite模块
#找到LoadModule并添加rewrite_module
LoadModule rewrite_module modules/mod_rewrite.so

  • 添加规则,所有以htmp结尾的网址都会跳转到index.html页面
#找到刚才添加的虚拟主机处,添加规则
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*).htmp$ index.html
</IfModule>

  • 回到服务器,重启apache
sudo systemctl restart httpd
  • 客户端浏览器,打开www.llinux.test/abc.htmp

猜你喜欢

转载自www.cnblogs.com/felixqiang/p/11362176.html