Mac OS X 啟用 Web 服務器

我們經常性的需要使用局域網搭建 Web 服務器測試環境,如部署局域網無線安裝企業應用等,Mac OS X 自帶了 Apache 和 PHP 環境,我們只需要簡單的啟動它就行了。

啟動 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.1http://localhost 如果看到 It Works! 頁面:

    img_01.png

    那么 Apache 就啟動成功了,站點的根目錄為系統級根目錄 /Library/WebServer/Documents。

    啟動后,你可以通過編輯 /etc/apache2/httpd.conf 文件來修改 Apache 配置。

  • 停止 Apache:sudo apachectl stop

  • 重啟 Apache:sudo apachectl restart

創建用戶級根目錄

我們也可以創建用戶級根目錄,更方便管理和操作。

  1. 在用戶目錄下創建 Sites 目錄,cd; mkdir Sites; touch Sites/.localized,舊的 Mac 系統中如果該目錄已存在,則略過。

  2. cd /etc/apache2/users 檢查目錄下是否存在 username.conf 文件,username 為當前用戶名,如果沒有則創建一個 sudo touch username.conf,并修改文件權限 sudo chmod 644 username.conf。

  3. 創建之后,打開 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>
    
  4. 編輯 /etc/apache2/httpd.conf 文件,找到下列代碼,并將前面的注釋符號 # 刪除:

    Include /private/etc/apache2/extra/httpd-userdir.conf
    
    LoadModule userdir_module libexec/apache2/mod_userdir.so
    
  5. 編輯 /etc/apache2/extra/httpd-userdir.conf 文件,找到下列代碼,并將前面的注釋符號 # 刪除:

    Include /private/etc/apache2/users/*.conf
    
  6. 重啟 Apache:sudo apachectl restart

在瀏覽器中輸入 http://127.0.0.1/~usernamehttp://localhost/~username,即可測試用戶目錄是否工作。

啟動 PHP

Mac OS X 也默認集成了 PHP 環境,如果測試需要用到 PHP 環境,可以通過配置手動開啟。

  1. 編輯 /etc/apache2/httpd.conf 文件,找到 LoadModule php5_module libexec/apache2/libphp5.so 并刪除行前的注釋符號 #
  2. 重啟 Apache:sudo apachectl restart。
  3. 現在 PHP 應該已經可以工作了,在頁面中嵌入 <?php phpinfo(); ?> 可以查看 PHP 信息。
  4. 命在終端輸入 php --ini 可查看 PHP 配置文件,我們可以將 /private/etc/php.ini.default 復制一份命名為 /private/etc/php.ini 并修改配置文件,如,設置 display_errors = On 打開PHP錯誤顯示。

安裝 MySQL

Mac OS X 沒有集成 MySQL,需要自己安裝,這個后續補充。

開啟 HTTPS

如果測試需要 HTTPS 環境,如, iOS 7.1 以上的設備部署無線安裝環境就必須使用 HTTPS,我們可以配置 Apache 開啟 HTTPS 服務。

創建自簽名證書

首先創建一個 ssl 目錄用來存放證書

$ cd /etc/apache2/
$ sudo mkdir ssl
$ cd ssl
  1. 創建主機密鑰

    $ 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]-----+
    $ 
    

    這里會被要求提供一個密碼用于主機密鑰,可以選擇任何的密碼或直接留空。

    也可以使用下面的命令創建密鑰:

    $ sudo openssl genrsa -out local.server.com.key 2048
    Generating RSA private key, 2048 bit long modulus
    ....+++
    ....+++
    e is 65537 (0x10001)
    $
    
  2. 創建簽署申請

    $ 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 []:
    $ 
    

    系統會提示輸入各項信息,由于這是自簽名的證書,除了 Common Name (e.g. server FQDN or YOUR name) []: FQDN( fully qualified domain name)必須是服務器域名或 IP 外,其他都不重要,可以隨意填寫或一路回車,這里作為測試使用 local.server.com

  3. 創建SSL證書
    在生產環境中,我們需要提交證書申請(CSR)文件給證書頒發機構,由證書頒發機構提供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
    $ 
    

    我們也可以直接通過以下的命令創建證書:

    $ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout local.server.com.key -out local.server.com.crt
    
  4. 創建NOPASS密鑰
    為了配置 Apache,我們需要創建一個 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

  1. 加載 mod_ssl.so,編輯 /etc/apache2/httpd.conf 文件,刪除下列代碼前的注釋符號 #

    LoadModule ssl_module libexec/apache2/mod_ssl.so
    
  2. 包含 httpd-ssl.conf 文件,編輯 /etc/apache2/httpd.conf 文件,刪除下列代碼前的注釋符號 #

    Include /private/etc/apache2/extra/httpd-ssl.conf
    
  3. 添加 <VirtualHost>httpd-ssl.conf,編輯 /etc/apache2/extra/httpd-ssl.conf 文件:
    httpd-ssl.conf 中已經有一條 <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>
    
  4. 為了能夠使用 URL 訪問服務器,我們需要配置HOST,sudo vi /etc/hosts,添加 127.0.0.1 local.server.com

  5. 檢查配置文件并重啟 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?).
    

    根據提示,編輯 /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/ 檢查。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容