Dan Colish

Ranting and Raving… still

Dan Colish header image 2

Apache2 with Macports

December 1st, 2007 · 5 Comments

Using Macports to install Apache2 is a cinch. First, make sure you actually have Macports installed and correctly configured. If you don’t visit www.macports.org where you can find the Macports download and documentation.Once that’s all taken care of you will need prepare for the Apache2 install. (Note: If you’re not using Leopard, try these instructions at 2tbsp: Installing Apache2 and PHP 5 with MacPorts.) Also note, there is a serious error with mod_ssl when installing with Macports. You have to turn this off before Apache2 will load. If you need mod_ssl, then Leopard may not be for you. Run a few Google searches for more info before continuing.At this point you’re ready to install. Begin by entering:

$ sudo port install sqlite3

This will being the install of the sqlite3 package. This step must come first to ensure proper installation. If you don’t install sqlite3 there is a good chance the portfile will not be able to load it as a dependency to Apache2 during the managed installation and your install will fail. If you get the error:

sudo: port: command not found

This means you did not add Macports to your user enviroment. You need to go back and add Macports, I use the following:

$ pico ~/.profile

Once in the Pico text editor add the following lines:

export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export DISPLAY=:0.0
export EDITOR=/usr/bin/pico

Write the file out using ctrl-o, then exit pico with ctrl-x and at the prompt type:

$ . ~/.profile

Finally check that you’re correctly configured:

$ env

This should return the following along with a bunch of other variables:

TERM_PROGRAM=Apple_Terminal
TERM=xterm-color
SHELL=/bin/bash
PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
EDITOR=/usr/bin/pico
DISPLAY=:0.0

If you path matches this output you’re good to go, retype:

$ sudo port install sqlite3

Since your environment is correctly installed, Macports will take control and install sqlite3. Once you’ve got installed sqlite3, you need to install apache2. Start by typing:

$ sudo port install apache2

Let Macports install your apache2 server. This can take a little while since Macports may need to download a number of dependencies, so grab a quick snack or read something. When you come back you should see the following code:

###########################################################
# A startup item has been generated that will aid in
# starting apache2 with launchd. It is disabled# by default. Execute the following command to start it,
# and to cause it to launch at startup:
## sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
###########################################################

Just cut and past the final line to add the startup item to LaunchD:

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

Once that startup item is loaded Apache will be able to launch on login. You might also want to start, stop and restart Apache while logged in. I use an alias to the apachectl application at /opt/local/apache2/bin/apachectl. To create the alias, add the following line to your bash ~/.profile file:

alias apachestart='sudo /opt/local/apache2/bin/apachectlstart'
alias apachestop='sudo /opt/local/apache2/bin/apachectlstop'
alias apacherestart='sudo /opt/local/apache2/bin/apachectlrestart'

I’ve used these commands because they describe the actions I want to perform. You could also do what Chad at 2tbsp suggests at just alias apachectl with any name besides apachectl such as:

alias apachectl2='sudo /opt/local/apache2/bin/apachectl'

Great so you’ve almost got everything ready to launch Apache for the first time! Now you need to setup your httpd.conf file. Goto your shell prompt and type the following to create your httpd.conf file:

$ sudo ditto /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf

Now we have to do a few more things to properly configure this file for Leopard and Apache2. There is a serious error with mod_ssl when installing with Macports. You have to turn this off before Apache2 will load. If you need SSL, then Leopard may not be for you. Let’s configure this file, go into Pico and find the line that controls the ssl_module, then turn that off:

$ sudo pico /opt/local/apache2/conf/httpd.conf
...
#LoadModule ssl_module modules/mod_ssl.so
....

Next, find your DocumentRoot. By default is set to /opt/local/apache2/htdocs, but you can set it to any folder you want. If you’ve been using the built-in server, set it to /Library/Webserver/Documents; I set it to /Users/[your login]/Sites. Do this in your httpd.conf by typing the following:Swap

...
DocumentRoot "/opt/local/apache2/htdocs"
...

for

...
DocumentRoot "/Users/[Login Name]/Sites"
...

or enter the directory of your choice. Then swap:

...
Directory "/opt/local/apache2/htdocs"
...

for

...
Directory "/Users/[Login Name]/Sites"
...

One thing to pay attention for, both your DocumentRoot and your main Directory need to be the same. Go back and check, if they’re different, make them the same. Your server will not work correctly if you do not. Now save and close the httpd.conf file and return to your command line. We are now going to start Apache for the first time and if all the files were configured correctly the server will load. At the command line type:

$ apachestart  #This will only work if you used my alias suggested earlier in this article.

Launch any browser, type in http://localhost and see the Leopard Personal Site test page if you linked to your /user/[login name]/sites directory.

Leopard Test User Site Screenshot

To stop the server go back to the command prompt and enter:

$ apachestop #Make sure your alias is setup correctly.

Well that’s it for setting up your webserver with macports. Of course you could have just turned on web sharing in your sharing prefs, but that’s boring and you can’t update that server as easily. If you want to learn more about the Apache Web Server check out their site: httpd.apache.org. Also try reading up on how to configure new directives for your httpd.conf file.Thanks for reading and check back soon for more info on setting up MySQL5 and PHP5 with the web server you just build.

convert this post to pdf.

Tags: Science and Technology · Software

5 responses so far ↓

  • 1 screenmates // Jan 2, 2008 at 10:06 am

    Great article!

    I used MacPorts 1.6 on Lepoard and installed apache2, php5 and mysql as under:

    sudo port install apache2 (this runs into errors at ncurses*, etc. with the error “No awk found!” and so I had to run it again)

    sudo port install apache2 (this resumes at ncurses* and goes well) sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

    sudo port install mysql5 +server sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist sudo -u mysql mysql_install_db5 (NEW INSTALL)

    sudo port install php5 +macosx +apache2 +php5-memcache +memcached +pear +phpmyadmin +gd2 +ipc

    sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf

    cd /opt/local/apache2/modules sudo /opt/local/apache2/bin/apxs -a -e -n “php5″ libphp5.so

    sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini

    And then, I modify the httpd.conf to AddType, DocumentRoot, etc.

    Although the following command says “Mysql is alive!”, it does not appear on phpinfo() output:

    mysqladmin5 -u root -p ping

    Another problem is that my application uses sqlite but after the above installation, my app bombs saying “SQLite extension can’t be loaded!” although sqlite3 is active and does not show up in phpinfo() output either. I saw sqlite3 being installed automatically with apache2. Here is the output of installed ports and their statuses which are all active:

    sudo port installed apache2 The following ports are currently installed: apache2 @2.2.6_0+darwin_9 (active)

    sudo port installed php5 The following ports are currently installed: php5 @5.2.5_1+apache2+ipc+macosx+pear (active)

    sudo port installed mysql5 The following ports are currently installed: mysql5 @5.0.51_0+server (active)

    sudo port installed
    The following ports are currently installed:
    apache2 @2.2.6_0+darwin_9 (active)
    apr @1.2.12_0+darwin_9 (active)
    apr-util @1.2.12_0 (active)
    bzip2 @1.0.4_1 (active)
    curl @7.17.1_0 (active)
    db44 @4.4.20_1 (active)
    expat @2.0.1_0 (active)
    freetype @2.3.5_1 (active)
    gawk @3.1.5_2 (active)
    gettext @0.17_2 (active)
    jpeg @6b_2 (active)
    libevent @1.3e_0 (active)
    libiconv @1.12_0 (active)
    libmcrypt @2.5.8_0 (active)
    libpng @1.2.24_0 (active)
    libxml2 @2.6.30_0 (active)
    libxslt @1.1.22_0 (active)
    memcached @1.2.4_1 (active)
    mhash @0.9.9_0 (active)
    mysql5 @5.0.51_0+server (active)
    ncurses @5.6_0 (active)
    ncursesw @5.6_0+darwin_9 (active)
    openssl @0.9.8g_0 (active)
    pcre @7.4_0 (active)
    php5 @5.2.5_1+apache2+ipc+macosx+pear (active)
    php5-memcache @2.1.2_0 (active)
    pkgconfig @0.22_0 (active)
    readline @5.2.007_0+darwin_9 (active)
    sqlite3 @3.5.4_0 (active)
    tiff @3.8.2_1+macosx (active)
    zlib @1.2.3_1 (active)

    Upgrading a port with additional variants does not work. For instance, sudo port upgrade php5 +tidy does not work!

    Can you please help?

    TIA

  • 2 mauwolff // Jul 17, 2008 at 6:14 am

    TIA: you have to be more explicit in your php5 installation with macports. Mine is:

    php5 @5.2.6_1+apache2+fastcgi+imap+macosx+mysql5+postgresql83+snmp+sockets+sqlite+tidy

    And to install with these options:

    # sudo port install php5 +apache2+fastcgi+imap+macosx+mysql5+postgresql83+snmp+sockets+sqlite+tidy

    cheers

  • 3 mauwolff // Jul 17, 2008 at 6:15 am

    Just a quick one: to check what variants are available, do:

    # port variants php5

  • 4 dan // Jul 17, 2008 at 9:09 pm

    I guess I never got around to showing the steps for php5 but its fairly simple as well. You can use all in the variants or I like to use just the basics.

    port install php5 +apache2 +mysql5 +pear

    if you’re not using those additional postgres or other php modules you dont need to install them at first

    Check out the port file for more options and what they will actually do.

    http://trac.macports.org/browser/trunk/dports/www/php5/Portfile

  • 5 dan // Jul 17, 2008 at 9:12 pm

    I like using Pear( http://pear.php.net/packages.php) for actually managing the php plugins

Leave a Comment