MAc配置虚拟主机
apache
# 查看apache版本
sudo apachectl -v
# 开启
sudo apachectl start
apache的根目录:/Library/WebServer/Documents/
Apache的安装目录:/etc/apache2/ (etc默认是隐藏的 可以在terminal 输入 "open /etc"打开)
在浏览器地址栏中输入 localhost 显示it works! 代表apache正在运行
Apache开启了虚拟主机配置功能
1.输入密码后切换到root权限
sudo su
2.编辑httpd.conf文件,输入命令:
vim /etc/apache2/httpd.conf
3.将#Include /private/etc/apache2/extra/httpd-vhosts.conf这行前的注释符号#去掉。
(点击i进入编辑模式,修改后按esc退出编辑模式,在输入ZZ 或者 :wq保存文件退出 :!q不保存强制退出)
4.运行、重启Apache(两种方式) 重启。
#1.平滑重启
apachectl -k graceful
#2.重启
apachectl restart
配置虚拟主机
1.编辑httpd-vhosts.conf文件,输入命令:
sodu vim /etc/apache2/extra/httpd-vhosts.conf
2.在httpd-vhosts.conf 中添加以下内容:
该文件默认开启了两个作为例子的虚拟主机:
完整的配置如下httpd.conf:
# 端口号配置建议知名端口号范围:0-1023 排除这个范围可随意配置端口号上限05535,有关端口号知识扩展,请自行百科
<VirtualHost 127.0.0.1:8080> # 8080为端口号
ServerName www.localhost.com # 配置的域名
DocumentRoot /library/WebServer/Documents/www_frontend_com #文件路径
<Directory "/library/WebServer/Documents/www_frontend_com">
Options Indexes FollowSymLinks
AllowOverride All # 不许可别人修改我们的页面
Require all granted # 设置访问权限
</Directory>
ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log" # 错误日志路径
CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common # 错误日志路径
</VirtualHost>
由于是在本地测试开发网站,还需要修改hosts文件,来将你的域名test. com对应本地的IP,输入命令:
vim /etc/hosts
并在文件后添加serverName
127.0.0.1 www.test.com
PHP配置文件修改
#1.找到“/private/etc/apache2/httpd.conf ”文件,打开文件找到
#2. LoadModule php5_module libexec/httpd/libphp5.so” 去掉前面注释
#3.运行“sudo apachectl restart”,重启Apache,这样PHP就可以用了。
#4.设置 php.ini 的位置或者使用默认的位置。 Mac OS X 上通常默认的位置是 /usr/local/php/php.ini ,调用 <?php phpinfo(); ?>也可以得到此信息。
此时就可以在浏览器中输入serverName显示It works!代表访问成功。