Home  >  Article  >  php教程  >  Configuring Apache + PHP + MySQL in Mac OS X 10.8

Configuring Apache + PHP + MySQL in Mac OS X 10.8

WBOY
WBOYOriginal
2016-07-09 09:09:23989browse

Configuring Apache+PHP+MySQL in Mac OS X 10.8 includes:

  1. Configuring Apache
  2. Configure PHP
  3. Install MySQL
  4. Configure PHPAdmin
  5. Set database default character set

1. Configure Apache

1. Start Apache

Open the terminal and enter:

sudo apachectl start

Open the browser and enter:

http://localhost

You should see the "It works!" page, which is located in the /Library/WebServer/Documents/ directory, which is the default root directory of Apache.

2. Configure user access directory

In the terminal enter:

mkdir ~/Sites
cp /Library/WebServer/Documents/index.html.en index.html

Create a new directory named Sites in the user directory as the access path to the user directory, and copy /Library/WebServer/Documents/index.html to the user directory

Enter:

<span style="color: #000000;">cd /etc/apache2/users/
sudo vi apple.conf</span>

Note: apple is your username.

In vi, press i to start typing and enter the following:

<Directory "/Users/apple/Sites/">
  Options Indexes MultiViews
  AllowOverride All
  Order allow,deny
  Allow from all
</Directory>

After completing the input, press the ESC key, then enter: wq, save and close vi.

Note: The first line of the file is used to specify the location of the user directory, where apple is your username.

In the terminal enter:

sudo apachectl restart

Restart Apache and now you can access it in the browser:

http://localhost/~apple

2. Configure PHP

In the terminal enter:

cd /etc/apache2/
sudo vi httpd.conf

In vi, enter /php to search for text containing php and find:

#LoadModule php5_module libexec/apache2/libphp5.so

Delete the # in front, then save and exit. (Press shift i to enter at the beginning of the line, press ESC to exit editing, press x to delete the current character, and #, enter: wq, save and exit.)

Enter in the terminal:

cd /etc
sudo cp php.ini.default php.ini
sudo apachectl restart

Enter in the terminal:

cd ~/Sites
vi info.php

Then enter the following into info.php:

<span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">><span style="color: #800000;">body</span><span style="color: #0000ff;">><span style="color: #800000;">h1</span><span style="color: #0000ff;">></span>It works!<span style="color: #0000ff;"></span><span style="color: #800000;">h1</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #ff00ff;">php phpinfo(); </span><span style="color: #0000ff;">?></span><span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span>

Enter in the browser:

http://localhost/~apple/info.php

3. Install Mysql

1. Download mysql-5.6.12-osx10.7-x86_64.dmg from the Mysql official website and double-click to open the dmg file.

2. Run mysql-5.6.12-osx10.7-x86_64.pkg to install the main package;

3. Run MySQLStartupItem.pkg to let mysql run automatically at boot;

4. Run MySQL.prefPane and add the mysql service management option in the system preferences;

Enter in the terminal:

<span style="color: #0000ff;">sudo</span> <span style="color: #0000ff;">chmod</span> +<span style="color: #0000ff;">w</span><span style="color: #000000;"> bashrc

</span><span style="color: #0000ff;">sudo</span> <span style="color: #0000ff;">vi</span> /etc/bashrc

Add the following two command aliases at the end of bashrc to facilitate quick use of mysql

#mysql
alias mysql='/usr/local/mysql/bin/mysql'
alias mysqladmin='/usr/local/mysql/bin/mysqladmin'

Tip: After adding the command alias in bashrc, you need to restart the terminal.

Change the default password of mysql and enter in the terminal:

mysqladmin -u root password "123"

You can specify any password at position 123.

If you want to change your password, you can enter

mysqladmin -u root -p password "123"

Before changing the password, you need to enter the previous correct password.

4. Configure PHPAdmin

1. Download PHPAdmin, extract it to the ~/Sites directory, and rename the directory to: phpmyadmin;

2. Enter in the browser:

http://localhost/~apple/phpmyadmin/setup/

Just add a server configuration.

5. Set the database default character set

Enter in the terminal:

mysql -u root -p
# 创建名为 mydb 的数据库
create database mydb;
# 将 mydb 的默认字符集设置为 utf8
alter database mydb default character set = utf8;

Note: The default character set used by mysql is latin1, which does not support Chinese and needs to be set.

Get it done and call it a day :)

P.S.

There are many documents on the Internet about configuring php mysql on mac. This article only focuses on my usage needs and simply records the steps. :)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn