我們經(jīng)常性的需要使用局域網(wǎng)搭建 Web 服務(wù)器測試環(huán)境冬骚,如部署局域網(wǎng)無線安裝企業(yè)應(yīng)用等劈猿,Mac OS X 自帶了 Apache 和 PHP 環(huán)境沮榜,我們只需要簡單的啟動它就行了尚胞。
啟動 Apache
查看 Apache 版本
打開終端,輸入httpd -v可以查看 Apache 版本信息掏呼。
$ httpd -v
Server version: Apache/2.4.16 (Unix)
Server built:? Aug 22 2015 16:51:57
$
啟動 Apache
在終端輸入sudo apachectl start即可啟動 Apache坏快。
啟動后,在瀏覽器中輸入http://127.0.0.1或http://localhost如果看到It Works!頁面:
img_01.png
那么 Apache 就啟動成功了憎夷,站點的根目錄為系統(tǒng)級根目錄/Library/WebServer/Documents假消。
啟動后,你可以通過編輯/etc/apache2/httpd.conf文件來修改 Apache 配置岭接。
停止 Apache:sudo apachectl stop
重啟 Apache:sudo apachectl restart
創(chuàng)建用戶級根目錄
我們也可以創(chuàng)建用戶級根目錄富拗,更方便管理和操作。
在用戶目錄下創(chuàng)建Sites目錄鸣戴,cd; mkdir Sites; touch Sites/.localized啃沪,舊的 Mac 系統(tǒng)中如果該目錄已存在,則略過窄锅。
cd /etc/apache2/users檢查目錄下是否存在username.conf文件创千,username為當前用戶名,如果沒有則創(chuàng)建一個sudo touch username.conf入偷,并修改文件權(quán)限sudo chmod 644 username.conf追驴。
創(chuàng)建之后,打開username.conf文件疏之,sudo vi? username.conf將下面的配置信息寫入文件殿雪,username依然為當前用戶名:
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
編輯/etc/apache2/httpd.conf文件,找到下列代碼锋爪,并將前面的注釋符號#刪除:
Include /private/etc/apache2/extra/httpd-userdir.conf
LoadModule userdir_module libexec/apache2/mod_userdir.so
編輯/etc/apache2/extra/httpd-userdir.conf文件丙曙,找到下列代碼,并將前面的注釋符號#刪除:
Include /private/etc/apache2/users/*.conf
重啟 Apache:sudo apachectl restart
在瀏覽器中輸入http://127.0.0.1/~username或http://localhost/~username其骄,即可測試用戶目錄是否工作亏镰。
啟動 PHP
Mac OS X 也默認集成了 PHP 環(huán)境,如果測試需要用到 PHP 環(huán)境拯爽,可以通過配置手動開啟索抓。
編輯/etc/apache2/httpd.conf文件,找到LoadModule php5_module libexec/apache2/libphp5.so并刪除行前的注釋符號#毯炮。
重啟 Apache:sudo apachectl restart逼肯。
現(xiàn)在 PHP 應(yīng)該已經(jīng)可以工作了,在頁面中嵌入可以查看 PHP 信息否副。
命在終端輸入php --ini可查看 PHP 配置文件汉矿,我們可以將/private/etc/php.ini.default復(fù)制一份命名為/private/etc/php.ini并修改配置文件,如备禀,設(shè)置display_errors = On打開PHP錯誤顯示洲拇。
安裝 MySQL
Mac OS X 沒有集成 MySQL奈揍,需要自己安裝,這個后續(xù)補充赋续。
開啟 HTTPS
如果測試需要 HTTPS 環(huán)境男翰,如, iOS 7.1 以上的設(shè)備部署無線安裝環(huán)境就必須使用 HTTPS纽乱,我們可以配置 Apache 開啟 HTTPS 服務(wù)蛾绎。
創(chuàng)建自簽名證書
首先創(chuàng)建一個 ssl 目錄用來存放證書
$ cd /etc/apache2/
$ sudo mkdir ssl
$ cd ssl
創(chuàng)建主機密鑰
$ sudo ssh-keygen -f local.server.com.key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in local.server.com.key.
Your public key has been saved in local.server.com.key.pub.
The key fingerprint is:
SHA256:bNX90ww2g2GCh38Q/h68JnazkZYtnbkMEb1G5E51QWw root@XuCreamandeiMac.local
The key's randomart image is:
+---[RSA 2048]----+
|? ? ? ? oo.o +o+|
|? ? ? ? o.o+ B E.|
|? ? ? ? oo.+ %? |
|? ? ? . ..o.* B.|
|? ? ? ? S? .= +.+|
|? ? ? .? . X o.|
|? ? ? ? ? o & =? |
|? ? ? ? . = B . |
|? ? ? ? ? ? . o? |
+----[SHA256]-----+
$
這里會被要求提供一個密碼用于主機密鑰,可以選擇任何的密碼或直接留空鸦列。
也可以使用下面的命令創(chuàng)建密鑰:
$ sudo openssl genrsa -out local.server.com.key 2048
Generating RSA private key, 2048 bit long modulus
....+++
....+++
e is 65537 (0x10001)
$
創(chuàng)建簽署申請
$ sudo openssl req -new -key local.server.com.key -out local.server.com.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:local.server.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
$
系統(tǒng)會提示輸入各項信息租冠,由于這是自簽名的證書,除了Common Name (e.g. server FQDN or YOUR name) []:FQDN( fully qualified domain name)必須是服務(wù)器域名或 IP 外薯嗤,其他都不重要顽爹,可以隨意填寫或一路回車,這里作為測試使用local.server.com骆姐。
創(chuàng)建SSL證書
在生產(chǎn)環(huán)境中镜粤,我們需要提交證書申請(CSR)文件給證書頒發(fā)機構(gòu),由證書頒發(fā)機構(gòu)提供SSL證書玻褪。
$ sudo openssl x509 -req -days 365 -in local.server.com.csr -signkey local.server.com.key -out local.server.com.crt
Signature ok
subject=/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd/CN=local.server.com
Getting Private key
$
我們也可以直接通過以下的命令創(chuàng)建證書:
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout local.server.com.key -out local.server.com.crt
創(chuàng)建NOPASS密鑰
為了配置 Apache肉渴,我們需要創(chuàng)建一個 NOPASS 密鑰。
$ sudo openssl rsa -in local.server.com.key -out local.server.com.nopass.key
OK带射,我們看下SSL目錄下面的文件同规,這些文件將在后面被用到:
$ ls -l
-rw-r--r--? 1 root? wheel? 1180 10 22 13:08 local.server.com.crt
-rw-r--r--? 1 root? wheel? 993 10 22 11:58 local.server.com.csr
-rw-------? 1 root? wheel? 1679 10 22 11:44 local.server.com.key
-rw-r--r--? 1 root? wheel? 408 10 22 11:44 local.server.com.key.pub
-rw-r--r--? 1 root? wheel? 1679 10 22 13:19 local.server.com.nopass.key
配置 SSL
加載mod_ssl.so,編輯/etc/apache2/httpd.conf文件庸诱,刪除下列代碼前的注釋符號#:
LoadModule ssl_module libexec/apache2/mod_ssl.so
包含httpd-ssl.conf文件捻浦,編輯/etc/apache2/httpd.conf文件,刪除下列代碼前的注釋符號#:
Include /private/etc/apache2/extra/httpd-ssl.conf
添加到httpd-ssl.conf桥爽,編輯/etc/apache2/extra/httpd-ssl.conf文件:
httpd-ssl.conf中已經(jīng)有一條記錄,我們將其注釋掉昧识,新建一條:
#General setup for the virtual host
DocumentRoot "/Library/WebServer/Documents"
ServerName local.server.com
#SSL Engine Switch:
SSLEngine on
#Server Certificate:
SSLCertificateFile "/etc/apache2/ssl/local.server.com.crt"
#Server Private Key:
SSLCertificateKeyFile "/etc/apache2/ssl/local.server.com.key"
#SSL Engine Options:
SSLOptions +StdEnvVars
SSLOptions +StdEnvVars
為了能夠使用 URL 訪問服務(wù)器钠四,我們需要配置HOST,sudo vi /etc/hosts跪楞,添加127.0.0.1? ? ? local.server.com
檢查配置文件并重啟 Apache
命令行輸入$ sudo apachectl -t缀去,提示:
AH00526: Syntax error on line 92 of /private/etc/apache2/extra/httpd-ssl.conf:
SSLSessionCache: 'shmcb' session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?).
根據(jù)提示,編輯/etc/apache2/httpd.conf文件甸祭,刪除下列這些代碼前的注釋符號#
LoadModule socache_shmcb_module libexec/apache2/mod_socache_shmcb.so
再次測試缕碎,顯示Syntax OK:
$ sudo apachectl -t
Syntax OK
說明測試通過,重啟 Apache:
$ sudo apachectl restart
此時池户,就可以使用 HTTPS 訪問本地服務(wù)了咏雌,在瀏覽器中輸入https://local.server.com/檢查凡怎。
作者:小白不是總
鏈接:http://www.reibang.com/p/d006a34a343f
來源:簡書
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)赊抖,非商業(yè)轉(zhuǎn)載請注明出處统倒。