OS X 10.10 Yosemite Apache Setup: Multiple PHP Versions
2015-04-01 15:01
726 查看
http://getgrav.org/blog/mac-os-x-apache-setup-multiple-php-versions
Yosemite Web Development Environment: Part 1
Developing web applications on OS X is a real joy. There are plenty of options for setting up your development environments, including the ever-popular MAMP
Pro that provides a nice UI on top of Apache, PHP and MySQL. However, there are times when MAMP Pro has slow downs, or out of date versions,
or is simply behaving badly due to its restrictive system of configuration templates and non-standard builds.
It is times like these that people often look for an alternative approach, and luckily there is one, and it is relatively straight-forward to setup.
In this blog post, we will walk you through setting up and configuring the OS X built-in Apache and multiple PHP versions. In the second blog post in this two-post series, we will cover MySQL, Apache
virtual hosts, APC caching, and Xdebug installation.
This post was updated 03/30/2015 adding PHP 5.6 and how to update packages and switch to specific PHP versions. Also updated 10/26/2014 to reflect OS X 10.10 Yosemite rather than Mavericks.
IMPORTANT: This guide is intended for experienced web developers. If you are a beginner developer, you will be better served using MAMP
or MAMP Pro.
OS X 10.10 Yosemite comes with the Apache 2.4 pre-installed, but there is no longer a Web Sharing preference pane in the System Preferences.
Not to fret! A simple Web sharing preference pane is available from clickontyler.com. This is a scaled down free version
of the more powerful VirtualHostX. VirtualHostX is a pretty UI for controlling Apache, but you don't really need
it.
Another approach is simply to use the provided
pretty scary prospect for many people, but there's no need to fear it.
To start up the web server, simply bring up the Terminal (/Applications/Utilities/Terminal) and type:
To stop apache you would type:
To restart apache when you have made configuration changes, simply type:
So start Apache now, and then try to reach your server in a browser by pointing it at your localhost, you should see a simple header that says It works!.
If you cannot reach your site via
The first thing we will want to do is to change the document root for Apache. This is the folder where Apache looks to serve file from. By default, the document root is configured as
As this is a development machine, let's assume we want to change the document root to point to a folder in our own home directory. To do this, we will need to edit Apache's configuration file.
You can use whatever command line editor you are comfortable with, just realize this is a
you will need to use
Text 3 with the commandline command
If you have never used a command line editor, try
Search for the term
Change this to point to your user directory where
You also need to change the
changed to point to your new document root also:
In that same
this should be changed as follows:
Now we have the Apache configuration pointing to a
however. By default, apache runs as the user
This will cause permission problems when trying to access files in our home directory. About a third of the way down the
there are two settings to set the
will run under. Change these to match your user account (replace
of
Save the file.
Of course you will now need to restart your Apache with
made to the configuration file.
Now, you need to create a
or in Finder. In this new
put some dummy content in it like:
Pointing your browser to
on!
This process relies heavily on the OS X package manager called Homebrew. Using the
you can easily add powerful functionality to your mac.
Xcode 6.1 and the Command Line Tools are required so make sure you install Xcode
via the Mac App Store first. Launch Xcode and agree to the Terms and Conditions, then install the Command Line Tools with this command:
The full installation documentation can be found on the Homebrew Wiki, but we will cover the essentials.
Install the homebrew with the following command in your terminal:
Yosemite Web Development Environment: Part 1
Developing web applications on OS X is a real joy. There are plenty of options for setting up your development environments, including the ever-popular MAMP
Pro that provides a nice UI on top of Apache, PHP and MySQL. However, there are times when MAMP Pro has slow downs, or out of date versions,
or is simply behaving badly due to its restrictive system of configuration templates and non-standard builds.
It is times like these that people often look for an alternative approach, and luckily there is one, and it is relatively straight-forward to setup.
In this blog post, we will walk you through setting up and configuring the OS X built-in Apache and multiple PHP versions. In the second blog post in this two-post series, we will cover MySQL, Apache
virtual hosts, APC caching, and Xdebug installation.
This post was updated 03/30/2015 adding PHP 5.6 and how to update packages and switch to specific PHP versions. Also updated 10/26/2014 to reflect OS X 10.10 Yosemite rather than Mavericks.
IMPORTANT: This guide is intended for experienced web developers. If you are a beginner developer, you will be better served using MAMP
or MAMP Pro.
Apache Setup
OS X 10.10 Yosemite comes with the Apache 2.4 pre-installed, but there is no longer a Web Sharing preference pane in the System Preferences.Not to fret! A simple Web sharing preference pane is available from clickontyler.com. This is a scaled down free version
of the more powerful VirtualHostX. VirtualHostX is a pretty UI for controlling Apache, but you don't really need
it.
Another approach is simply to use the provided
apachectlcommand from your terminal. Using the terminal is a
pretty scary prospect for many people, but there's no need to fear it.
To start up the web server, simply bring up the Terminal (/Applications/Utilities/Terminal) and type:
$ sudo apachectl start
To stop apache you would type:
$ sudo apachectl stop
To restart apache when you have made configuration changes, simply type:
$ sudo apachectl restart
So start Apache now, and then try to reach your server in a browser by pointing it at your localhost, you should see a simple header that says It works!.
If you cannot reach your site via
http://localhostyou might need to add an alias in your
/etc/hostsfile:
127.0.0.1 localhost. It should exist by default.
Document Root
The first thing we will want to do is to change the document root for Apache. This is the folder where Apache looks to serve file from. By default, the document root is configured as /Library/WebServer/Documents.
As this is a development machine, let's assume we want to change the document root to point to a folder in our own home directory. To do this, we will need to edit Apache's configuration file.
/etc/apache2/httpd.conf
You can use whatever command line editor you are comfortable with, just realize this is a
root-ownedfolder so
you will need to use
sudoto be able to edit and save the file. I prefer to use Sublime
Text 3 with the commandline command
subl, others will probably prefer
vibut
If you have never used a command line editor, try
nano:
$ sudo nano /etc/apache2/httpd.conf
Search for the term
DocumentRoot, and you should see the following line:
DocumentRoot "/Library/WebServer/Documents"
Change this to point to your user directory where
your_useris the name of your user account:
DocumentRoot "/Users/your_user/Sites"
You also need to change the
<Directory>tag reference right below the DocumentRoot line. This should also be
changed to point to your new document root also:
<Directory "/Users/your_user/Sites">
In that same
<Directory>block you will find an
AllowOverridesetting,
this should be changed as follows:
AllowOverride All
User & Group
Now we have the Apache configuration pointing to a Sitesfolder in our home directory. One problem still exists,
however. By default, apache runs as the user
_wwwand group
_www.
This will cause permission problems when trying to access files in our home directory. About a third of the way down the
httpd.conffile
there are two settings to set the
Userand
GroupApache
will run under. Change these to match your user account (replace
your_userwith your real username), with a group
of
staff:
User your_user Group staff
Save the file.
Create a Sites Folder
Of course you will now need to restart your Apache with sudo apachectl restartto pick up the changes you just
made to the configuration file.
Now, you need to create a
Sitesfolder in the root of your home directory. You can do this in your terminal,
or in Finder. In this new
Sitesfolder create a simple
index.htmland
put some dummy content in it like:
<h1>My User Web Root</h1>.
$ mkdir ~/Sites $ echo "<h1>My User Web Root</h1>" > ~/Sites/index.html
Pointing your browser to
http://localhostshould display your new message. If you have that working, we can move
on!
Homebrew
This process relies heavily on the OS X package manager called Homebrew. Using the brewcommand
you can easily add powerful functionality to your mac.
Xcode 6.1 and the Command Line Tools are required so make sure you install Xcode
via the Mac App Store first. Launch Xcode and agree to the Terms and Conditions, then install the Command Line Tools with this command:
xcode-select --install
Installation
The full installation documentation can be found on the Homebrew Wiki, but we will cover the essentials.Install the homebrew with the following command in your terminal:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"[/code]
Just follow the terminal prompts and enter your password where required. This may take a few minutes, but when complete, a quick way to ensure you have installedbrewcorrectly,
simply type:$ brew --versionYou should probably also run the following command to ensure everything is configured correctly:
Verification$ brew doctor
It will instruct you if you need to correct anything. If you already have brew installed, make sure you have the all the latest available brews:$ brew update
Now you are ready to brew!Now we have HomeBrew installed it is a simple procedure to install PHP. We will proceed by installing both PHP 5.4, PHP 5.5, and PHP 5.6 and using
Install PHP 5.4, 5.5, 5.6
a simple script to switch between them as we need. First though we have totapinto the PHP formulas and run
the following commands.
You can get a full list of available options to include by typingbrew info php54, for example. In this example
we are just including MySQL support (required for Joomla, WordPress, etc.)$ brew tap homebrew/dupes $ brew tap homebrew/versions $ brew tap homebrew/homebrew-php $ brew install php54 $ brew install php55 $ brew install php56
This may take some time as your computer is actually compiling Apache.
NOTE: If you get an error during compilation:configure: error: Cannot find OpenSSL's <evp.h>you need to reinstall
your Xcode command line tools with:xcode-select --install
Also, you may have the need to tweak configuration settings of PHP to your needs. A common thing to change is the memory setting, or thedate.timezoneconfiguration.
Thephp.inifiles for each version of PHP are located in the following directories:/usr/local/etc/php/5.4/php.ini /usr/local/etc/php/5.5/php.ini /usr/local/etc/php/5.6/php.iniYou have successfully installed your PHP versions, but we need to tell Apache to use them. You will again need to edit the
Apache Setup/etc/apache2/httpd.conffile
and search for#LoadModule php5_module. You will notice that this is line is commented out. We can ignore it
because this is pointing to the version of PHP that came with OS X. Below the otherLoadModulelines, add this:# Brew PHP LoadModule LoadModule php5_module /usr/local/opt/php54/libexec/apache2/libphp5.so
This will tell Apache to use PHP 5.4 to handle PHP requests. We will add the ability to switch PHP versions later.
You should also take this time to uncomment themod_rewrite.somodule definition so that it will be available:LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Let's restart Apache again, now that we have installed PHP:$ sudo apachectl restartThe best way to test if PHP is installed and running as expected is to make use of phpinfo(). This is not
Validating PHP Installation
something you want to leave on a production machine, but it's invaluable in a development environment.
Simply create a file calledinfo.phpin yourSites/folder
you created earlier. In that file, just enter the line:<?php phpinfo();
Point your browser tohttp://localhost/info.phpand you should see a shiny PHP information page:
If you see a similar phpinfo result, congratulations! You now have Apache and PHP running successfully.We hard-coded Apache to use PHP 5.4, but we really want to be able to switch between versions. Luckily, some industrious individual has already done the hard work for us and written a very handy little PHP
PHP Switcher Script
switcher script.
It is useful to have a your own localbinfolder in your home directory. We will create one and setup thesphpscript:$ mkdir -p ~/bin/ $ curl -L https://raw.githubusercontent.com/conradkleinespel/sphp-osx/master/sphp > ~/bin/sphp $ chmod +x ~/bin/sphpNow we want to ensure this script is easily found when we are in the terminal, so we need add our
Update Your Path/Users/your_user/bindirectory
to our path, as well as putting HomeBrew's preferred/usr/local/binand/usr/local/sbinbefore
OS X's default/usr/binfolder. Depending on your shell your using, you may need to add this line to~/.profile,~/.bash_profile,
or~/.zshrc.
We will assume you are using the default bash shell, so add this line to a your.profile(create it if it doesn't
exist) file at the root of your user directory:export PATH=/usr/local/bin:/usr/local/sbin:$PATH:/Users/your_user/bin
Replace your_user with your actual user account. E.g./Users/joeschmo/bin
At this point, I strongly recommend closing ALL your terminal tabs and windows. This will mean opening a new terminal to continue with the next step. This is strongly recommended because some really strange path issues can
arise with existing terminals (trust me, I have seen it!).
You can double-check and ensure your path has been updated to put the/usr/localpaths first with the following
command:$ echo $PATHAlthough we configured Apache to use PHP earlier, we now need to change this again to point the PHP module to a location used by the PHP switcher script. You will again need to edit the
Apache Setup - Take 2/etc/apache2/httpd.conffile
and search for theBrew PHPtext that you added earlier. Change theLoadModuleline,
to this:# Brew PHP LoadModule LoadModule php5_module /usr/local/lib/libphp5.so
IMPORTANT: Switch to your PHP 5.4 version with thesphpswitch command :$ sphp 54PHP version 54 found Unlinking old binaries... Linking new binaries... Linking /usr/local/Cellar/php54/5.4.32... 37 symlinks created Linking new modphp addon... Updating version file... Restarting Apache... Done.
Let's restart Apache again now we have installed linked the PHP load module:$ sudo apachectl restartAfter you have completed these steps, you should be able to switch your php version by using the command:
Testing the PHP Switching$ sphp 55
You will probably have to enter your administrator password, and it should give you some feedback:PHP version 55 found Unlinking old binaries... Linking new binaries... Linking /usr/local/Cellar/php55/5.5.18... 38 symlinks created Linking new modphp addon... Updating version file... Restarting Apache... Done.
Test to see if your Apache is now running PHP 5.5 by again pointing your browser tohttp://localhost/info.php.
With a little luck, you should see something like this:Brew makes it super easy to update PHP and the other packages you install. The first step is to update Brew so that it gets a list of available updates:
Updating PHP and other Brew Packages$ brew update
This will spit out a list of available updates, and any deleted formulas. To upgrade the packages simply type:$ brew update
You will need to switch to each of your installed PHP versions and run update again to get updates for each PHP version and ensure you are running the version of PHP you intend.Due to the way our PHP linking is set up, only one version of PHP is linked at a time, only the current activeversion of PHP will be updated to the latest version. You can see the current
Activating Specific/Latest PHP Versions
active version by typing:$ php -v
And you can see the specific versions of PHP available by typing:$ brew info php55 brew info php55 php55: stable 5.5.23 (bottled), HEAD https://php.net /usr/local/Cellar/php55/5.5.18 (493 files, 40M) Built from source /usr/local/Cellar/php55/5.5.19 (493 files, 40M) Built from source /usr/local/Cellar/php55/5.5.20 (493 files, 40M) Built from source /usr/local/Cellar/php55/5.5.21 (493 files, 40M) Built from source /usr/local/Cellar/php55/5.5.22 (494 files, 40M) Built from source /usr/local/Cellar/php55/5.5.23 (497 files, 50M) * Poured from bottle ...
You will see all the versions of PHP 5.5 brew has available, with the active one have an asterisk (*)after it and then you can switch to a specific version by typing:$ brew switch php55 5.5.21
OK, that wraps up Part 1 of this 2 part series You now have a fully functional Apache 2.4 installation with a quick-and-easy way to toggle between PHP 5.4, 5.5, and 5.6. Check
out Part 2 to find out how to setup your environment with MySQL, Virtual Hosts, APC caching, and Xdebug.
相关文章推荐
- MAC 升级到10.10(OS X Yosemite)下apache+php的配置问题
- MAC 升级到10.10(OS X Yosemite)下apache+php的配置问题
- MAC 升级到10.10(OS X Yosemite)下apache+php的配置问题
- Mac OS X Yosemite 10.10 配置 Apache+PHP 教程注意事项
- Mac OS X 10.10 Yosemite下配置 apache+php
- MAC 升级到10.10(OS X Yosemite)下apache+php的配置问题
- Mac OS X Yosemite 10.10 配置 Apache+PHP 教程注意事项
- MAC 升级到10.10(OS X Yosemite)下apache+php的配置问题
- Mac OS X Yosemite 10.10 配置 Apache+PHP 教程注意事项
- Mac OS X Yosemite 10.10 配置 Apache+PHP 教程注意事项
- Get Apache, MySQL, PHP and phpMyAdmin working on OSX 10.10 Yosemite
- PHPFarm - How to run multiple versions of PHP on the same computer
- OS X Yosemite 10.10. 自带PHP环境gd库扩展缺少freetype 解决办法
- 安装ubuntu10.10后,如何配置一个Apache+MySQL+PHP环境
- Setup Ruby On Rails on Mac OS X 10.10 Yosemite
- Versions 崩溃(Mac升级OS X Yonsemite 10.10)
- Ubuntu10.10安装Drupal7及其环境(apache,mysql,php)
- os x 10.10 Yosemite ON vmware workstation 11
- 苹果发布 OS X 10.10 Yosemite 的 Darwin 开源代码
- Ubuntu10.10安装Drupal7及其环境(apache,mysql,php)