MacMost Now 80: Running a Web Server and PHP On Your Mac

Gary Rosenzweig looks at how you can use your Mac to develop Web pages using the built-in Apache Web server and PHP.
Video Transcript / Captions
Closed captioning for this video is available on YouTube: MacMost Now 80: Running a Web Server and PHP On Your Mac.

Hi this is Gary with another tasteful episode of MacMost Now. Did you know that your Mac comes complete with a web server? Built right into the OS is the Apache web server. All you need to do is turn it on, and then you can use it to do things like test out webpages. You can even enable PHP. Let me show you how.
So there are a lot of different web servers out there but Apache is one of the most popular. It's the one usually used with a linux machine. Now, Apache's running in Mac OS10 and all you need to do is turn it on. Once you turn it on, you can access your local website- the one that's running on your computer by just going to localhost. Localhost like that. No .com, nothing else. Just hit return.
Now, you'll see that in this machine it's not enabled so let's enable it. Go to system preferences, and under the sharing tab, see right here, we want to enable web sharing. We turn that on, it takes a second. And it'll actually give you a link to the local IP address of your machine. But we can again just go ahead and use localhost.
Now we can go and see that the Apache web server is going. Every account has its own website, so you want to use a tilde(~), which is usually found using a Shift all the way at the upper left hand corner of your keyboard. And type in the username for the account you want to access. You'll see a webpage, it looks like this in leopard. Just basically a placeholder.
Now, where exactly is this website? Well if we go ahead and open a finder window we can go in and see that it's in your documents folder. So we go here's MacMost-- sorry not your documents folder, but your sites folder. You can see it with all the other things there in your home directory. Go into sites, and there is the index.html that actually is this page right here. So what we want to do is we want to customize this to be our own page. So one of the ways we could do that is to basically create our own index.html which automatically loads as the homepage.
So we're going to go ahead into text edit and we're going to create a new text document and just type something simple. It's not exactly html but it'll do for a quick test. And we're going to save it in here. And we're going to save it as test.html. We're going to go ahead and say yes use the .html extension. We're just using text edit for this. And then we go ahead and instead of index.html we go to test.html. Here- and there is the webpage you just created.
Now to take this to the next level we're going to go ahead and get PHP working. PHP is a programming language used to build lots of websites. If you're a PHP programmer, it's very useful to have PHP running in the Apache web server on your Mac- so you can actually build PHP pages right there on your Mac before uploading to a website. So what we're going to do is we're going to go ahead and create another document using text edit. So go ahead and create something that uses PHP.
We'll do a simple little command that will tell us whether or not PHP is running. Just add 5 + 1. We're going to call this test.PHP. Now when we go to the web browser and look at this page we actually see the code. It treats it like a text document, not like a PHP document. We need to enable PHP for it to treat it like a PHP document. The way we're going to do it is pretty simple. We're going to open the terminal window. Now once we're in the terminal window, we're going to go ahead and we're going to edit a file- that's the Apache configuration file.
So we're going to use sudo which will give us full access to this file. And we're going to use a text editor. You can use vi or pico. Pico is pretty easy because it works just like text editor in the Mac interface. We want to go to /private/etc/apache2/httpd.conf. And this will then prompt us for the password since we're using the 'sudo' command. We enter in our password and now we have access to this document.
So we're going to do a ctrl, not command, but ctrl+w and search for PHP. It'll quickly take us to the line here where the PHP module is loaded, or in this case the line is commented out. Remove the # at the beginning of that line to uncomment it and then we'll go ahead and exit, entering yes to save modified buffer and hitting return to save it as the same filename.
Great, now we've modified it. Now, we still don't have PHP running because we need to restart Apache. We do that by simply going back to the file sharing window and turning off web sharing and then turning it back on. The quickest, easiest way to restart Apache. Now when we run, we get 6! It's actually executing that echo command with the 5+1 in. So we're actually running PHP now.
So that's a great way to be able to test webpages on your Mac without having to upload to a web server. If you're a PHP programmer who uses a Mac, then this is a must. The next step you probably want to do is to get mySQL running. Just do a google search for 'install mySQL Mac' and it'll come up with one of dozens of different tutorials- always slightly different instructions on how to do it. They probably all work. But you're going to have to go to the website and download the leopard installer, and then it adjust some settings.
Well once you get PHP and mySQL working you have a full web development environment that you can use for a variety of different things and testing out your websites. Until next time this is Gary Rosenzweig at MacMost Now.

Comments: 4 Responses to “MacMost Now 80: Running a Web Server and PHP On Your Mac”

    Glen Farmer
    11 years ago

    Is this like MAMP without downloading MAMP?

    Can I run a WordPress test blog using this or is it for creating blogs from scratch only..

    11 years ago

    Glen: Yes, I have run a WordPress install on my Mac locally. You need to get PHP working, and also install MySQL. That is definitely a great way to customize a theme, as a matter of fact.

    Donna Brooks
    7 years ago

    OK, my eyes glazed over on this one. Not a programmer, although in 2003 I did teach myself HTML from scratch by studying code, reading books, and looking up info on the web. And doing ANYTHING in terminal scares the bejezzus out of me since the last time I had an iMac (and used Terminal for troubleshooting & fsck commands), I think I may have unwittingly unmounted my hard drive & rendered my Mac useless. Either that, or it just died after only 3 years (and the Genius at the Apple Store pronounced it dead). It should have had several more years of life in it.

    What I don’t understand is, if a Mac can act as a webserver, then why can’t it be a HOST for a website?

      7 years ago

      Sorry about your Mac. Maybe take it to an independent shop for a second opinion. If it is just the hard drive, a new drive can be installed very cheaply. But there are many things that a good tech can try first.
      Macs can be used as servers and to host a web site. But having a constant, supported host-level connection between your Mac and the rest of the Internet is another issue.

Comments Closed.