Apache Php Server For Mac

  1. Install Apache On Mac

Now it’s time to continue with setting up Apache and PHP. If you’re coming here through search engine then you probably want to check our previous posts on [intlink id=”how-to-create-a-wordpress-development-environment-on-mac-os-x-snow-leopard” type=”post”]how to install and configure WordPress on Mac OS X Snow Leopard[/intlink].

Fortunately Snow Leopard comes with the latest PHP version (version 5.3.0 at the time of this post) so that’s covered. Please note that PHP is not by default turned on in Snow Leopard. To make sure that you’re configuring everything correctly it’s best to make Web Sharing active (System Preferences » Sharing » Web Sharing). Once that’s done we’re free to go ahead and configure setup PHP and configure Apache.

Installing Apache and PHP on macOS Catalina 10.15. (Spotlight Search) and bring up the terminal. Start as root sudo su - 1) The Apache HTTP Server As Apache 2.4 comes pre-packaged in macOS Catalina 10.15, check the version to verify httpd -v. You can place this newly created Sites directory in the Favorites section of your Mac's Finder. Homebrew: Homebrew is a popular package manager for the Mac operating systems. It is useful for installing most open source software like Node. Homebrew installation tutorial; Step 1 – Install Apache on macOS. Remove built-in Apache server (if any) from your system. Open a terminal and execute commands to stop running Apache server and remove it.

Lets setup PHP first

In order to do so copy php.ini.default to php.ini cp /private/etc/php.ini.default /private/etc/php.ini. This way we’ll keep the original php.ini.default just in the right place for any future reference. If you know what you’re doing and need any special stuff then go ahead and modify the php.ini as you feel fit.

Configure Apache web server

If you have the default Apache installation (and I assume you do) then you find your httpd.conf under /private/etc/apache2/httpd.conf. I expect that you have the technical ability of a software developer familiar with Linux commands. Now edit the httpd.conf file using root access with the sudo command and your favorite text editor. I personally prefer vim if I need to get something done on Terminal but you can also use TextMate or any other text editor.

Enable PHP5 module

Install apache on mac

Search for the PHP module (/php5. Then it would be good idea to remove the comment from the following line in order to enable PHP to run with Apache: # LoadModule php5_module. The line should look like that after you’re done: LoadModule php5_module.

Enable mod_rewrite for permalinks

If you like beautiful URLs (permalinks) then make sure that you have mod_rewrite Apache module enabled. It should look something like that in your httpd.conf: LoadModule rewrite_module libexec/apache2/mod_rewrite.so

Change the user Apache runs as

I prefer not to use Mac OS X Snow Leopards default location for sites (the Sites folder in your Home folder). On my case I have a folder called “Development” in my Home folder that has all the development projects organized in there. So in this case it’s also good idea to run Apache under your own user in development environment (which your Snow Leopard installation most probably is). In your Apache configuration you’ll find that both user and group that Apache is run with is _www.

Find and change

to (replace Martin with your own users name)

Final check for virtual hosts

Make sure that you have these two lines at the end of your httpd.conf. Otherwise you’re most probably going to find out soon that your virtual hosts simply doesn’t work.

Php
NameVirtualHost *:80
Include /private/etc/apache2/other/*.conf

Restart Apache

Now lets start (sudo apachectl start) or restart (sudo apachectl restart) the Apache web server and test if PHP is running by creating a file in your Web directory containing a call to the function phpinfo().

It works!

Final push – setup virtual hosts for your sites

At the time of writing this post I have 3 WordPress themes in development plus quite a few that I’m still maintaining. This kind of setup could easily become a mess and I really wouldn’t like that, now would I. Fortunately there’s a fix: create a virtual host for each WordPress installation.

Custom hostnames for your virtual server

I’m using a simple convention for my WordPress projects – projectname.wordpress.dev. Fortunately this keeps all WordPress installations in one clean convention which is also easy to deploy to production server (we’ll get to that later on).

Apache Php Server For Mac

To create your hostname just open up the hosts file with your text editor:

Write something like that at the end of the hosts file.

127.0.0.1 perfectline.wordpress.dev
127.0.0.1 www.perfectline.wordpress.dev

Done. You now have a hostname for your virtual host named perfectline.wordpress.dev which you can also access by www.perfectline.wordpress.dev if you like.

Configure a virtual host

Now that we are sure that we have PHP turned on, Apache working properly and even hostnames in place it’s time to create the actual virtual host that will be serving content for your WordPress installation.

First you need to create the necessary folders. In my case I’ve created Development folder where all my projects live plus one level to specify the type of the project (WordPress, Ruby, etc) and an actual WordPress project name:

mkdir ~/Development
mkdir ~/Development/Wordpress
mkdir ~/Development/Wordpress/default

Create a new configuration file for your WordPress related virtual hosts:

Apache
sudo vim /private/etc/apache2/other/wordpress.conf

Paste these lines into the newly created wordpress.conf. You need to change the file paths as you’ve set up the folders on your own system.

<virtualhost *:80>
ServerName wordpress.dev
ServerAlias www.wordpress.dev
DocumentRoot'/Volumes/Macintosh HD/Users/Martin/Development/Wordpress'
<directory'/Volumes/Macintosh HD/Users/Martin/Development/Wordpress'>
OptionsIndexesFollowSymLinks
AllowOverrideAll
Orderallow,deny
Allow from all
</directory>
</virtualhost>
<virtualhost *:80>
ServerName default.wordpress.dev
ServerAlias www.default.wordpress.dev
DocumentRoot'/Volumes/Macintosh HD/Users/Martin/Development/Wordpress/default'
<directory'/Volumes/Macintosh HD/Users/Martin/Development/Wordpress/default'>
OptionsIndexesFollowSymLinks
AllowOverrideAll
Orderallow,deny
Allow from all
</directory>
</virtualhost>

Install Apache On Mac

Now it’s time to restart Apache for one more time and we are set. Enjoy your newly installed WordPress development platform.