DescubreApple

Apple, Mac, iPhone, iPad

Resources

Apache and PHP on Mac

We could use MAMP, but let’s experiment a little.

To do so, we’ll have to get rid of our fear, since, being Mac OS X a UNIX system, it has another way of doing things that is really effective: Terminal

PHPWe’ll go step by step, command by command, explaining what is done and why, to make the task of setting up a basic web server on which to share your personal website, for example, on an internal network, really simple.

First of all, it must be said that using the resources Mac OS X provides by default to use an iMac, for example, as a web server is not a very viable option, since there are many other better and more powerful solutions for this type of use.

Well, let’s begin.

You should know that Mac OS X comes with an Apache server installed by default, and version 4.3.11 of PHP.
Obviously it is not the most current, but it will surely come in handy for solving some minor problem, or for writing a small script we need.

The first thing we’ll do is start the Apache server.
We could do it in a very convenient way… but we’ll take the “hard” route and I’ll mention the easy way afterwards, so everyone gets their hands dirty.

Open a Terminal.
The first thing we’ll do is type this:

sudo su

It will ask for our root/administrator password, and then we will have sufficient privileges to carry out the whole task.

Now, to start the Apache server, we type this in the Terminal:

apachectl start

A multi-line message will appear and confirm that the server is running:

/usr/sbin/apachectl/ start: httpd started

Now, let’s move on to the editing part.
We have to open a file and modify some specific lines so that, with Apache running, we can use PHP; so what we’ll do is enable it.

We type in the Terminal:

cd /etc/httpd

With this, we move into the folder where the Apache server is installed.
Now, what we need to do is edit the file “httpd.conf” located in that folder.

pico httpd.conf

Pico is a very simple and useful file editor to use from Terminal, without having to load files with any other plain-text editing program.

Now, in the Terminal, we will have all the lines that make up the “httpd.conf” file, and we need to focus on finding some specific ones.
We press “CTRL+W” and a search box will appear, where we type “#LoadModule php4_module”.
It will find the following line:

#LoadModule php4_module libexec/httpd/libphp4.so

Now, moving with the cursor keys, we edit that line and delete the hash “#”, thereby changing the line from being commented out to being processed.

Next, we do the same search with the following line:

#AddModule mod_php4.c

And we do the same thing: we delete the hash “#” to uncomment it.

All that remains is to tell Apache that the PHP extension is valid and that it should process it.
It will take care of sending it to the PHP processor so that it executes line by line the files we enter, and returns them correctly.

We look for this code fragment:

</IfModule mod_dir.c>
DirectoryIndex index.html
</IfModule>

And after “index.html” we add the following, “index.htm index.php”, so it looks like this:

</IfModule mod_dir.c>
DirectoryIndex index.html index.htm index.php
</IfModule>

We save the file, overwriting it, with CTRL+O, and exit with CTRL+X.
Now we need to restart Apache:

apachectl restart

Or we could have stopped it completely with:

apachectl stop
apachectl start

With this, we will have a small Apache server with fully functional PHP.
The only thing missing would be the ability to use some database connection, for example with MySQL.

Next installment: MySQL on Mac

AC

Angel C. Delgado

Besides being a fanatic of everything Apple does, I'm also just another blogger. <a href="http://www.ancude.net/myblog/">Ancude</a> is the blog I run, a blog named after the nick I use around here, which is simply the combination of my first name and surname; <strong><abbr title="Angel Custodio Delgado">Angel C. Delgado</abbr></strong>. I love photography, video games, and computing and technology in general; any gadget that falls into my hands, I analyze down to its guts. If I had to choose a movie it would be Matrix, if I had to choose a band it would be Linkin Park, although if it were a solo artist it would surely be Louis Armstrong (I love jazz), and if I had to reread a book it would undoubtedly be "The Gun Seller" by Hugh Laurie.

11 comments · Comment

11 comments

  1. Estoy teniendo problemas para instalar MySQL, a ver si haceis la segnda parte en breve ^^

  2. Me parece muy interesante, voy a probar las instrucciones, espero tener éxito!.

  3. Saludos, igual que a Franklin al colocar apachectl start lo que sale es
    launchctl:CFURLWriteDataAndPropertiesToResource(/System/Library/LaunchDaemons/org.apache.httpd.plist) failed: -10.

  4. ya soy otro con dificultades al igual que Julio me aparece
    launchctl:CFURLWriteDataAndPropertiesToResource(/System/Library/LaunchDaemons/org.apache.httpd.plist) failed: -10.

    seria bueno una ayuda. salu2

  5. Buenas!
    Yo estoy teniendo problemas es con esto,

    Last login: Sat Dec 12 02:25:17 on ttys000
    macbook-pro-de-jose-rafael-pages:~ joserafaelpages$ sudo su
    sh-3.2# apachectl start
    org.apache.httpd: Already loaded

    supongo que es porque ya apache esta “corriendo” luego sigo con la parte de edtitar y me dice:

    sh-3.2# cd /etc/httpd
    sh: cd: /etc/httpd: No such file or directory

    ayuda!!!

    1. Para editar en lugar de poner:
      sh-3.2# cd /etc/httpd

      Cambialo por:
      sh-3.2# vi /etc/httpd

      ya que el comando cd es para moverte entre directorios y vi es un programa de consola para editar archivos

  6. por cierto, el servidor parece estar funcionando a la perfeccion, pero sin leerme PHP. ya he subido una web que tenia a la carpeta “site” y he logrado entrar en todas las maquinas de mi red incluso en la de afuera… necesitaria ayuda es con lo de PHP ps, de esta manera puedo usar el mysql y hacer las bases de datos… un saludo!

  7. al igual que jose mi problemas es que no se como hacer que el servidor me lea php. si me podrian ayudar… gracias y muy interesante todo este articulo.
    un saludo

🇬🇧 EN