Build the php development environment tutorial under Mac

  • 2020-05-10 23:11:18
  • OfStack

Mac OS X has Apache and PHP built in, which makes it very easy to use. This paper takes Mac OS X 10.6.3 as an example. The main contents include:

Start the Apache
Run PHP
Install MySQL
Using phpMyAdmin
Configure PHP's MCrypt extension library
Setting up the virtual host

Start the Apache

There are two ways:

Open system preferences (System Preferences) - > "Sharing (Sharing)" - > "Web sharing (Web Sharing)"
Open "terminal (terminal)" and (note: the password required by sudo is the system's root account password)
Run "sudo apachectl start", enter your account password, and Apache will be up and running.

Run "sudo apachectl-v" and you will see Mac OS X 10.6.3 with the Apache version number:

Server version: Apache/2.2.14 (Unix)
Server built:     Feb 11 2010 14:40:31

If you type "http://localhost" into the browser, you will see a message saying "It works!" Is located under /Library /WebServer/Documents/, which is the default root of Apache.

Note: When Apache is enabled, "Web share" is enabled, and users in the network will access the "/Library /WebServer/ WebServer/" directory through "http://[local IP]/", and the "/Users/[local IP]/~[user name]" directory through "http://[local IP]/~[user name]". This access can be disabled by setting "firewall (Firewall)" in "security (Security)" in "system preferences".

Run PHP

Run in the terminal "sudo vi etc/apache2 / httpd conf", open Apache configuration file. (if not used to operating terminals and vi can set in Finder show all hidden system files, remember to restart Finder, so that you can find the corresponding files, follow one's inclinations editor, but it's important to note that some file modifications need open root account, but on the whole, or in terminal is safe to use sudo comes to obtain root permission.)

Find "# LoadModule php5_module libexec/apache2 / libphp5 so", remove the front # #, saved (on the command line, enter: w) and exit vi (on the command line, enter: q).

Run "sudo cp/etc/php ini. default/etc/php ini", so that you can pass php. ini to configure various PHP function. Such as:

; Adjust the maximum value of the PHP submission file through the following two items, such as the maximum value of the imported data in phpMyAdmin

upload_max_filesize = 2M
post_max_size = 8M

; For example, display_errors is used to control whether to display the error of the PHP program

display_errors = Off

Run "sudo apachectl restart" and restart Apache so that PHP is ready to use.

Run "cp/Library/WebServer/Documents/index html. en/Library/WebServer/Documents/info php", namely under the root directory of Apache reproduction index. html. en file and named info. php

In terminal "vi/Library/WebServer/Document/info php", so that you can in vi editor info. php file. In the "It 's works!" Add" < ?php phpinfo(); ? > "And save it. Could be http: / / localhost/info. See the information about PHP php, such as the version number is 5.3.1.

Install MySQL

Since Mac OS X is not pre-installed, it needs to be installed manually. Currently, the most stable version of MySQL is 5.1. MySQL provides installation instructions under Mac OS X.
Download MySQL 5.1. Select the appropriate version, such as mysql-5.1.46-osx10.6-x86_64.dmg.
Run dmg, and you'll find four files inside. First click to install mysql-5.1.46-osx10.6-x86_64.pkg, which is the main installation package of MySQL. In general, the installation file will automatically install MySQL to the folder of the same name under /usr/local. For example, clicking and running "mysql-5.1.46-osx10.6-x86_64.dmg" will install MySQ into "/usr/local/ mysql-5.1.46-osx10.6-x86_64". 1 default installation can be completed.

Click to install the second file MySQLStartupItem.pkg, and MySQL will start automatically at boot time.

Click to install the third file, MySQL.prefPane, and you will see ICON named "MySQL" in "system preferences". You can set whether MySQL starts or stops, and whether it will run automatically when starting up. At this point, MySQL is basically installed.

Add the alias mysql and mysqladmin to the bash configuration file by running "sudo vi /etc/bashrc"


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

For example, the default password of root of MySQL is empty after installation. If you want to set the password, you can run "mysqladmin-u root password mysqlpassword" at the terminal to set the password, where mysqlpassword is the password of root. For more information, please refer to B.5.4.1.How to Reset the Root Password.

Note: The upgrade of Mac OS X or other reasons may cause MySQL to start or start automatically, On the operation panel MySQL prompt "Warning: The/usr/local mysql/data directory is not owned by the 'mysql or' _mysql '". Or under the command-line prompt "Can 't connect to local MySQL server through socket'/tmp/mysql sock '(2)", it should be in some cases/usr local mysql/data changed the host, Only need to run "sudo chown - R mysql usr/local mysql/data".

In addition, use PHP link MySQL may report errors "Can 't connect to local MySQL server through socket'/var/mysql/mysql sock '", or are unable to connect to MySQL use localhost need 127.0.0.1, Reason is that when connecting php default to find/var mysql/mysql sock, but MAC version MYSQL changed the location of the file, under/tmp. The solution is to modify php as follows.ini:

mysql.default_socket = /tmp/mysql.sock

Using phpMyAdmin

phpMyAdmin is a program developed by PHP to manage MySQL. It is very popular and practical. Being able to use phpMyAdmin to manage MySQL is a very effective way to test the results of the first few steps.

Download phpMyAdmin. Select the appropriate version. For example, I chose phpMyAdmin-3.3.2-all-languages. tar. bz2.

Copy the phpMyAdmin-3.32-all-languages folder in "download (downloads)" to "/Users/[username]/Sites" and change the name to phpmyadmin.

Copy "/ Users / [username] / Sites/phpmyadmin/" the config. sample. inc. php, and named config. inc. php

Open config.inc.php and make the following changes:

Used for Cookie encryption, arbitrarily long strings

$cfg['blowfish_secret'] = '';  

When "#2002 cannot login to MySQL server" appears in phpMyAdmin,

Please change localhost to 127.0.0.1 for ok.

This is due to the MySQL daemon doing the IP binding (bind-address =127.0.0.1)

$cfg['Servers'][$i]['host'] = 'localhost';

Change false to true so that you can access the passwordless MySQL,

You can do this even if MySQL has a password, and then enter it when you log on to phpMyAdmin

$cfg['Servers'][$i]['AllowNoPassword'] = false;

This allows access to phpMyAdmin via http://localhost/~[username]/phpmyadmin. At this point, you will see a prompt "unable to load the mcrypt extension, please check your PHP configuration." , which involves installing the MCrypt extension in the next section.

Configure the MCrypt extension for PHP

MCrypt is a powerful encryption algorithm extension library, which includes 22 algorithms. phpMyAdmin relies on the PHP extension library. But its installation under Mac OS X is less friendly, as follows:

Download and unzip libmcrypt-2.5.8. tar. bz2.

Execute the following command at the terminal (note that the following command is required to install xcode support) :


cd ~/Downloads/libmcrypt-2.5.8/
./configure --disable-posix-threads --enable-static
make
sudo make install

Download and unzip the PHP source file php-5.3.1.tar.bz2. Mac OS X 10.6.3 pre-installed version of PHP is 5.3.1, but now the latest version of PHP is 5.3.2, you need to choose the corresponding version according to your actual situation.

Execute the following command at the terminal:


cd ~/Downloads/php-5.3.1/ext/mcrypt
phpize
./configure
make
cd modules
sudo cp mcrypt.so /usr/lib/php/extensions/no-debug-non-zts-20090626/

Open the php ini

sudo vi /etc/php.ini

Add the following code to php.ini, save and exit, then restart Apache

extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so

When you visit http://localhost/~[username]/phpmyadmin again, you will find "unable to load mcrypt extension, please check your PHP configuration". ", which means the MCrypt extension library was installed successfully.

Set up the virtual host  

The terminal operation "sudo vi etc/apache2 / httpd conf", open Apche configuration file

Found in httpd. conf "# Include/private etc/apache2 extra/httpd - vhosts. conf", remove the "#" in front of, save and exit.

Run "sudo apachectl restart" and restart Apache to enable its virtual host configuration.

Run "sudo vi etc/apache2 extra/httpd - vhosts. conf", thus opened the virtual host configuration file httpd - vhost. conf, configure the virtual host you need. It is important to note that this file has two example virtual hosts turned on by default:


<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/usr/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
    CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/usr/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
    CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost> 

In fact, these two virtual hosts do not exist, and when no other virtual hosts are configured, the following prompt may appear when accessing localhost:

Forbidden

You don't have permission to access /index.php on this server

The easiest way to do this is to # them before each line and comment them out so that you can refer to them without causing any other problems.

Add the following configuration


<VirtualHost *:80>
    DocumentRoot "/Users/[ The user name ]/Sites"
    ServerName sites
    ErrorLog "/private/var/log/apache2/sites-error_log"
    CustomLog "/private/var/log/apache2/sites-access_log" common
</VirtualHost> 

Save to exit, and restart Apache.

Run "sudo vi /etc/hosts", open the hosts configuration file, add "127.0.0.1 sites", and then the sites virtual host can be configured, so that it can be accessed with "http://sites", the content and "http://localhost/~[user name]" are completely 1 to 1.

This is a configuration that USES native support in Mac OS X 10.6.3, or "Mac OS SQLite SQLite and phpMyAdmin(1)" and "Mac OS X Leopard: Configure Apache, PHP, SQLite, MySQL, and phpMyAdmin(2) ". In fact, you can also use third-party integration solutions like XAMPP or MacPorts for easy installation and use.


Related articles: