Mac OS X Snow Leopard 开启Web共享,建立Web服务器:Apache+PHP+MySql

来源:互联网 发布:淘宝小号批量购买 编辑:程序博客网 时间:2024/06/05 23:12

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