Configure Apache + PHP + MySQL in Mac OS X

Source: Internet
Author: User
Tags bz2 mcrypt mysql in posix sqlite phpmyadmin


Configure Apache + PHP + MySQL in Mac OS X


Mac OS X has built-in Apache and PHP, so it's easy to use. This article takes Mac OS X 10.6.3 and10.8.1as an example. The main contents include:


    1. Launch Apache
    2. Running PHP
    3. Install MySQL
    4. Using phpMyAdmin
    5. Configuring PHP's MCrypt Extension library
    6. Set up a virtual host
Launch Apache


There are two ways of doing this:


    1. Open Web share (Web sharing), System settings Preferences (Preferences), sharing (sharing).Note that Web sharing (Web sharing) is canceled from Mac OS x starting with 10.8.
    2. Open terminal (terminal), then (note that sudo requires a password that is the root account password of the system)
      1. Run "sudo apachectl start", then enter the account password so that Apache runs.
      2. Run "sudo apachectl -v" and you will see the Apache version information for Mac OS X, as in 10.8.1:
        Server version: Apache/2.2.22 (Unix)Server built:   Jun 20 2012 13:57:09


So enter "http://localhost" in the browser, you can see a content of "It works!" Page, which is located under/library (Resource Library)/webserver/documents/, which is the default root directory for Apache.






NOTE: When Apache is turned on, "web sharing" is turned on, and the networked user accesses the "/library (repository)/webserver/documents/" directory via "http://[local ip]/", http://[local ip]/ ~[user Name] "To access the/users/[user name]/sites/directory.It is worth noting that when Mac OS x cancels "Web share (Web sharing)" in 10.8, it also removes the "/users/[User name]/sites/" directory, so accessing "http://[local ip]/~[User name" in 10.8 displays " 403 Forbidden ", but http://[local ip]/can still be accessed.you can go to "system Preferences", "Security", "Firewall (Firewall)", turn on the firewall, and in the "Firewall options (Firewall options)" Tick "organize all incoming connections (block all Incoming connections) "can. You can also set httpd.conf to allow only localhost and 127.0.0.1 to access the/library (Resource library)/webserver/documents/.


<Directory "/Library/WebServer/Documents">
    ......
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    #Allow from all
    Allow from 127.0.0.1
    Allow from localhost 

</Directory>
Running PHP
  1. Run "" In the terminalsudo vi /etc/apache2/httpd.confto open the Apache configuration file. (If you are not accustomed to operating terminal and VI, you can set up in the Finder to display all system hidden files, remember to restart the Finder after Setup, and then you can find the corresponding files, the arbitrary editing, you need to note that some of the file changes or need to open the root account, However, it is more secure to use it on the terminal as a whole orsudoto obtain root privileges temporarily. )
  2. Find "#LoadModule php5_module libexec/apache2/libphp5.so", remove the previous #, save (enter at the command line:w) and Exit VI (AT command line input:q).
  3. Run "sudo cp /etc/php.ini.default /etc/php.ini" so that you can runsudo vi /etc/php.inito edit php.ini to configure various functions. Like what:
    Adjust the maximum value of the PHP submission file by the following two items, such as the maximum value of the imported data in phpMyAdmin
    Upload_max_filesize = 2M
    Post_max_size = 8M
    Use display_errors to control whether the error message of the PHP program is displayed, which is very useful when debugging PHP programs.
    Display_errors = Off
    
  4. Run "sudo apachectl restart" and restart Apache so that PHP can be used.
  5. Run "sudo cp /Library/WebServer/Documents/index.html.en /Library/WebServer/Documents/info.php", that is, copy the Index.html.en file in the root directory of Apache and rename it to info.php.
  6. Run "" In the terminalsudo vi /Library/WebServer/Document/info.phpso that you can edit the info.php file in VI. In "It s works!" Add "<?php phpinfo(); ?>" to the back and save it. So you can see the information about PHP in the http://localhost/info.php, such as the 10.8 built-in PHP version number is 5.3.13.
Install MySQL


Mac OS x does not have built-in MySQL, so it needs to be installed manually, and the most stable version of MySQL is now 5.5. MySQL provides installation instructions under Mac OS x.


  1. Download MySQL 5.5. Select the appropriate version, if MYSQL-5.5.27-OSX10.6-X86_64.DMG is selected here.
  2. Run DMG and you will find 4 files inside. First click Install Mysql-5.5.27-osx10.6-x86_64.pkg, this is the main MySQL installation package. In general, the installation file will automatically install MySQL under/usr/localthe folder with the same name. If run "mysql-5.5.27-osx10.6-x86_64.dmg" will install MySQL to "/usr/local/mysql-5.5.27-osx10.6-x86_64", the default installation is complete. (Note that starting from 10.8, Mac OS x has more restrictive permissions, and a direct click will prompt "Mysql-5.5.27-osx10.6-x86_64.pkg can ' t be opened because it's from a unidentified deve Loper. Your security preferences allow installation for only apps from the Mac App Store and identified developers. " Block the installation, you can use the two-finger click on the installation file, in the pop-up menu select "With ... Turn on (open with), and then select Install (Installer) to proceed with the installation. )
  3. Installing the 2nd file Mysqlstartupitem.pkg,mysql automatically starts at boot time. (Note that the 10.8 installation method is the same as above.) )
  4. Install the 3rd file Mysql.prefpane, you will see "MySQL" icon in "System Preferences", through which you can control whether MySQL is turned on, and whether it runs automatically when booting. The basic installation of MySQL is complete here. (Note that in 10.8, click the installation file with two fingers and select "Use ..." in the popup menu. Turn on (open with), and then select System perference to install it. )
  5. by runningsudo vi/etc/bashrc, addmysqlstart,MySQL, andin the bash configuration file Mysqladmin the alias of the(note: You need to exit "terminal (Terminal)" After the modification is complete, these commands will not take effect):
    #mysql
    alias mysqlstart=‘sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart‘
    alias mysql=‘/usr/local/mysql/bin/mysql‘
    alias mysqladmin=‘/usr/local/mysql/bin/mysqladmin‘

    This makes it easier to perform the corresponding operation in the terminal by command. Start installing mysqlstartupitem.pkg to "/library/startupitems/mysqlcom/" to control MySQL running, autorun, stop, shutdown, and so on. Running theMySQLormysqladmincommand directly when MySQL is not started prompts "Can ' t connect to local MySQL server through Socket '/tmp/mysql.sock ' (2)", so we can start MySQL via the control Panel or run themysqlstartcommand directly, then runMySQLOr themysqladmincommand is OK. For example, after installation, the default password for MySQLrootis empty, if you want to set the password can be run in the terminal "mysqladmin-u root password" Mysqlpassword "" to set, Where Mysqlpassword is the root password. For more information, see b.5.4.1. How to Reset the Root Password.


Note: Mac OS x Upgrades or other reasons may cause MySQL to start or boot automatically when the MySQL operator panel will prompt "Warning:The /usr/local/mysql/data directory is not owned by the ‘mysql‘ or ‘_mysql‘ ", this should be caused in some cases/usr/local/mysql/datathe host has changed, only need to run "sudo chown -R mysql /usr/local/mysql/data".



Also, using PHP to connect MySQL may error "can ' t connect to local MySQL server through socket '/var/mysql/mysql.sock '", or using localhost can not connect to MySQL and need 127.0.0.1, because PHP by default when connecting to find/var/mysql/mysql.sock, but the Mac version of MySQL changed the file location, placed in/tmp. The approach is to modify php.ini as follows:


mysql.default_socket = /tmp/mysql.sock
Using phpMyAdmin


phpMyAdmin is a PHP-developed program for managing MySQL, which is very popular and practical. Being able to manage MySQL with phpMyAdmin is a very effective way to test the previous steps.


  1. Download phpMyAdmin. Select the appropriate version, for example, choose phpmyadmin-3.5.22-all-languages.tar.bz2 this version.
  2. Extract the phpmyadmin-3.5.22-all-languages.tar.bz2 file from download (downloads) to "/Library/WebServer/Documents/" and change the name to phpMyAdmin.
    sudo tar -xf ~/Downloads/phpMyAdmin-3.5.2.2-all-languages.tar.bz2 -C
                 /Library/WebServer/Documents/
    sudo mv /Library/WebServer/Documents/phpMyAdmin-3.5.2.2-all-languages
                /Library/WebServer/Documents/phpmyadmin
  3. Copy the/Library/WebServer/Documents/phpmyadmin/config.sample.inc.php in "" and name it config.inc.php
  4. Edit the config.inc.php and modify the following:
    For cookie encryption, random long strings
    $cfg[‘blowfish_secret‘] = ‘a8b7c6d‘;
    
    When "#2002 Cannot log in to the MySQL server" appears in phpMyAdmin,
    Please change localhost to 127.0.0.1 and ok.
    This is because the MySQL daemon does IP binding (bind-address =127.0.0.1)
    $cfg[‘Servers‘][$i][‘host‘] = ‘localhost’;
    
    Change false to true so that you can access MySQL without passwords.
    Even if MySQL has set a password, you can set it up, and then enter the password when logging in to phpMyAdmin.
    $cfg[‘Servers‘][$i][‘AllowNoPassword‘] = false;
    
  5. This will allow you tohttp://localhost/phpmyadminaccess the phpMyAdmin. You will see a prompt "Unable to load the mcrypt extension, please check your PHP configuration." (The MCrypt extension is missing. Please check your PHP configuration.) ", this will involve installing the mcrypt extension in the next section.
Configuring PHP's MCrypt Extension


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


  1. Download libmcrypt-2.5.8.tar.gz.
  2. Execute the following command at the terminal (note that the following commands need to install Xcode support, you can directly go to the Mac App store to download, after installation may be found in the terminal operation./configure --disable-posix-threads --enable-staticwill be error, runmakewill prompt the command does not exist, at this time also need to open Xcode, and then in Xcode software "Configuration (Preference ... ), download (Downloads), install "command line tools":
    cd ~/Downloads
    tar -zxvf libmcrypt-2.5.8.tar.bz2
    cd libmcrypt-2.5.8
    ./configure --disable-posix-threads --enable-static
    make
    sudo make install
  3. Download php source file php-5.3.13.tar.bz2, remember to choose Chinese mirror will be faster. Mac OS X 10.6.3 Pre-installed PHP version is 5.3.1,10.8 version is 5.3.13, and now the latest version of PHP is 5.4.6, so you need to choose the corresponding version according to their actual situation, this article takes PHP version 10.8 for example.
  4. Before the command can be run, execute the following command on the terminal, put the php-5.3.13.tar.bz2, and configure autoconf (you need to configure it in the new Mac OS x Xcode)phpize:
    cd ~/Downloads
    tar -zxvf php-5.3.13.tar.bz2
    
    cd php-5.3.13/ext/mcrypt
    curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
    tar -zxvf autoconf-latest.tar.gz
    cd autoconf-2.69
    ./configure
    make
    sudo make install
    
    cd ..
    phpize
    ./configure
    make
    sudo make install
  5. Open php.ini
    sudo vi /etc/php.ini

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

    extension=mcrypt.so


When you rehttp://localhost/phpmyadmin-visit, you will find "unable to load the mcrypt extension, please check your PHP configuration." "The hint is gone, which means the MCrypt expansion Library was installed successfully. If it is not yet loaded, try to change the addition of PHP.iniextensionto:


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


Installing MCrypt extensions under Mac OS X is really complicated, and there are a few small problems that can be seen with a little bit of caution, and you may also refer to the how-to-install mcrypt for PHP on Mac OSX Lion 10.8 & 10.7 Developmen T server and adding MCRYPT to your OSX Lion PHP install


Set up a virtual host
  1. Run "" At Terminalsudo vi /etc/apache2/httpd.confto open the Apche configuration file
  2. Find "" In httpd.conf#Include /private/etc/apache2/extra/httpd-vhosts.conf, remove the previous ""#, save and exit.
  3. Run "sudo apachectl restart", after restarting Apache, the virtual Host Configuration feature is turned on.
  4. Runsudo vi /etc/apache2/extra/httpd-vhosts.conf, the configuration virtual host file httpd-vhost.conf is turned on, and the virtual host is configured. It is important to note that the file is enabled by default for two virtual hosts as an example:
    <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 host is configured, it may result in the following prompt when accessing localhost:


    ForbiddenYou don‘t have permission to access /index.php on this server


    The simplest way to do this is to add the # to the front of each line and comment it out, so that you can refer to it without causing other problems.


  5. Add the following configuration
    <VirtualHost *:80>
        DocumentRoot "/Library/WebServer/Documents"
        ServerName localhost
        ErrorLog "/private/var/log/apache2/localhost-error_log"
        CustomLog "/private/var/log/apache2/localhost-access_log" common
    </VirtualHost> 
    
    <VirtualHost *:80>
        DocumentRoot "/Users/[用户名]/Sites"
        ServerName sites
        ErrorLog "/private/var/log/apache2/sites-error_log"
        CustomLog "/private/var/log/apache2/sites-access_log" common
        <Directory />
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride None
                    Order deny,allow
                    Allow from all
          </Directory>
    </VirtualHost>

    Save the exit and restart Apache.

  6. Run "sudo vi /etc/hosts", open the Hosts profile, add "127.0.0.1 sites", so you can configure the completion of the sites virtual host, you can access "Http://sites", before 10.8 mac OS x version of its content and "http://localhost/~ [user name] is exactly the same.
  7. Note that the log "ErrorLog "/private/var/log/apache2/sites-error_log"" can also be deleted, but logging is actually a good habit, in the event of a problem can help us to judge. If you keep these log code, a certain log file path is present, if you arbitrarily modify a non-existent, will cause Apache can not service without error prompts, this is more disgusting.


Here the configuration is implemented using Mac OS X 10.6.3 and 10.8.1 native support, also refer to "Mac OS X Leopard: Configure Apache, PHP, SQLite, MySQL, and PhpMyAdmin (i)" and "Mac OS X Leopard: Configure Apache, PHP, SQLite, MySQL, and PhpMyAdmin (ii) ". In fact, it is possible to use XAMPP or MacPorts as a third-party integration solution for simple installation and use.


You may be interested in:
    • Mysql&phpmyadmin installed in Win
    • Enable mod rewrite and. htaccess
    • Migrating data from MySQL4.0 to MySQL5
    • Use phpMyAdmin to migrate WordPress data from MySQL4 to MySQL5
    • Installing the apache+php under Windows+iis
    • Upgrade to Wordpress2.3 Summary
    • Tips for Ajax
    • Ssi


Configure Apache + PHP + MySQL in Mac OS X


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.