Mac OS X Snow Leopard 開啟Web共用,建立Web伺服器:Apache+PHP+MySql

來源:互聯網
上載者:User

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)

 

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.網域名稱服務 (DNS)

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

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

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


如果你的外網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/ 用著還行,功能挺全面的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.