来源 https://gist.github.com/jonathantneal/774e4b0b3d4d739cbc53
强烈建议,在执行<code>.conf</code>相关操作时,请务必先备份相关文件,例:
sudo cp ..some ..some.bak
一 配置个人的Sites文件夹
1 确认<code>apache</code>
打开终端,启动<code>apache</code>
sudo apachectl start
访问http://localhost/ 如果提示<code>It works</code>,表示<code>apachectl</code>已OK。
2 配置<code>apache</code>
小技巧:vi打开后按shift+: 提示后输入
set number
可显示终端行数:
sudo vi /etc/apache2/httpd.conf
将 line 212 替换为可用的域名
ServerName localhost`
下一步, 将line 160 和 line 499 去掉#号来打开<code>Virtual Hosts</code>.
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
Include /private/etc/apache2/extra/httpd-vhosts.conf
另,如果没打开php的话,可以将169前#号去掉打开php
LoadModule php5_module libexec/apache2/libphp5.so
3 配置<code>Virtual Hosts </code>
sudo vi /etc/apache2/extra/httpd-vhosts.conf
将里面所有内容替换成以下内容,其中<code>indieweb</code>改成你的用户名,可在终端<code>whoami</code>查看
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Users/indieweb/Sites/localhost"
<Directory "/Users/indieweb/Sites/localhost">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
4 重启<code>apache</code>
sudo apachectl restart
5 配置储存网页文件夹
mkdir -p ~/Sites/localhost
创建一个html便于展示结果
echo "<h1>localhost works</h1>" > ~/Sites/localhost/index.html
现在打开http://localhost/,看是否显示<code>localhost works</code>,如果不是的话请检查一下配置。
二 配置SSL
1 创建证书文件夹
sudo mkdir /etc/apache2/ssl
2 生成私有key和证书
sudo openssl genrsa -out /etc/apache2/ssl/localhost.key 2048
sudo openssl req -new -x509 -key /etc/apache2/ssl/localhost.key -out /etc/apache2/ssl/localhost.crt -days 3650 -subj /CN=localhost
3 将证书添加到钥匙链中,并且添加始终信任
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain /etc/apache2/ssl/localhost.crt
4 配置<code>Virtual Host</code>
sudo vi /etc/apache2/httpd.conf
5 打开 lines 89 和143 前的#适配HTTPS
LoadModule socache_shmcb_module libexec/apache2/mod_socache_shmcb.so
LoadModule ssl_module libexec/apache2/mod_ssl.so
6 打开line 516前#号
Include /private/etc/apache2/extra/httpd-ssl.conf
7 配置<code>Virtual Host</code>
sudo vi /etc/apache2/extra/httpd-vhosts.conf
8 添加443端口,将<code>indieweb</code>替换成自己的用户名
<VirtualHost *:443>
ServerName localhost
DocumentRoot "/Users/indieweb/Sites/localhost"
SSLEngine on
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile /etc/apache2/ssl/localhost.crt
SSLCertificateKeyFile /etc/apache2/ssl/localhost.key
<Directory "/Users/indieweb/Sites/localhost">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
9 配置ssl文件,此处原文有错误
sudo vi /etc/apache2/extra/httpd-ssl.conf
10 将line 44,45 注释,修改成自己的路径
#SSLCertificateFile "/private/etc/apache2/server.crt"
#SSLCertificateKeyFile "/private/etc/apache2/server.key"
SSLCertificateFile "/etc/apache2/ssl/localhost.crt"
SSLCertificateKeyFile "/etc/apache2/ssl/localhost.key"
11 检查是否正确
sudo apachectl configtest
12 重启apache
sudo apachectl restart
网友评论