Version 51 (modified by dweber@…, 15 years ago) (diff) |
---|
Getting Apache, MySQL, and PHP Running under MacPorts
- Audience: Users who don’t like the default OS X installation
- Requires: MacPorts
Introduction
These instructions are written for the following versions:
- Apache 2.2.*
- MySQL 5.0.*
- PHP 5.3.*
Installation
Step 1: Turn off Apple's "Personal Web Sharing"
If you haven’t already done so, make sure you turn off Apple’s "Personal Web Sharing" in the System Preferences so that the default Apache server is not running. While you can set things up so that both servers run on different ports (e.g., port 80 and port 8080), it’s not worth the hassle of maintaining two web server daemons. Just use the latest Apache from MacPorts and be done with it.
Step 2: Install Apache
sudo port install apache2
A sample configuration file is provided in /opt/local/apache2/conf/original/httpd.conf
. Don't edit this file; leave it as is so you can refer to the defaults later. The first time you install apache2, the sample configuration file will be copied to /opt/local/apache2/conf/httpd.conf
for you. You can edit this file as desired. When you later upgrade apache2 to a newer version, MacPorts will upgrade the sample configuration file but will not modify your httpd.conf
, so be sure to manually compare your httpd.conf
with the new sample configuration file and bring over any relevant changes. You should also keep backup copies of your httpd.conf
in a safe place.
Activate your Apache installation so that it starts now and every time you boot your machine:
sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
To verify Apache is now running, point your browser to http://localhost/ (you may need to reload the page). You should see a page that says "It works!"
If desired, reboot your machine and point your browser again to http://localhost/ to confirm that Apache is again running.
User directories
If you would like to be able to access web pages in the Sites directory of your home directory, edit /opt/local/apache2/conf/httpd.conf
and locate the following line:
#Include conf/extra/httpd-userdir.conf
Uncomment it by removing the "#
" at the start of the line so that it reads:
Include conf/extra/httpd-userdir.conf
Then edit /opt/local/apache2/conf/extra/httpd-userdir.conf
add the following lines to the end of the file:
For Mac OS X 10.4 and lower:
# # Include user configurations # Include /private/etc/httpd/users/*.conf
For Mac OS X 10.5 and above:
# # Include user configurations # Include /private/etc/apache2/users/*.conf
Restart Apache using sudo /opt/local/apache2/bin/apachectl -k restart
to make this change take effect. You can then view your personal pages by accessing http://localhost/~username/, where "username" is your Mac OS X account's short name.
Local Apache manual
The Apache manual is available on the Apache web site. If you would like to be able to access a copy of this manual on your web server, edit /opt/local/apache2/conf/httpd.conf
and uncomment the following line:
#Include conf/extra/httpd-manual.conf
So that it reads like this:
Include conf/extra/httpd-manual.conf
Restart Apache using sudo /opt/local/apache2/bin/apachectl -k restart
to make this change take effect. You can then view the manual by accessing http://localhost/manual/.
Secure Sockets (https)
This works for a development system (not recommended for production!).
openssl genrsa -des3 -out server.key 1024 # [enter your passphrase, a simple password we will remove soon] openssl req -new -key server.key -out server.csr # [you can accept all the defaults, it does not matter, or customize it to your liking] openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt # [will ask for passphrase] cp server.key server.key.bak openssl rsa -in server.key.bak -out server.key # [passphrase needs to be typed] sudo cp server.crt /opt/local/apache2/conf/ sudo cp server.key /opt/local/apache2/conf/
Then enable the following in /opt/local/apache2/conf/httpd.conf
LoadModule ssl_module modules/mod_ssl.so # Secure (SSL/TLS) connections Include conf/extra/httpd-ssl.conf
Step 3: Install MySQL
If you merely want to access a MySQL server running on another computer, skip to step 4. If you want to run a MySQL server on this computer, install MySQL like this:
sudo port install mysql5-server
Set up the main database
sudo -u mysql mysql_install_db5 sudo chown -R mysql:mysql /opt/local/var/db/mysql5/ sudo chown -R mysql:mysql /opt/local/var/run/mysql5/ sudo chown -R mysql:mysql /opt/local/var/log/mysql5/
If that doesn’t work try this:
sudo mysql_install_db5 sudo chown -R mysql:mysql /opt/local/var/db/mysql5/ sudo chown -R mysql:mysql /opt/local/var/run/mysql5/
Activate your MySQL server installation so that it autostarts when you boot your machine:
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
alternatively you can use port’s load command
sudo port load mysql5-server
and then verify that it’s running
ps -ax | grep mysql
Set the MySQL root
password (it’s currently empty):
mysqladmin5 -u root -p password <new-password>
where <new-password>
is your new desired root password. You will be prompted for your existing password ("Enter password:
"); since it’s empty, just press Return.
Test everything by logging in to the server.
mysql5 -u root -p
Once you’re logged in, simply exit the session
mysql> exit ;
If desired, reboot your machine and then run
ps -ax | grep mysql
again to verify that the daemon is again running.
Step 4: Install PHP
sudo port install php5 +apache2 sudo port install php5-mysql
Note: php5 contains the core PHP features, but there are many optional features available in separate ports, some of which you may want to install as well (such as MySQL support via the php5-mysql port). Use
port search php5
to see all the options that are available. See also http://www.php.net/manual/mysqli.overview.php
Register PHP with Apache
cd /opt/local/apache2/modules sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
Update Apache’s httpd.conf
file to enhance the "DirectoryIndex" directive to include additional "index" files. Search for:
DirectoryIndex index.html
and change it this way:
DirectoryIndex index.php index.html
Also, at the end of the httpd.conf
file, add the following lines so that Apache includes the mod_php "AddType" configurations
# # Include PHP configurations # Include conf/extra/mod_php.conf
You may also need to load the PHP module before including mod_php.conf
as described above:
# Load the PHP module LoadModule php5_module modules/libphp5.so
Set up your PHP configuration files
cd /opt/local/etc/php5 sudo cp php.ini-development php.ini
or (depending on your needs)
cd /opt/local/etc/php5 sudo cp php.ini-production php.ini
Setup the MySQL default socket to use the MacPorts configuration (/opt/local/var/run/mysql5/mysqld.sock)
$ sudo -i # cd /opt/local/etc/php5 # cp php.ini php.ini.bak # defSock=`/opt/local/bin/mysql_config5 --socket` # cat php.ini | sed \ -e "s#pdo_mysql\.default_socket.*#pdo_mysql\.default_socket=${defSock}#" \ -e "s#mysql\.default_socket.*#mysql\.default_socket=${defSock}#" \ -e "s#mysqli\.default_socket.*#mysqli\.default_socket=${defSock}#" > tmp.ini # grep default_socket tmp.ini # Check it! # mv tmp.ini php.ini # exit # OR rm php.ini.bak && exit
If you installed php5 with the +pear variant you should also:
sudo cp pear.conf.sample pear.conf
Restart Apache so that your changes take effect
sudo /opt/local/apache2/bin/apachectl -k restart
Note: Make sure to use the full path as shown here, as
apachectl
in your PATH refers to the system provided Apache!
Create a file named phpinfo.php
that contains the following three lines
<?php phpinfo(); ?>
and place it in your Apache "DocumentRoot
" directory (should be "/opt/local/apache2/htdocs
") or your own user "Sites
" directory if you activated user directories as specified above. Point your browser to http://localhost/phpinfo.php (or http://localhost/~username/phpinfo.php if applicable) and verify that the correct version of PHP is active (v5.3.0 as of this writing) and that MySQL support is active (you may want to search the page for "mysql
").
As a last check, reboot and verify that everything has autostarted and is running (i.e., repeat the above tests).
Step 5: Install phpMyAdmin (optional)
Use MacPorts to install the latest version of phpMyAdmin
.
sudo port install phpmyadmin
Update Apache’s httpd.conf
file to find phpmyadmin
. First add the following lines to the end of the file:
# Local access to phpmyadmin installation Include conf/extra/httpd-phpmyadmin.conf
and then create a file /opt/local/apache2/conf/extra/httpd-phpmyadmin.conf
containing this text:
AliasMatch ^/phpmyadmin(?:/)?(/.*)?$ "/opt/local/www/phpmyadmin$1" <Directory "/opt/local/www/phpmyadmin"> Options -Indexes AllowOverride None Order allow,deny Allow from all LanguagePriority en de es fr ja ko pt-br ru ForceLanguagePriority Prefer Fallback </Directory>
Restart Apache so that your changes take effect
sudo /opt/local/apache2/bin/apachectl -k restart
Note: Make sure to use the full path as shown here, as
apachectl
in your PATH refers to the system provided Apache!
Finally, you need to set up the phpMyAdmin
configuration to access mySQL. First, set up the config file:
cd /opt/local/www/phpmyadmin/ sudo cp config.sample.inc.php config.inc.php
This will create a file config.inc.php
in the phpMyAdmin
folder. Edit that file, and locate the lines:
$cfg['Servers'][$i]['auth_type'] = 'config'; // Authentication method (config, http or cookie based)? $cfg['Servers'][$i]['user'] = 'root'; // MySQL user $cfg['Servers'][$i]['password'] = ''; // MySQL password (only needed // with 'config' auth_type)
Where ' ' is an empty password; fill it with your MySQL root password. You can either change the 'auth_type' from 'config' to 'cookie' or 'httpd', or alternatively provide the password you selected for the root user in the 'password' option.
There is also an interactive setup, see http://www.phpmyadmin.net/documentation/#setup_script and the demo at http://www.phpmyadmin.net/documentation/setup/
For additional features provided by pmadb (http://wiki.phpmyadmin.net/pma/pmadb), run run this SQL to setup the pma 'controluser' (change the 'pmapass' to your password).
$ mkdir ~/tmp && cd tmp $ cat > tmp.sql GRANT USAGE ON mysql.* TO 'pma'@'localhost' IDENTIFIED BY 'pmapass'; GRANT SELECT ( Host, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Show_db_priv, Super_priv, Create_tmp_table_priv, Lock_tables_priv, Execute_priv, Repl_slave_priv, Repl_client_priv ) ON mysql.user TO 'pma'@'localhost'; GRANT SELECT ON mysql.db TO 'pma'@'localhost'; GRANT SELECT ON mysql.host TO 'pma'@'localhost'; GRANT SELECT (Host, Db, User, Table_name, Table_priv, Column_priv) ON mysql.tables_priv TO 'pma'@'localhost'; -- -- [Hit CNT-D] -- $ mysql5 -u root -p < tmp.sql
Then setup all the tables for the pmadb features with:
mysql5 -u root -p < /opt/local/www/phpmyadmin/scripts/create_tables.sql
To check your phpMyAdmin installation, point your browser to http://localhost/phpmyadmin and verify that phpMyAdmin loads and can access your database (by providing a username and password, depending on the authentication method you selected).