Enable Web Sharing for Mac OS X snow leopard and create a web server: Apache + PHP + MySql

Source: Internet
Author: User

1. Enable Web Sharing:

System Preferences-> sharing-> check Web Sharing


The default website directory is located in ~ /Sites

Test: http: // localhost /~ Elfsundae (elfsundae is your user name)

 

2. Configure Apache, install and configure MySQL

For details, see:

Install Apache/PHP/MySQL on Mac snow leopard

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

Note: This article is an article I have reposted outside China. It is the essence of stackoverflow's heated discussion. It is recommended to read and record it in detail.

 

After installing MySQL, start MySQL server in the system preset

If you need to change the default port 80, the easiest way is to display the hidden file and directly edit/private/etc/apache2/httpd. conf


Add the environment variable path for MySQL:

Step: http://www.cnblogs.com/elfsundae/archive/2010/12/01/1893190.html if path is not added

 

 /* Reference MySQL readme */

The Mac OS X PKG of MySQL installitself
'/Usr/local/MySQL-version' and also installa symbolic link,
'/Usr/local/mysql', that points to the new location. If a directory
Named '/usr/local/mysql' exists, it is renamed
'/Usr/local/MySQL. Bak' first. Additionally, the installer creates
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

Test path: $ Mysql -- version
MySQL ver 14.14 distrib 5.1.53, for apple-darwin10.3.0 (i386) using Readline 5.1

 

Create an Administrator Account: Account: Root, password: 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,201 0, 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. Test PHP

Create test. php under/library/webserver/documents:


<html>
<head>
<title> php test - By:Elf Sundae </title>
</head>

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

phpinfo();
?>

</body>
</html>










Test: http: // localhost: 8080 / test.php (If you have not changed the default port, you do not need to add: 8080)

 

4. Test MySql connection

Build a table:

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>

Change the test.php you just created to the following:



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



Access test: http: // localhost: 8080 / test.php

 

5. Extranet test + NAT port mapping

If you are using ADSL or a separate PPPOE dial-up connection, replace the localhost above with your external IP to make the website public on the Internet.

If you use a router to access the Internet, that is, NAT, there is only one public IP in the LAN. At this time, you need to set "port mapping" in the router, and map the 80 (or your Web port) port to your LAN IP. Enter http: // 外 网 IP [: port] /test.phpThe router gateway will throw the request to you.

ps. The easiest way to obtain the Internet IP: Visit www.ip138.com.

 

6. Domain Name Service

After completing step 5, you can send your website address (URL) with an external IP to anyone on the earth, they can access your website, and the server is your computer.

However, long IP addresses are very unfriendly. To save money during the study period, you can apply for a free domain name, such as co.cc.

Application address: http://www.co.cc/?id=834647


If your external network IP address is not fixed, you need "Dynamic Domain Name Resolution". Peanut shell can be used under Windows. DynDNS is recommended for Mac: http://www.dyndns.com/

 

7. Free space

If you can't bear to boot 7 * 24h, and don't want to spend money to rent a server or virtual host, for the convenience of learning, the best way is to find free space.

There is a lot of free space in PHP. I have been testing something in the past two days, and I have found a few online. See:

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

The application was successful one: http://www.heliohost.org/ It's okay to use, and the function is quite comprehensive.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.