mac 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

創建用戶級根目錄

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

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

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

創建之后,打開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/~usernamehttp://localhost/~username,即可測試用戶目錄是否工作。

啟動 PHP

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

編輯/etc/apache2/httpd.conf文件,找到LoadModule php5_module libexec/apache2/libphp5.so并刪除行前的注釋符號#。

重啟 Apache:sudo apachectl restart。

現在 PHP 應該已經可以工作了,在頁面中嵌入可以查看 PHP 信息。

命在終端輸入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

創建主機密鑰

$ 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)

$

創建簽署申請

$ 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。

創建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

創建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

加載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中已經有一條記錄,我們將其注釋掉,新建一條:

#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 訪問服務器,我們需要配置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?).

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

作者:小白不是總

鏈接:http://www.lxweimin.com/p/d006a34a343f

來源:簡書

著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

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

推薦閱讀更多精彩內容

  • 我們經常性的需要使用局域網搭建 Web 服務器測試環境,如部署局域網無線安裝企業應用等,Mac OS X 自帶了 ...
    小白不是總閱讀 55,497評論 24 134
  • 下面的證書是通過mac終端命令行生成的。參考文件:http://www.lxweimin.com/p/f312a84...
    xiaofengl閱讀 20,120評論 1 9
  • 服務器https配置 配置https操作說明文檔 1、查看服務器環境配置(tomcat和apache合并使用) 2...
    南京楊小兵閱讀 8,919評論 0 9
  • 一、概述 二、編譯安裝 三、httpd服務基礎 四、httpd.conf配置文件 五、httpd服務訪問控制 六、...
    紫_軒閱讀 965評論 0 0
  • @轉自GitHub 介紹js的基本數據類型。Undefined、Null、Boolean、Number、Strin...
    YT_Zou閱讀 1,198評論 0 0