Changes between Initial Version and Version 1 of howto/Apache2


Ignore:
Timestamp:
Dec 28, 2014, 10:53:33 PM (10 years ago)
Author:
magill@…
Comment:

New page - excerpted from howto/MAMP

Legend:

Unmodified
Added
Removed
Modified
  • howto/Apache2

    v1 v1  
     1This page describes the installation of the Apache2 Web server under OSX via MacPorts.
     2
     3For information on integration '''Apache''' with '''MySql''' and '''PhP''', see:''' [[howto/MAMP]]'''
     4
     5
     6= Step 1: '''Turn off Apple's "Personal Web Sharing"''' = #pws
     7If you haven’t already done so, make sure you turn off Apple’s "Personal Web Sharing" so that the default Apache server is not running. While you can set things up so that both servers run on different ports (e.g., port 80 and port 8080), it’s not worth the hassle of maintaining two web server daemons. Just use the latest Apache from MacPorts and be done with it.
     8
     9=== OS X 10.8 and older ===
     10Turn off Personal Web Sharing in the Sharing pane of System Preferences.
     11=== Mavericks ===
     12Stop the web server from the command line:
     13{{{
     14sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
     15}}}
     16=== Yosemite ===
     17Yosemite no longer has Personal Web Sharing as an option. Although the Apache 2 web server is still supplied by Apple as part of the Yosemite distribution, there is no "easy" way to turn it on (or off). So, if you managed to turn it on, you already know how to turn it off!
     18=== OS X Server ===
     19Launch Server Manager and turn off the web server.
     20
     21= Step 2: Install Apache = #apache
     22
     23{{{
     24sudo port install apache2
     25}}}
     26
     27A sample configuration file is provided in `/opt/local/apache2/conf/original/httpd.conf`. Don't edit this file; leave it as is so you can refer to the defaults later. The first time you install apache2, the sample configuration file will be copied to `/opt/local/apache2/conf/httpd.conf` for you. You can edit this file as desired. When you later upgrade apache2 to a newer version, MacPorts will upgrade the sample configuration file but will not modify your `httpd.conf`, so be sure to manually compare your `httpd.conf` with the new sample configuration file and bring over any relevant changes. You should also keep backup copies of your `httpd.conf` in a safe place.
     28
     29== 2A:  Verify your config file ==
     30Verify any changes you have made to the config file: /opt/local/apache2/conf/httpd.conf
     31{{{
     32 /opt/local/apache2/bin/apachectl -t
     33}}}
     34This will return either "Syntax OK" or a specific line by line error listing.
     35
     36==  2B: Activate Apache2 ==
     37Activate your Apache installation so that it starts now and every time you boot your machine:
     38
     39{{{
     40sudo port load apache2
     41}}}
     42If when you issue this command, you immediately receive the message:
     43 /opt/local/etc/LaunchDaemons/org.macports.apache2/org.macports.apache2.plist: Operation already in progress
     44This means that the Launch Daemon believes that it successfully started Apache. At this point, a configuration error is your most likely problem. Verify your config file as above.
     45
     46After correcting any config errors and saving the updated config file, simply run:
     47{{{
     48sudo port unload apache2
     49}}}
     50
     51to stop apache (even though it is not running) and then start it using
     52
     53{{{
     54sudo port load apache2
     55}}}
     56
     57to start it again.
     58
     59The default log file location is: '''/opt/local/apache2/logs/'''. The startup will be logged in "'''error_log'''"
     60
     61== 2C: Initial changes to the config file ==
     62Once you have determined that your basic installation is working, you need to consider making some initial changes to your config file.
     63
     64After each change to the config file, you should again verify the file syntax, and then you need to stop and restart Apache for the changes to take effect.
     65
     66{{{
     67/opt/local/apache2/bin/apachectl -t
     68sudo port unload apache2
     69sudo port load apache2
     70}}}
     71In Apache documentation (including this page), you will see the use of "''apachectl -k restart''" described as the method to cause the config file to be re-read.
     72
     73However under OSX, the purpose of ''launchd'' is to notice processes that stop without its knowledge, and to restart them. So if you use ''apachectl -k restart'', launchd may notice apache "crashing" and restart it itself, confusing apachectl. Instead, you shoulduse "port" to tell launchctl to unload apache2, then load it again. This will result in a short interruption of service.
     74
     75If the sever name is not set properly in the configuration file, `/opt/local/apache2/conf/httpd.conf`, you will encounter the warning below.
     76
     77{{{
     78httpd: Could not reliably determine the server's fully qualified domain name, using <Computer-Name>.local for ServerName
     79}}}
     80
     81Where `<Computer-Name>` refers to the name of your computer as specified in the System Preferences Sharing pane.  To resolve the warning, edit the configuration file and set the server name appropriately.  For personal use, `localhost` is sufficient.
     82
     83{{{
     84ServerName localhost:80
     85}}}
     86
     87To verify Apache is now running, point your browser to http://localhost/ (you may need to reload the page). You should see a page that says "It works!"
     88
     89If desired, reboot your machine and point your browser again to http://localhost/ to confirm that Apache is again running.
     90
     91'''NOTE:''' If you have set "ServerName" to be anything other than "localhost" the links above to "localhost" will fail with Safari complaining "''Failed to open page. Safari can't open the page "localhost" because Safari can't connect to the server "localhost".''
     92=== User directories === #userdir
     93
     94If you would like to be able to access web pages in the Sites directory of your home directory, edit `/opt/local/apache2/conf/httpd.conf` and locate the following line:
     95
     96{{{
     97#Include conf/extra/httpd-userdir.conf
     98}}}
     99
     100Uncomment it by removing the "`#`" at the start of the line so that it reads:
     101
     102{{{
     103Include conf/extra/httpd-userdir.conf
     104}}}
     105
     106Then edit `/opt/local/apache2/conf/extra/httpd-userdir.conf` add the following lines to the end of the file:
     107
     108{{{
     109#
     110# Include user configurations
     111#
     112Include /private/etc/apache2/users/*.conf
     113}}}
     114
     115For Mac OS X 10.4 and lower:
     116{{{
     117#
     118# Include user configurations
     119#
     120Include /private/etc/httpd/users/*.conf
     121}}}
     122
     123Restart Apache using `sudo /opt/local/apache2/bin/apachectl -k restart` to make this change take effect. You can then view your personal pages by accessing http://localhost/~username/, where "username" is your Mac OS X account's short name.
     124
     125=== Local Apache manual === #manual
     126
     127The [http://httpd.apache.org/docs/2.2/ Apache manual] is available on the Apache web site. If you would like to be able to access a copy of this manual on your web server, edit `/opt/local/apache2/conf/httpd.conf` and uncomment the following line:
     128
     129{{{
     130#Include conf/extra/httpd-manual.conf
     131}}}
     132
     133So that it reads like this:
     134
     135{{{
     136Include conf/extra/httpd-manual.conf
     137}}}
     138
     139Restart Apache using `sudo /opt/local/apache2/bin/apachectl -k restart` to make this change take effect. You can then view the manual by accessing http://localhost/manual/.
     140
     141=== Secure Sockets (https) === #ssl
     142
     143This works for a development system (not recommended for production!).
     144
     145{{{
     146openssl genrsa -des3 -out server.key 1024
     147# [enter your passphrase, a simple password we will remove soon]
     148openssl req -new -key server.key -out server.csr
     149# [you can accept all the defaults, it does not matter, or customize it to your liking]
     150openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
     151# [will ask for passphrase]
     152cp server.key server.key.bak
     153openssl rsa -in server.key.bak -out server.key
     154# [passphrase needs to be typed]
     155sudo cp server.crt /opt/local/apache2/conf/
     156sudo cp server.key /opt/local/apache2/conf/
     157}}}
     158
     159Then enable the following in {{{/opt/local/apache2/conf/httpd.conf}}}
     160
     161{{{
     162LoadModule ssl_module modules/mod_ssl.so
     163
     164# Secure (SSL/TLS) connections
     165Include conf/extra/httpd-ssl.conf
     166}}}