我們經(jīng)常性的需要使用局域網(wǎng)搭建 Web 服務器測試環(huán)境,如部署局域網(wǎng)無線安裝企業(yè)應用等,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
,并修改文件權限sudo chmod 644 username.conf
。-
創(chuàng)建之后,打開
username.conf
文件,sudo vi username.conf
將下面的配置信息寫入文件,username
依然為當前用戶名:<Directory "/Users/username/Sites/"> Options Indexes MultiViews FollowSymLinks AllowOverride All Order allow,deny Allow from all Require all granted </Directory>
-
編輯
/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 應該已經(jīng)可以工作了,在頁面中嵌入
<?php phpinfo(); ?>
可以查看 PHP 信息。 - 命在終端輸入
php --ini
可查看 PHP 配置文件,我們可以將/private/etc/php.ini.default
復制一份命名為/private/etc/php.ini
并修改配置文件,如,設置display_errors = On
打開PHP錯誤顯示。
安裝 MySQL
Mac OS X 沒有集成 MySQL,需要自己安裝,這個后續(xù)補充。
開啟 HTTPS
如果測試需要 HTTPS 環(huán)境,如, iOS 7.1 以上的設備部署無線安裝環(huán)境就必須使用 HTTPS,我們可以配置 Apache 開啟 HTTPS 服務。
創(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)必須是服務器域名或 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
-
添加
<VirtualHost>
到httpd-ssl.conf
,編輯/etc/apache2/extra/httpd-ssl.conf
文件:
httpd-ssl.conf
中已經(jīng)有一條<VirtualHost>
記錄,我們將其注釋掉,新建一條:<VirtualHost *:443> #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: <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory "/Library/WebServer/CGI-Executables"> SSLOptions +StdEnvVars </Directory> </VirtualHost>
為了能夠使用 URL 訪問服務器,我們需要配置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 訪問本地服務了,在瀏覽器中輸入 https://local.server.com/ 檢查。