OS X 10.10 Yosemite Apache Setup: Multiple PHP Versions

来源:互联网 发布:北京生活垃圾数据统计 编辑:程序博客网 时间:2024/05/04 21:39

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 ApachePHP 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 MySQLApache virtual hostsAPC 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 apachectl command 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://localhost you might need to add an alias in your /etc/hosts file: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-owned folder so you will need to use sudo to be able to edit and save the file. I prefer to use Sublime Text 3 with the commandline command subl, others will probably prefer vi but If you have never used a command line editor, try nano:

$ sudo nano /etc/apache2/httpd.conf

Nano Editor

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_user is 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 AllowOverride setting, this should be changed as follows:

AllowOverride All

User & Group

Now we have the Apache configuration pointing to a Sites folder in our home directory. One problem still exists, however. By default, apache runs as the user _www and 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.conf file there are two settings to set the User and Group Apache will run under. Change these to match your user account (replaceyour_user with your real username), with a group of staff:

User your_userGroup staff

Save the file.

Create a Sites Folder

Of course you will now need to restart your Apache with sudo apachectl restart to pick up the changes you just made to the configuration file.

Now, you need to create a Sites folder in the root of your home directory. You can do this in your terminal, or in Finder. In this new Sites folder create a simple index.html and 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://localhost should 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 brew command 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

Xcode Installation

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)"

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 installed brew correctly, simply type:

$ brew --version

Verification

You should probably also run the following command to ensure everything is configured correctly:

$ 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!

Install PHP 5.4, 5.5, 5.6

Now we have HomeBrew installed it is a simple procedure to install PHP. We will proceed by installing both PHP 5.4PHP 5.5, and PHP 5.6 and using a simple script to switch between them as we need. First though we have totap into the PHP formulas and run the following commands.

You can get a full list of available options to include by typing brew 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 the date.timezone configuration. The php.ini files 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.ini

Apache Setup

You have successfully installed your PHP versions, but we need to tell Apache to use them. You will again need to edit the /etc/apache2/httpd.conf file 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 other LoadModule lines, add this:

# Brew PHP LoadModuleLoadModule 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 the mod_rewrite.so module 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 restart

Validating PHP Installation

The best way to test if PHP is installed and running as expected is to make use of phpinfo(). This is not something you want to leave on a production machine, but it's invaluable in a development environment.

Simply create a file called info.php in your Sites/ folder you created earlier. In that file, just enter the line:

<?php phpinfo();

Point your browser to http://localhost/info.php and you should see a shiny PHP information page:

PHPInfo()

If you see a similar phpinfo result, congratulations! You now have Apache and PHP running successfully.

PHP Switcher Script

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 switcher script.

It is useful to have a your own local bin folder in your home directory. We will create one and setup the sphpscript:

$ mkdir -p ~/bin/$ curl -L https://raw.githubusercontent.com/conradkleinespel/sphp-osx/master/sphp > ~/bin/sphp$ chmod +x ~/bin/sphp

Update Your Path

Now we want to ensure this script is easily found when we are in the terminal, so we need add our/Users/your_user/bin directory to our path, as well as putting HomeBrew's preferred /usr/local/bin and/usr/local/sbin before OS X's default /usr/bin folder. 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/local paths first with the following command:

$ echo $PATH

Apache Setup - Take 2

Although 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 /etc/apache2/httpd.conf file and search for the Brew PHP text that you added earlier. Change the LoadModule line, to this:

# Brew PHP LoadModuleLoadModule php5_module /usr/local/lib/libphp5.so

IMPORTANT: Switch to your PHP 5.4 version with the sphp switch command :

$ sphp 54
PHP version 54 foundUnlinking old binaries...Linking new binaries...Linking /usr/local/Cellar/php54/5.4.32... 37 symlinks createdLinking 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 restart

Testing the PHP Switching

After you have completed these steps, you should be able to switch your php version by using the command:

$ sphp 55

You will probably have to enter your administrator password, and it should give you some feedback:

PHP version 55 foundUnlinking old binaries...Linking new binaries...Linking /usr/local/Cellar/php55/5.5.18... 38 symlinks createdLinking 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 to http://localhost/info.php. With a little luck, you should see something like this:

PHPInfo()

Updating PHP and other Brew Packages

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:

$ 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.

Activating Specific/Latest PHP Versions

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 active version by typing:

$ php -v

And you can see the specific versions of PHP available by typing:

$ brew info php55brew info php55php55: stable 5.5.23 (bottled), HEADhttps://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 MySQLVirtual HostsAPC caching, and Xdebug.


0 0
原创粉丝点击