Mac OS X Snow Leopard 開啓Web共享,建立Web服務器:Apache+PHP+MySql

1.開啓Web Sharing:

System Preferences -> Sharing -> 勾選Web Sharing

網站默認目錄位於 ~/Sites

測試:http://localhost/~ElfSundae  (ElfSundae爲你的用戶名)

 

2.配置Apache,安裝並配置MySql

詳見:

Install Apache/PHP/MySQL on Mac Snow Leopard 

http://www.cnblogs.com/elfsundae/archive/2010/11/27/1889570.html

注意:該篇文章是我轉載國外的一篇文章,是stackoverflow熱烈討論後的精華,建議詳細閱讀並熟記。

 

安裝完MySql後在系統預設裏Start MySql Server

如果需要更改默認端口80,最簡便的方法就是顯示隱藏文件,直接編輯 /private/etc/apache2/httpd.conf

爲MySql添加環境變量PATH:

不會添加PATH的請移步:http://www.cnblogs.com/elfsundae/archive/2010/12/01/1893190.html

 

複製代碼
/*引用MySql ReadMe*/

The Mac OS X PKG of MySQL installs itself into
`
/usr/local/mysql-VERSION' and also installs a symbolic link,
`/usr/local/mysql', that points to the new location. If a directory
named `/usr/local/mysql' exists, it is renamed to
`/usr/local/mysql.bak' first. Additionally, the installer creates the
grant tables in the `mysql' database by executing `mysql_install_db'.
複製代碼

 

$ echo 'export PATH=/usr/local/mysql-5.1.53-osx10.6-x86/bin:$PATH' >> ~/.bash_profile

測試PATH:$ mysql --version
mysql  Ver 14.14 Distrib 5.1.53, for apple-darwin10.3.0 (i386) using readline 5.1

 

建立管理員帳號:帳號:root,密碼:123456

mysqladmin -u root password 123456
mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.1.53 MySQL Community Server (GPL)

 

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.02 sec)

 mysql> quit
Bye

 

3.測試PHP

在/Library/WebServer/Documents 下建立test.php:

 

複製代碼
<html>
<head>
<title> php test - By:Elf Sundae </title>
</head>

<body>
<?php
echo "http://www.cnBlogs.com/ElfSundae <hr>";

phpinfo();
?>

</body>
</html>
複製代碼

 

測試:http://localhost:8080/test.php  (如果你沒有改默認端口,就不需要加:8080)

如果上邊操作都正確,但該鏈接不可以用提示無法連接到localhost ,則重新啓動Apache 就可以了

 

4.測試MySql連接

建表:

mysql> Create database mytest;
Query OK, 1 row affected (0.00 sec)

 

mysql> use mytest;
Database changed
mysql> CREATE TABLE members (
    -> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> password VARCHAR(12) NOT NULL,
    -> username VARCHAR(12) NOT NULL,
    -> index(password),
    -> index(username));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into members values('','ElfSundae','abcdef');
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from members;
+----+-----------+----------+
| id | password  | username |
+----+-----------+----------+
|  1 | ElfSundae | abcdef   |
+----+-----------+----------+
1 row in set (0.00 sec)

mysql>

更改剛纔創建的test.php爲以下內容:

複製代碼
<html>
<head>
<title> php mysql test - By:Elf Sundae </title>
</head>

<body>
<?php
echo "http://www.cnBlogs.com/ElfSundae <hr>";

$link = mysql_connect("localhost","root","123456") or die("Unable to connect to SQL server");
mysql_select_db("mytest",$link) or die("Unable to select database");


$qstr = "SELECT * from members where id = '1' ";
$result = mysql_query($qstr);

if (mysql_num_rows($result))
{
$username = mysql_result($result,0, "username");
$password = mysql_result($result,0, "password");
echo "<b>The username:</b> $username<br>";
echo "<b>The password:</b> $password<br>";
}
else echo "ERROR - unable to find username and password!";

mysql_close();

?>

</body>
</html>
複製代碼
訪問測試:http://localhost:8080/test.php

 

5.外網測試+NAT端口映射

如果你是ADSL或單獨的PPPOE撥號連接,把上面的localhost替換成你的外網IP就可以使網站公開於互聯網。

如果你使用路由器上網,也就是NAT,局域網內對外只有一個公網IP,這時需要在路由器裏設置”端口映射",將80(或者你的Web端口)端口映射到你的局域網IP,這樣輸入http://外網IP[:port]/test.php路由器網關就會把請求扔給你了。

ps.獲取外網IP最簡單的辦法:訪問www.ip138.com。

 

6.域名服務

完成第5步,你就可以把這個帶有外網IP的你的網站地址(URL)發給地球上的任何人了,他們都可以訪問你的網站,而服務器就是你的計算機。

但是,長長的IP地址顯得很不友好,學習期間爲了節省開支,你可以申請一個免費的域名,例如co.cc。

申請地址:http://www.co.cc/?id=834647

CO.CC:Free Domain


如果你的外網IP地址不是固定的,此時需要"動態域名解析",Windows下可以用花生殼,Mac下推薦使用DynDNS:http://www.dyndns.com/

 

7.免費空間

如果你捨不得7*24h開機,更不想花錢去租用服務器或虛擬主機,爲了學習方便,最好的辦法就是尋找免費空間。

PHP的免費空間很多,這兩天在測試個東西,在網上找了幾個,見:

http://www.free-webhosts.com/free-php-webhosting.php

申請成功了一個:http://www.heliohost.org/ 用着還行,功能挺全面的。


原文連接:http://www.cnblogs.com/elfsundae/archive/2010/11/27/1889570.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章