Categories
Apache MAMP PHP Tutorials

How to install Apache and PHP on a Mac (MAMP Part 1)

This tutorial will show you how to set up an Apache server to run PHP on a mac OSX 10.8+ with Mountain Lion or later operating system. Tested on OS X Mountain Lion, OS X Mavericks, OS X Yosemite 10.10+, macOS El Capitan, macOS Catalina

Last updated Aug 7, 2020

Updated for macOS Catalina

What you need to know since the release of Mountain Lion +

  1. Apple changed its hosting environment for OSX 10.8 +
  2. Apple dropped the GUI option to turn on web sharing, but it’s still possible to do manually.
  3. If you upgraded from a previous version of OSX your hosting environment will be broken.
  4. Apache is still installed on your Mac by default

What you need to know since the release of Yosemite

  1. Apple changed the way global permissions are given to users/ directory

What you need to know since the release of Catalina

  1. Apple locked files on the root directory, but it’s still possible to set up
  2. If you upgraded from a previous version of OSX your hosting environment will be broken. But you can get it back.
  3. Any files previously stored in the root directory are moved to /Users/Shared/Relocated Items/Security/

What green means: any lines that you need to replace in this tutorial will be in green.


Step 1

How to start your Apache web server:

Apache comes preinstalled on Apple OS X 10.8+ (Mountain Lion, Mavericks, Yosemite, Etc)

Open your Terminal (found in your Utilities folder which is inside your Applications folder)

To start the Apache server type the following command in your Terminal window

sudo apachectl start

To stop the Apache server type

sudo apachectl stop

To restart the Apache server type

sudo apachectl restart

As was pointed out in the comments by Vince and others, for people new to this command the last letter in apachectl is the letter l and not the number 1.

Next press ENTER and Apache will start. If you open your internet browser and navigation to http://localhost. You will see the following. Congratulations you now have a running Apache server.


To find the Apache version

httpd -v

If this page isn’t loading, type the following command into your Terminal to see if there is a configuration error. Fix any issues you see.

apachectl configtest

Step 2

How to Configure Apache to enable PHP:

You will need to edit your Apache Httpd configuration file to enable PHP.

Inside your Terminal window type the following command to open the httpd.conf file in the editor nano

sudo nano /etc/apache2/httpd.conf

terminal nano editor

Uncomment the php line

Now that the file is open, find the line where LoadModule php5_module libexec/apache2/libphp5.so is commented out. Remove the # in the front of the line commenting it out. This will enable PHP.

terminal screen shot

Important* macOS Catalina comes with PHP 7 preinstalled. So if you’re using that then you will want to uncomment this line instead.

LoadModule php7_module libexec/apache2/libphp7.so

Uncomment the userdir

While the httpd.conf file is still open search for the following two lines and remove the comment # from in front as well if it isn’t already. This will enable the ability to use the users/ configuration files.

LoadModule userdir_module libexec/apache2/mod_userdir.so

Include /private/etc/apache2/extra/httpd-userdir.conf

Save and exit the file

Hold control + x to save and exit. Type Y to confirm. Then press enter.


Step 3

How to set up a user configuration file:

1. Check your Apache version. You will need this in a couple minutes.

Type the following in Terminal. It will output your Apache version.

httpd -v

Write your Server version down for future reference.

2. Setting up a user configuration file:

To host files in your Sites/ directory you will need to create a user file for your osx user (use the user short name, if unsure of what it is go /Users directory and you will see the short names of all the users)

Create the file by typing the following in your Terminal. Using nano will open the file if it already exists or create it if it doesn’t. Replace [username] with your username

sudo nano "/etc/apache2/users/username.conf"

Add the following lines to the file. *Important: the directory path in the first line is case sensitive.

If you are running Apache 2.4.1 and earlier use this:

<Directory "/Users/username/Sites" >
    Options FollowSymLinks Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

If you are running Apache 2.4.2 + use this:

<Directory "/Users/username/Sites" >
    Options FollowSymLinks Indexes MultiViews
    AllowOverride All
    Require local
</Directory>

Hold control + x to save and exit. Type Y to confirm. Then press enter.

Set the permission of the user configuration file by typing the following in your Terminal

sudo chmod 644 "/etc/apache2/users/username.conf"

How to make sure user configuration files are enabled:

Open the following file in Terminal

sudo nano /etc/apache2/extra/httpd-userdir.conf

Find the next line and make sure the comment is removed #

Include /private/etc/apache2/users/*.conf

Save and exit the file.


Step 4

How to enable and configure Htaccess:

.htaccess is not enabled by default. Many php applications rely on htaccess for URL routing.

Again open the httpd.conf file in your terminal.

sudo nano /etc/apache2/httpd.conf

Now press control+w to search in the Nano editor. Search for the word “override”. On all instances, change AllowOverride None to AllowOverride All. Save the file.

In your terminal type the following and press enter to restart your Apache server.

sudo apachectl restart

Step 5

Test it out:

Create a test file in your Sites folder called phpinfo.php

1. Check that your Sites folder exists in your Users/username/ directory. If not then create it.

Create a Sites directory in Users

2. Change the permission of your Sites folder to allow Apache to access your files and folders.

Enter the following command into Terminal. Be sure to replace username with your actual username. And press enter.

sudo chmod 0755 "/Users/username/Sites"

3. Next you will need to create a phpinfo.php file

sudo nano "/Users/username/Sites/phpinfo.php"

In your file add the following.

<?php phpinfo(); ?>

Save the file and exit the editor. If you get the following error “Error writing … No such file or directory” check that you did the previous step correct. Sites directory needs its permission set to 0755.

4. Open your internet browser and type http://localhost/~username/phpinfo.php

You should now see the phpinfo file load.

It will look something like this.

phpInfo(); screenshot

5. (Optional) Update the permission of the phpinfo.php file

If you got a permission error or a page not found error when opening the page in your URL you will need to update the permission of the Sites directory. To do this.

sudo chown -R :_www "/Users/username/Sites"
sudo chmod -R g+w "/Users/username/Sites"

Restart Apache

sudo apachectl restart

Open your internet browser and repeat step 3

Congratulations! You have set up your PHP Apache server on your mac.


Part 2: How to setup MySQL on a Mac with OSX 10.8 + (MAMP Part 2)

Part 3: How to setup phpMyAdmin on a Mac with OSX 10.8 + (MAMP Part 3)

Part 4: How to enable and setup php.ini on a Mac (MAMP Part 4)

Part 5: How to host multiple sites on your Mac (MAMP Part 5)

By Jonathan Whiting

I enjoy sharing what I am learning and hopefully it's of interest and help to you. I live in Canada with my wife. Follow me on Twitter.

219 replies on “How to install Apache and PHP on a Mac (MAMP Part 1)”

wording details: instead of
“Find the next line and make sure the comment is removed #”
replace with:
“Find the next line and make sure the comment (#) is removed”

Great work.
First part completed, but i have answers for you!I can use the same guide for xampp ? or you advise me to use MAMP on mac ? thanks a lot 😉

Hi Paola, It’s going to depend on your needs. If you need to work on PHP I generally recommend a MAMP setup.

Hey Jonathan,

I have followed each and every step but the testing isnt getting through. Also when i even do http://localhost/~pulkitkumar95, it says “Cannot connect to localhost”, i am running mac os el Capitan and apache server version is 2.4.16. I am in urgent need of this to work.plesase help me out asap

Hi Pulkit, also, in step 3 part 2, make sure you setup your configuration file correctly for your version of Apache. Then Restart your server. Best of luck

Hi.
I have EL CAPITAN
At the end I get the message: “SAFARI CAN’T CONNECT TO THE SERVER Safari can’t open the page “http://localhost/~MarcelloMacMini/phpinfo.php” because Safari can’t connect to the server “localhost”

but if I type just “localhost” I get the IT WORKS! page so localhost by itself is working.. only not the php page…

I’ve followed everything, the permissions are correct, the php file is there

Hi Marcello, a permission error should be easy to resolve by updating the permissions on your Sites directory. See Step 5. Part 5 in the tutorial. It shows you how to update them. Let me know how it goes.

Hello, Jonathan –

After following all the instructions several times in your – most excellent – tutorial, I’ve consistently received the following error message in Chrome:

This webpage is not available

ERR_CONNECTION_REFUSED

Google Chrome’s connection attempt to localhost was rejected. The website may be down, or your network may not be properly configured.

Your help in solving this issue will be greatly appreciated!

Hi Kym,

It’s tough to solve with just that error alone. Do you get this same error when using Safari? Also, what OS do you have installed? This will help to diagnose the issue. Thanks.

HELP??? I have now gone through at least 4 tutorials on getting Apache working on Mac (OSX 10.8.5). Every single one starts with the command sudo apachectl start in terminal. I get a response of command not found. I’ve made the change to enable PHP, and I’ve set up a user config file, but the base command simply doesn’t work. Any ideas?

Hi Kristen, try running this command to check the config syntax. “apachectl configtest”

Thank for the tutorial.
I keep getting (after completing all the steps) a 404 error. How can I overcome
this?

Hi Pavan, sorry for the late response. 404 error means that the page wasn’t found. Sometimes that means that Apache doesn’t have permission to access the location of the site. Have you tried giving the Apache user permission to your Sites folder? I’ve left instructions after part 5 of the tutorial.

Run the following in your terminal. Remember to replace username with your username.

sudo chown -R :_www "/Users/username/Sites"

sudo chmod -R g+w "/Users/username/Sites"

sudo apachectl restart

Hello Jonathan,

Thanks for the instructions. I am running El Capitan beta. I am getting a 404 error: The requested URL /Users/~quint/phpinfo.php was not found on this server.

Could you provide any troubleshooting tips?

Hi Quint, I’m about to test on El Capitan myself. Downloading it as I type this. What does your Apache error log say?

Thank for the tutorial.
I keep getting (after completing all the steps) a 403 error. How can I troubleshoot this?

Hi Marco,
This is error means that the directory is forbidden. You can fix this in your user configuration file. Double check that you are using the right one. You can see the options under Step 3 in the tutorial. Can you let me know what version of Apache you have? Type this into your terminal to find out httpd -v

I am trying to set up a testing server on a Mac running OSX 10.8.3
I want to set up virtual hosting. When I get to the final step, I don’t see the correct PHPInfo file. I am getting the file, but just the line of code. The PHP code isn’t executing. I have it at the top level of the Sites directory. Inside are other directories. I can go into a site and see the files there, but just as a list. What am I doing wrong? Thanks very much.

Hi Carol,

This is possible if PHP is not enabled. First thing I would check is that the following line doesn’t have a # comment in front of it. See Step 2 in the tutorial.

Open the Httpd config file in nano

sudo nano /etc/apache2/httpd.conf

Then look for this line

LoadModule php5_module libexec/apache2/libphp5.so

If that is correct, then try restarting the Apache server. Once restarted it should work fine.

sudo apachectl restart

Hi Jonathan, Thanks for this easy tutorial.
I’m trying to make it work for a WordPress program.
I do however, have a little problem when:
Username-MacBook-Pro:~ username$ sudo chmod /Users/username/Sites 0755
it says:
chmod: Invalid file mode: /Users/username/Sites

What happened? I can’t find a understandable answer on the net.
Please help 🙂
Thanks,

Sydney

Hi Sydney,

Sorry about that. The command is:

sudo chmod 0755 /Users/username/Sites

The permission 0755 needs to be written before the file path in the command. I’ve updated the tutorial.

Hey,
I have a question about permissions to delete or edit files using PHP. I would like to unlink files and add files to folders. Using PHP Unlink(path) will not work. Any suggestions?

Hi Matthew,

Correct, PHP will need permission to delete this file using unlink. You can give php permission by changing the files permission using a php function like chmod($filepath,$permission); When using a command like chmod() make sure to add a 0 in front of the permission number. So for example if you want to set the permission to 777, use 0777 to make sure the number is correctly interpreted as an octal number. Here’s an example chmod($filepath,0777);

Thanks for the quick reply,
I am still getting permission error:

[message] => unlink(../user/123123/main.jpeg): Permission denied

This is my code:

chmod(“../user/123123/main.jpeg”, 0777);
unlink(“../user/123123/main.jpeg”);

I went step by step like you said but I am still getting “The requested URL was not found on this server.” Even tried the last two optional steps. Plz help

Hi Aditya,

I’m going to see if I can replicate the problem you are having. Will get back to you this weekend with what I learn.

Hi Aditya,

Okay, so I think I may have found the problem. It’s likely that your /Users/username/Sites folder permissions need to be changed. Run the following command in terminal to change your Sites/ permission to 0755. Be sure to change username with yours.

sudo chmod /Users/username/Sites 0755

Hey Jonathan,
great tutorial. thanks for that. However, I seem to be stuck at this point. Once I type in sudo chmod /Users/myusername/Sites 0755, I get the following error message:

chmod: Invalid file mode: /Users/myusername/Sites

Do you know what is going on here?

Cheers,
Stephan

Hi Stephan,

The error is because of the command format.

Try this:

sudo chmod 0755 "/Users/myusername/Sites"

Hi, this article is really cool.
But now I want to try to host my php files in some other directory, instead,
/Users/username/Sites

What do I have to do?

It’s easy. It requires 2 things

1. Update the virtual host file to point apache to the new location
2. Make sure that the apache user _www has permissions for your new directory.

I do this with all my projects. Instead of hosting them in my user directory. I host them the same way I would on my live server /srv/www/

Hi Jonathan,
Your tutorial is very informative and I’ve tried to follow and read it through a dozen times. I am still stuck on step 1; I never get the browser to say “It Works!”. I’ve been reading pretty much every forum on the matter and nothing seems to make it work for me. (I should also say that I’m pretty new to doing this kind of things). I also tried to install nginx to see if I had more luck with that but it didn’t work either.

When I run “sudo apachectl” in the terminal I get this:

AH00112: Warning: DocumentRoot [/usr/docs/dummy-host.example.com] does not exist
AH00112: Warning: DocumentRoot [/usr/docs/dummy-host2.example.com] does not exist
no listening sockets available, shutting down
AH00015: Unable to open logs

and when I run sudo nginx I get:

nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] still could not bind()

From this I draw the conclusion that I’ve maybe messed something really basic up and I don’t know what?
Do you think you could help me out?

// Hjalmar

Hi Hjalmar, this looks like a port error and is pretty unusual.

The errors show that both the Apache and Nginx servers are failing to bind to listening port. This likely means that a process is already using that port.

Run this command to see what is bound to port 80

sudo lsof -i :80

Jonathan – great tutorial, thanks (and no thanks to Apple for removing the very simple Web Sharing GUI route!) One suggested edit for the tutorial, which you got in one of the comments, but I think would help is that it’s not apachect1 (number 1) but apachectl (letter l) – they look so alike, I think it’s worth highlighting in the tutorial. Great work, thanks!

I know this article is a little on the old side, but I wanted to request a small correction, for the sake of performance and security. You say to change *all* instances of ‘AllowOverride None’ to ‘AllowOverride All’. This should not be done in (ie, in the root directory of your filesystem) as this allows .htaccess files *outside* of your document root. Instead, AllowOverride should only be enabled in the directory designated as DocumentRoot, or in user directories if you’ve got that enabled.

Hi Rich,

Good point. On a local server it’s useful to enable .htaccess on all sites. But on a production server you would want to specify the root directory of the site. Such as:

For those who want to dive deeper into .htaccess it’s also worth pointing out that AllowOverride All doesn’t enable overriding all apache options. Some options such as Multiview need to be set manually.

AllowOverride Options=All,MultiViews

Well i followed the steps exactly and still get the forbidden error even. 403 You don’t have permission to access /~Tom/phpinfo.php on this server. Anyone have ideas and yes I try the updated sudo commands it didn’t work

Hi Tom,

It’s probable that your /Sites folder permissions need to be changed to 0755. You can do this by running the following command in Terminal. Remember to replace username with your actual username.

sudo chmod /Users/username/Sites 0755

Thanks so much for this well written tutorial. Coming from the windows world to the Mac world and having to go through this would have been a lot harder if you hadn’t written this, so much thanks!

One thing you might want to add is the necessity to hit the enter key after the ^X and “Y” when finishing a file edit. If you leave the terminal screen before doing that (and it’s not as obvious as it should be), your edits are lost. Nice learning lesson, though. lol

Hi Jonathan!

I’ve got the problem of 404 when I try to access to my phpinfo.php page.

It can be caused by the apache “AH00558” error? How I can solve it? Thank you in advance!

Hi,
Since apache 2.4 ( Yosemite have 2.4.9 installed ) the directives :
Order allow,deny
Allow from all

Must be replaced by:
Require all granted

Hope it help

Hi Luigi,

Good tip. Also Require local works too. I will update the tutorial for Yosemite.

Hello I did a little more research and your instructions are missing the following:

In Step 2 while in the in /etc/apache2/httpd.conf

Also uncomment out the following two lines:

#LoadModule userdir_module libexec/apache2/mod_userdir.so

#Include /private/etc/apache2/extra/httpd-userdir.conf

Changing these two lines allowed all of your previous directions to work, good job on everything else, thanks!

Hi Mr. Jonathan

I’ve just updated new os ypsemite. before that php was working fine and after update apache is wokring fine but php is not working.
I have applied the step to reconfigure the php but still php is not working.
below error is coming .

Not Found

The requested URL /~username/phpinfo.php was not found on this server.

any suggestion?

Hi Ashish, so I believe I have found the source of your problem. The permission of your Sites folder needs to be changed to 0755

Run the following command to ensure Apache has permission to access it. Be sure to replace the string username with yours.

sudo chmod /Users/username/Sites 0755

Hi Major, I added instructions at the end of the tutorial to show you how to update the permissions of your Sites folder. That should solve your problem.

Thanks for the awesome tutorial. I also got the same error as Holley got. I could fix by adding ” /” after Sites in the step 3 code

Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all

And the URL as http://localhost/~geeta/phpinfo.php

The URL must have ~ before the useraname

Hi Jonathan,
First Thanks for the explanation above.
After starting the server also my localhost is refusing connection.
even then i went ahead and completed all the steps.
then also the browser is showing connection refused.
Before i could find ur website i followed the step from another website

And i just creating individual html and php pages for my website…so i thought of putting them in the sites folder created above and running them…

Hi Bhardwaj,

It seems the error likely has to do with the Apache not having permission to access the Sites folder. It’s taken some time to locate the problem.

Run the following command in your Terminal. Be sure to replace username with yours.

sudo chmod /Users/username/Sites 0755

Hi Bhardwaj,

What error are you getting? In that tutorial he sets up the Apache host settings using the httpd-vhosts.conf file. It is likely you have a mistake in the /etc/apache2/extra/httpd-vhosts.conf file. It’s probably a very simple problem to fix when you know where to look.

Hi after creating the phpinfo.php file and getting a forbidden warning in my browser it turns out that I had skipped much of step 4 which was to change the multiple instances of “allowoverride none” in the httpd.conf file. There are 3 instances that need to be changed not one.

Hi Robert,

Glad you got it working. Thanks for sharing what you found I think it will help others with the same problem.

Hello,

I have one quick question. I followed through all the step you gave, but when I typed “http://localhost/~yeaseul/phpinfo.php” in my browser, it doesnt’ work. I can see that there is a file in the path /Users/yeaseul/Sites/phpinfo.php. I am using 10.9.4. Is tihs going to be a problem?

Hi YS,

Yes you will need this to work. It may be related to the same problem Robert had. In step 4 make sure you replace all the instances of “allowoverride none” in the httpd.conf file

best of luck

Opening the file phoinfo.php using nano in your Terminal will automatically create it for you if it is not created already

Hey! Loved it all, just hit one bump I can’t get over though!
When I’m on the step of testing out the php; and enter this in the terminal:
sudo nano /Users/username/Sites/phpinfo.php
and then add

to the file I get this error when trying to save:
“Error writing /Users/~username/Sites/phpinfo.php: No such file or directory

It’s a huge pain, I proofread it several times (changed ~username to my actual username btw) and still can’t figure out what is wrong! 🙁

Hi Waleed,

When trying to open the file in nano using the Terminal remove the ~ from the path

The path should look like:
sudo nano /Users/username/Sites/phpinfo.php
not:
sudo nano /Users/~username/Sites/phpinfo.php

Let me know if that works for you

Yup! I’ve done that several times over, it keeps on saying
“Error writing /Users/waleed/Sites/phpinfo.php: No such file or directory”

🙁

Yup! I’ve done that several times over, it keeps on saying
“Error writing /Users/waleed/Sites/phpinfo.php: No such file or directory”

Hi Waleed,

Try to create the file from within the folder directly.

Using Terminal

cd /Users/waleed/Sites/
sudo nano phpinfo.php

Hej,
I am having exactly the same problem. Did you solve it?
(apart from this great instruction! Thanks a lot!)

Sorry, I misunderstand. I am not havin the same problem as Waleed. I am not having poblems on creating this file but when I try to open it in my browser I get the message:
The requested URL /username/phpinfo.php was not found on this server.
Even though it exists!

Hi Holley,

Hopefully I will be able to help you out. Are you using the ~ character at the beginning of the URL?

http://localhost/~yourusername/phpinfo.php

Also, Robert had a similar problem. In step 4 make sure you replace all the instances of “allowoverride none” in the httpd.conf file

If that still doesn’t work, check the phpinfo.php file permissions. Set them to 777 and try agian. Let me know how that goes?

Howdy Jonathan,

I am getting an error “Firefox can’t establish a connection to the server at localhost.”
I get the same error on Safari.

http://localhost
IT Works – shows up

http://localhost/~macpro/phpinfo.php
Gets the above error

macpro$ whoami
macpro

vi +217 /etc/apache2/httpd.conf
AllowOverride all

vi /etc/apache2/httpd.conf
/php
LoadModule php5_module libexec/apache2/libphp5.so

macpro$ cat /etc/apache2/users/macpro.conf

Options FollowSymLinks Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all

FollowSymlinks

macpro$ ls -l /Users/macpro/Sites
total 8
-rwxr-xr-x 1 root staff 20 Jun 30 18:11 phpinfo.php

sudo apachectl restart

I would surely appreciate any help you can give me.

Hey Jon,

It is saying:

Not Found

The requested URL /MitchelBlais was not found on this server.

I have the Sites folder in MacintoshHD/Users/MitchelBlais/Sites/phpinfo.php
and it still isn’t working any ideas?

Thanks

Hi Jonathan! I hope you can help me with this. I managed to get Apache started but when I go to http://localhost I get the Forbidden Error 403.
I followed all the other instructions without any trouble but I still can’t get the localhost to work, and consequently I can’t get the http://localhost/~username/phpinfo.php doesn’t work either. It gives me the same error.
My mac is OSX 10.8.5
Do you know what this is? Please help!

Hi Catalina,

I believe that the 403 error is caused by a permission problem. I’m not sure why you would be having that problem. Were you able to resolve it?

when I run “sudo apachectl start” it is saying sudo:apachect1 command not found. please advice. Thnaks

Hi Tejaswi,

The last character in apachectl is a the letter L even though it can easily be mistaken for the number 1

sudo apachectl start

Hey, thanks for posting this stuff. Wondering if I should follow your directions or use MAMP – just got my first Macbook (yay). Someone wrote me code (php, sql, css) for pages for my new website that I’ll be launching – registration, login/logout, profile, etc – so I want to open them, use them, make any changes so they’ll be functional for me. Thanks.

Well, downloading the MAMP.org project is a good place to start. It is easy to get setup. The problem with using it is that it is very limited with customization. If you need to install a PHP Extension for an application it can be very hard to do with their app. And in the long run it is much better to use a true local environment on your Mac.

So if you want to get started quickly, start with the MAMP app, then install your own Apache and PHP setup on your machine as you become more advanced

Thank you — had typo trouble but was able to find it and fix it thanks to your clear and easy-to-navigate instructions. Thank you.

The optimist fell ten stories.
At each window bar
He shouted to his friends:
“All right so far.”
-Unknown.

After spending a few hours trouble shooting my php I came across your blog. Step #4 fixed the problem and put a smile on my face. Thanks a mil.

Thank you for providing this information. Now that I have Apache and PHP working, I have one question:

Will these changes automatically start Apache and PHP on reboot or do you have to execute “sudo apachectl start” when you want to start it? If the latter, is there some way to execute the “apachectl start” at boot?

Thanks again.

No problem Chuck,

To make Apache automatically start when your restart your mac run the following command in Terminal

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

And this to turn it off again run this

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Hi Jonathan,

I am very new to this Apache and PHP on a Mac thing. However, I was able to follow your guide with no issue until I got to step five and got the following error: “Error writing /Users/bnax/Sites/phpinfo.php: No such file or directory”. I am using OS X Version 10.9. Any insight would be highly appreciated.

Hi JB,

Another visitor had the same problem, I’m unsure why this is not working for you.

Perhaps go to that folder directly and create the file manually using a coding text editor. I use Coda 2.

Hi Jonathan,

I’m from Malaysia. I’m new on Mac. Just migrated from Windows to Mac.
I’ve followed all the steps and I had this problem. When just using http://localhost it’s fine but when I try to open the phpinfo.php file it says “Connect Connect To The Server”. I also have restarted the machine the my apache via Terminal.

HI Azman,

First off, welcome to Mac.

This problem would point to Apache not running and needing to be started. But because it works on http://localhost that would not be the problem.

Have you tried restarting Apache?

hi jonathan.

i followed all the steps you posted. however, when i use localhost/~username/phpinfo.php i got an error ‘you don’t have permission to access /~username/phpinfo.php on this server.

what should i do?

Sorry about the late reply Joe. First step would be to check if your ~username is the correct path.

Sorry, the angle brackets weren’t shown in my previous post. Here’s what I meant to say:

The username you enter in the username.conf is case-sensitive.

For example, if your site is located under “Users/Robert/Sites”, then the first line in robert.conf should be:

<Directory “/Users/Robert/Sites”>

Thanks for your comment Robert! I’m sure it will be very helpful to someone with the same problem

Yes, it’s really easy to setup using virtual host files and your local /etc/host file. I can make a tutorial on it if you would like.

Thank you for this great tutorial. Can you please make one for virtual host file? Or direct me to where I can find it if you already made one.

Thanks.

Hi Phil,

Yes you can make better URLs by setting up virtual hosts. It’s very easy to do.

Then you can set a URL like

http://testsite.localhost/

I don’t have a tutorial for it yet, but will create one and post it for you.

Is there a way to change “localhost” or “localhost/~username” to something else? Like “local” or “dev”. I’ve tried googling it but don’t think I’m searching with the right terminology.

Yes, it is actually very easy. You just need to enable virtual hosts and then setup a pointer in your /etc/hosts file. I will create a tutorial to show you how to do this.

Hi Jonathan
I have been having the same problem for a while now, and I follow all comments and tutorials and it always ends up not working, and me having to start again. Yours has come the closest so far, but still I am unable to progress past this point.

This is my machine name and my short name assigned by the system: Petars-MacBook:~ petar j$ . My short name on the user directory is: petar j

Whenever I try and (re)configure the apache user, I have tried using all of the following short names tyoing them in as follows: “petar j” , “petar%20j , petar j$ and “petar_j” all with no success! As a result of this I am unable to configure anything any further.

Is there a fixed rule when there is a “space” assigned in the short name when I type this into the terminal, and then when I am adjusting the code?

I have also copied the apache2 error_log for your info:

“[Thu Oct 24 15:30:48 2013] [notice] caught SIGTERM, shutting down
[Thu Oct 24 15:30:48 2013] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
httpd: Could not reliably determine the server’s fully qualified domain name, using Petars-MacBook.local for ServerName
[Thu Oct 24 15:30:48 2013] [notice] Digest: generating secret for digest authentication …
[Thu Oct 24 15:30:48 2013] [notice] Digest: done
[Thu Oct 24 15:30:48 2013] [notice] Apache/2.2.24 (Unix) DAV/2 PHP/5.3.26 mod_ssl/2.2.24 OpenSSL/0.9.8y configured — resuming normal operations
[Thu Oct 24 15:34:19 2013] [error] [client fe80::1] File does not exist: /Library/WebServer/Documents/petar j
[Thu Oct 24 15:34:47 2013] [error] [client fe80::1] File does not exist: /Library/WebServer/Documents/petar_j
[Thu Oct 24 15:35:22 2013] [error] [client fe80::1] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: /Users/petar j/Sites/
[Thu Oct 24 15:42:02 2013] [error] [client fe80::1] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: /Users/petar j/Sites/”

Mac OS X 10.8.5 Macbook Pro

Looking forward to your reply.

Many thanks

Hi Petar,

It may be that because you have a space in your name.

On a mac any path with a space in it will need quotes wrapping them.

If you were running this command for example

sudo chmod 644 /etc/apache2/users/user name.conf

It would need to look like this to work

sudo chmod 644 “/etc/apache2/users/user name.conf”

Hopefully that helps

Hey Jonathan,
I´ve gone through the steps in your tutorial but I get the “Firefox can’t establish a connection to the server at localhost.” when trying to open the phpinfo.php
Everything looks ok in all the steps but this last one just doesn´t work. Phillip Friese talked about a missing folder or something, but I don´t know how to check this in terminal (new to terminal…)

What can I do to figure this out? Help is greatly appreciated.

You help a lot, thanks very much. With a little attention and search yours hints make me into the Apache and PHP for MAC.

Got to step 5 with no issues. When I attempt to open the phpinfo.php file in Safari I get the error “Safari cannot connect to the server localhost. After step 1 I got the “It Works” in Safari. I did restart teh Apache server after seting up the user.conf file.

Followed every step. I get the illustrious

403 Forbidden

You don’t have permission to access /~username/index.html on this server.

of course, I changed the “username” to my username. I haven’t tried php yet because I don’t need php. I just want to host some files on a server. Are there any extra steps I should be taking for this function?

I’m using 10.8.3

You may need to update your Site folder to give Apache permission to access it. I’ve added a section at the end of Part 1 to show you how to do that.

Hi Jonathan

I have now spent a 2 x day’s trying to get this set-up to work and now officially don’t know anymore. I am pretty new to this, so please bear with me if I do not use the correct terminology.
I am trying to set-up my machine to host a website I propose building. I started by installing the complete MAMP package. Doing it this way, I was unable to get the Apache Server to run. I unintalled the package and found your tutorials on-line and I have subsequently followed your tutorial to the letter. I have checked and double checked each and everyone of your suggested settings, but all I get when I type in http://localhost I currently get ‘Failed to open page” Safari can’t open the page…..”localhost” Yesterday I got a “Error 404 message” when trying the same thing, so I am not sure if this progress or not? My mac short name is “petar j” I have put the short name in ” ” I have a Sites Folder, I have created and see the “phpinfo.php” file. I do also have existing iWeb Folders in my configure Sites Folder, not sure what other information you may require to assist me further?

What am I doing wrong, or what do I need to change or what other settings do I need to do?

Many thanks in advance?

Hi Petar,

It’s difficult to diagnose your problem without seeing your setup. Are you still having this problem?

Hi Bhargav,

Hopefully I can help. Have you tried restarting Apache and then entering the URL in your browser again?

This all works on my machine but how can I access the page from another computer on the same network?

Just wanted to say thanks for the tutorial, works great! I tried three other tutorials before yours and none of them worked for me….so thank you!

Hello,

thank you for the guide! But I have a problem: even the very first step does not work. After starting apache with “apachectl start” and typing in both “http://localhost” and “localhost” Safari says it is unable to connect to the server. Am I doing something wrong?

I am sorry for double-posting, but I found some things out. First, I am indeed using an upgraded Mac OS X, I have been using it since Snow Leopard.

bash -x /usr/sbin/apachectl -k start gave me an interesting output:
“+ /usr/sbin/httpd/ -k start
(2)No such file or directory: https: could not open error log file /private/var/log/apache2/error_log”

And indeed, /usr/sbin/httpd/ is completely missing. How may I fix this major problem? And will there be some other important things missing?

I am using Mac OS X 10.8.4, upgraded from Snow Leopard over Lion. But I fixed the problem by myself! I just created a the missing …/log/apache2 folder and chmod-ed it and now it works without a problem! Your How-To “php” just worked fine, thank you!

Hey there,

First off thank you for the great tutorial. I am a bit lost though.

I have walked through all the steps here. When I go to “localhost” I get back the message that “It Works” ok so we’re good there. In the users directory I have the folder named “Sites” in there is the “phpinfo.php”. Ok great so those commands worked as well.

Yet when I try the final step to test “http://localhost/~TenfoldSystems_MBP/phpinfo.php” I am getting that the page failed to open because it cannot connect to the server. I tried running the restart command and same thing applies. Could there be an issue since my short name is “TenfoldSystems_MBP” ?

Hi James, I don’t think it is the short name. It sounds like you did that correctly. I am going to try to recreate the problem on my server to see if I can duplicate the problem.

Hi James, to confirm. Does the error you are getting say, “You don’t have permission to access /~TenfoldSystems_MBP//phpinfo.php on this server.” ?

Actually. I restarted: sudo apachectl restart and restarted the computer. Then started: sudo apachectl start which was successful and let the server be found to load the php page created in the sites folder.

Got through the rest of the steps successfully. Have MySQL and phpMyAdmin installed. Now just trying to figure out how to implement the MySQL for my website. (Just getting into web design so that’s why I don’t know.) If you create something local server through the phpMyAdmin then does it provide the code for website use? Or is this only really for local testing? Already have a dedicated server and website up.

Nice. Yes having the MySQL installed on your localhost is best for local testing and development. You will just need to install MySQL on your dedicated server. What OS are you running on it?

Server Settings
Apache version 2.2.22
PHP version 5.2.17
MySQL version 5.5.31-cll
Architecture x86_64
Operating system linux

The cpanel does not have any controls like my old godaddy did Foy MySQL.

Computer osx 10.8.4
Using dreamweaver

Hi Tom. This is because the php module is not enabled in the httpd.conf file. Check to make sure the line of code LoadModule php5_module libexec/apache2/libphp5.so in the file /etc/apache2/httpd.conf is not commented out. You can see instructions in Step 2

Thanks for answering my question, Mr. Whiting. As embarrassing as it is to have to admit, in the short time between my question and your response everything that I had gotten to work following your steps has stopped working all together. I’m pretty sure my brain exploded. Parts of it that were clicking just a few weeks ago when all this was making sense are not there. Someday, though…

Please I was confortable untill step 5.
I got this message on firefox:
The connection was reset.The connection to the server was reset while the page was loading.
I got osx 10.8.2.
I am still studying php now and for days I cannot get enable.Why?
However I search google but no solutions.Please help.

Thank you Bro, this is a great tutorial but i got stuck here on this part:

how to ad this directory on terminal after the password? Could you give me specifics details if possible with IMAGES? thanks

Options FollowSymLinks Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all

Hi,

Thanks for this guide. So I’m having a hard time at Step 3. Apache2 works since I can type in http://localhost and it says “It Works” but I’m having a problem setting up the configuration file.

My mac’s shortname is odd. It’s “Alex (Main”). This was the default. I’m not a terminal wizard, so I’m not sure how to enter this.

Could you help me set up my configuration file the right way? My end goal is to be able to load Dokuwiki.

Hi Alex
Try this. Put the path in quotes when you type it in a file or on the command line. When a path has spaces it needs to be either wrapped in quotes. ie. “path with spaces” or escaping the spaces in the path ie. path\ with\ spaces

Hey Jonathan,

Thanks for that tip. I wrapped in quotes, and I got a .conf file ‘Alex (Main)’.conf

I proceeded through all the steps, but when I go to my browser and enter http://localhost/~Alex%20(Main)/phpinfo.php I get a response saying ‘Forbidden

You don’t have permission to access /~Alex (Main)/phpinfo.php on this server.’

I’m at a loss. Any words of wisdom?

That sounds like the file permissions need to be set to allow visitors to read.

Try this. sudo chmod 644 your conf file and also sudo chmod 755 your phpinfo.php file

Check to make sure you restarted your apache server correctly. In your terminal run sudo apachectl restart. Apache won’t recognize the new .conf file until after it is restarted.

Hello thx for sharing the knowledge. I’m using the mac 10.8.3 version and i found that has no websharing function s in this version. And i follow ur step until step5 after finish “http://localhost/~denni/phpinfo.php” i test in Chrome shows 404 Not Found. Its that only work in Safari ?

Hi Mary,
I’m trying to recreate the problem. I have two quick questions for you.
1. After setting up the username.conf file did you restart the apache server? sudo apachectl restart
2. If you did restart the server, is the path in the conf file directory set correctly? /Users/username/Sites” > (it’s case sensitive)

Let me know, and we can keep figuring out the problem from there.

Leave a Reply

Your email address will not be published.