一、mac自带apache
sudo apachectl start //开启
sudo apachectl stop //关闭
sudo apachectl restart //重启
二、配置文件所在位置
/etc/hosts //系统host文件
/etc/apache2/httpd.conf //apache配置文件
/etc/apache2/extra/httpd-vhosts.conf //配置虚拟主机文件
三、编辑apache配置文件
sudo vim /etc/apache2/httpd.conf
提示输入密码,输入本机密码回车。进入文件并处于查看状态
查找输入
/php5_module
按n进行向下查询,按N则向上查询
#LoadModule php5_module libexec/apache2/libphp5.so
查找到上面这句,按下i进入文件编辑状态。把#去掉
LoadModule php5_module libexec/apache2/libphp5.so
这样就可以引用php顺道开启url重写,许多网站都会用到重写功能
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
网站根目录改成自己指定的目录,配置如下:
DocumentRoot "/Users/johnson/Sites"
<Directory "/Users/johnson/Sites">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options FollowSymLinks Multiviews
MultiviewsMatch Any
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
Include /private/etc/apache2/extra/httpd-vhosts.conf
找到这句,把前面的#去掉
最后按键盘上的esc键退出编辑状态,再按:wq
这样就成功保存配置文件并退出
四、编辑虚拟主机httpd-vhosts.conf文件
sudo /etc/apache2/extra/httpd-vhosts.conf
添加这段
<VirtualHost *:80>
ServerAdmin webmaster@csm.com
DocumentRoot "/Users/johnson/Sites/BlogX/advanced/backend/web"
ServerName blogx.csm.com
#ServerAlias www.csm.com
ErrorLog "/private/var/log/apache2/blogx.csm.com-error_log"
CustomLog "/private/var/log/apache2/blogx.csm.com-access_log" common
</VirtualHost>
保存并退出
五、编辑主机hosts文件
sudo /etc/hosts
添加
127.0.0.1 blogx.csm.com
保存退出
六、重启apache
sudo apachectl restart
可以访问blogx.csm.com了