我們經(jīng)常性的需要使用局域網(wǎng)搭建 Web 服務(wù)器測(cè)試環(huán)境,如部署局域網(wǎng)無(wú)線安裝企業(yè)應(yīng)用等,Mac OS X 自帶了 Apache 和 PHP 環(huán)境,我們只需要簡(jiǎn)單的啟動(dòng)它就行了。
啟動(dòng) Apache
查看 Apache 版本
打開(kāi)終端,輸入httpd -v可以查看 Apache 版本信息。
$ httpd -v
Server version: Apache/2.4.16 (Unix)
Server built:? Aug 22 2015 16:51:57
$
啟動(dòng) Apache
在終端輸入sudo apachectl start即可啟動(dòng) Apache。
啟動(dòng)后,在瀏覽器中輸入http://127.0.0.1或http://localhost如果看到It Works!頁(yè)面:

img_01.png
那么 Apache 就啟動(dòng)成功了,站點(diǎn)的根目錄為系統(tǒng)級(jí)根目錄/Library/WebServer/Documents。
啟動(dòng)后,你可以通過(guò)編輯/etc/apache2/httpd.conf文件來(lái)修改 Apache 配置。
停止 Apache:sudo apachectl stop
重啟 Apache:sudo apachectl restart
創(chuàng)建用戶級(jí)根目錄
我們也可以創(chuàng)建用戶級(jí)根目錄,更方便管理和操作。
在用戶目錄下創(chuàng)建Sites目錄,cd; mkdir Sites; touch Sites/.localized,舊的 Mac 系統(tǒng)中如果該目錄已存在,則略過(guò)。
cd /etc/apache2/users檢查目錄下是否存在username.conf文件,username為當(dāng)前用戶名,如果沒(méi)有則創(chuàng)建一個(gè)sudo touch username.conf,并修改文件權(quán)限sudo chmod 644 username.conf。
創(chuàng)建之后,打開(kāi)username.conf文件,sudo vi? username.conf將下面的配置信息寫(xiě)入文件,username依然為當(dāng)前用戶名:
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
編輯/etc/apache2/httpd.conf文件,找到下列代碼,并將前面的注釋符號(hào)#刪除:
Include /private/etc/apache2/extra/httpd-userdir.conf
LoadModule userdir_module libexec/apache2/mod_userdir.so
編輯/etc/apache2/extra/httpd-userdir.conf文件,找到下列代碼,并將前面的注釋符號(hào)#刪除:
Include /private/etc/apache2/users/*.conf
重啟 Apache:sudo apachectl restart
在瀏覽器中輸入http://127.0.0.1/~username或http://localhost/~username,即可測(cè)試用戶目錄是否工作。
啟動(dòng) PHP
Mac OS X 也默認(rèn)集成了 PHP 環(huán)境,如果測(cè)試需要用到 PHP 環(huán)境,可以通過(guò)配置手動(dòng)開(kāi)啟。
編輯/etc/apache2/httpd.conf文件,找到LoadModule php5_module libexec/apache2/libphp5.so并刪除行前的注釋符號(hào)#。
重啟 Apache:sudo apachectl restart。
現(xiàn)在 PHP 應(yīng)該已經(jīng)可以工作了,在頁(yè)面中嵌入可以查看 PHP 信息。
命在終端輸入php --ini可查看 PHP 配置文件,我們可以將/private/etc/php.ini.default復(fù)制一份命名為/private/etc/php.ini并修改配置文件,如,設(shè)置display_errors = On打開(kāi)PHP錯(cuò)誤顯示。
安裝 MySQL
Mac OS X 沒(méi)有集成 MySQL,需要自己安裝,這個(gè)后續(xù)補(bǔ)充。
開(kāi)啟 HTTPS
如果測(cè)試需要 HTTPS 環(huán)境,如, iOS 7.1 以上的設(shè)備部署無(wú)線安裝環(huán)境就必須使用 HTTPS,我們可以配置 Apache 開(kāi)啟 HTTPS 服務(wù)。
創(chuàng)建自簽名證書(shū)
首先創(chuàng)建一個(gè) ssl 目錄用來(lái)存放證書(shū)
$ cd /etc/apache2/
$ sudo mkdir ssl
$ cd ssl
創(chuàng)建主機(jī)密鑰
$ 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]-----+
$
這里會(huì)被要求提供一個(gè)密碼用于主機(jī)密鑰,可以選擇任何的密碼或直接留空。
也可以使用下面的命令創(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)建簽署申請(qǐ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)會(huì)提示輸入各項(xiàng)信息,由于這是自簽名的證書(shū),除了Common Name (e.g. server FQDN or YOUR name) []:FQDN( fully qualified domain name)必須是服務(wù)器域名或 IP 外,其他都不重要,可以隨意填寫(xiě)或一路回車(chē),這里作為測(cè)試使用local.server.com。
創(chuàng)建SSL證書(shū)
在生產(chǎn)環(huán)境中,我們需要提交證書(shū)申請(qǐng)(CSR)文件給證書(shū)頒發(fā)機(jī)構(gòu),由證書(shū)頒發(fā)機(jī)構(gòu)提供SSL證書(shū)。
$ 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
$
我們也可以直接通過(guò)以下的命令創(chuàng)建證書(shū):
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout local.server.com.key -out local.server.com.crt
創(chuàng)建NOPASS密鑰
為了配置 Apache,我們需要?jiǎng)?chuàng)建一個(gè) 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文件,刪除下列代碼前的注釋符號(hào)#:
LoadModule ssl_module libexec/apache2/mod_ssl.so
包含httpd-ssl.conf文件,編輯/etc/apache2/httpd.conf文件,刪除下列代碼前的注釋符號(hào)#:
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èn)服務(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文件,刪除下列這些代碼前的注釋符號(hào)#
LoadModule socache_shmcb_module libexec/apache2/mod_socache_shmcb.so
再次測(cè)試,顯示Syntax OK:
$ sudo apachectl -t
Syntax OK
說(shuō)明測(cè)試通過(guò),重啟 Apache:
$ sudo apachectl restart
此時(shí),就可以使用 HTTPS 訪問(wèn)本地服務(wù)了,在瀏覽器中輸入https://local.server.com/檢查。
作者:小白不是總
鏈接:http://m.itdecent.cn/p/d006a34a343f
來(lái)源:簡(jiǎn)書(shū)
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。