Installing Apache on Mac/Linux

Mac comes with apache pre-installed, however. You may wish to install your own apache instance
http://margotskapacs.com/2013/09/installing-apache-on-mac/
Before start, make sure the default apache server is not running(see troubleshooting section below). To turn off, go to ‘System Preferences->Sharing’ and uncheck ‘Web Sharing’

Step 1. Installing Apache via macport

port selfupdate
sudo port install apache2

This installes Apache in /opt/local/apache2/ directory

Step 2. Configure Apache

Apache comes with sample configurations, lets copy it

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

In the apache configuration file ../conf/httpd.conf change the following lines to specify root folder from where your websites is going to be serviced

...
<Directory "/opt/local/apache2/htdocs">
...
DocumentRoot "/opt/local/apache2/htdocs"

The path /opt[..]/htdocs’ can be anything you wish but in both places it have to point to the same folder

Next, add ‘index.php’ so that Apache grabs index.php file if present

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

Afterwards, configure Apache interpret all files ending with .php/.phps by adding these lines:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Finally, to enable user directories uncomment:

Include conf/extra/httpd-userdir.conf

Step 3. Activate PHP module

After installing and compiling PHP, there is PHP package that Apache needs to load, so here how to activate it

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
[activating module 'php5' in /opt/local/apache2/conf/httpd.conf] 

Step 4. Start Appache

To start and stop Apache:

sudo /opt/local/apache2/bin/apachectl start
sudo /opt/local/apache2/bin/apachectl stop

After starting, point your browser to ‘locahost’ and it should work. To test php, add the following file to the folder you specified ‘DocumentRoot’ in above:

<?php
phpinfo();
?>

Afterwards point to this file ‘localhost/fileName.php’ and it should display php configurations

Enable Virtual Hosting

To Enable virtual hosting, uncomment the following line in apache configuration file conf/httpd.conf

Include conf/extra/httpd-vhosts.conf

Afterwards, configure you virtual host at conf/extra/httpd-vhosts.conf. Here is an example to get you started:

NameVirtualHost *:80
...
<VirtualHost *:80>
    DocumentRoot "/path/to/your/site"
    ServerName domainName.com
    <Directory /path/to/your/site>
        Options FollowSymLinks
        AllowOverride All
        Allow from all
        Order Deny,Allow
    </Directory>
    ErrorLog "/private/var/log/apache2/domainName.com-error_log"
    CustomLog "/private/var/log/apache2/domainName.com-access_log" common
</VirtualHost>

Troubleshooting

1. To see Apache looaded configs:
httpd -V
//or for Debian servers
apache2ctl -V
2. To see if Apache deamon is running
ps -ef | grep httpd
3./.htaccess: Options not allowed here

This happen after specifying alias in the virtual host but never declaring directive:

     <Directory /path/to/the/alias>
        Options FollowSymLinks
        AllowOverride All
        Order Deny,Allow
        Allow from all
    </Directory>
    Alias /path /path/to/the/alias

So, by including the “<Directory..” gives the permissions for the .htaccess manage the ‘Options’ configuration solving the problem you see in the log

4. Virtual Hosting is Not Working

Make sure you have added "NameVirtualHost *:80" in one of the configurations that is being loaded such apache2.conf, some-v-host.conf,etc

5. client denied by server configuration (PHP FPM)

Make user to have Require all granted part of the Virtual Host configuration Directory section as following:

<Directory "/path/to/serving/dir">
		Options Indexes MultiViews FollowSymLinks
		AllowOverride All
		Order allow,deny
		Require all granted
	</Directory>
6. Forms Will not submit or “AH00128: File does not exist” in logs

The forms could be submitted and the apache logs were showing “AH00128: File does not exist[..]” for Apache 2.4. There issue is that mod_rewrite is not enabled by default. You can check if the mod_rewrite is enabled and loading by looking at phpinfo(). To enable mod_rewrite:

sudo a2enmod rewrite
sudo service apache2 restart

This will load mod_rewrite making the forms to Post for Drupal

caught SIGTERM, shutting down

This error was coming up for Apache 2.4 on Ubuntu after logging into Drupal. I am not sure which exactly fix it but did the following:

  • turn off limit for MaxKeepAliveRequests in the apache2.conf configuration file
  • Increase memory in php.ini by changing memory_limit
  • Increased max_input_time in php.ini
  • Increased max_execution_time in php.ini

In addition, it appears that apache restarts with the above error every time there is php syntax error.

Blank Screen

By default Apache will display blank screen when there is php error. To turn on errors to be displayed in browser instead of blank screen, edit php.ini as following:

display_errors=On
html_errors=On
error_reporting=E_ALL | E_STRICT 
display_startup_errors=On

This will enable error reporting in the browser. In case it doesn’t, then at your application level there must be ErrorHandler defined that hides all the errors. See more info here http://stackoverflow.com/questions/1475297/phps-white-screen-of-death

Troubleshooting Tip

When there is error, turn on max logging by changing ErrorLevel to trace8 in virtual host settings or Apache global settings:

...
 LogLevel trace8
 ErrorLog ${APACHE_LOG_DIR}/dev-virtual/error.log
..

Afterwards, tail the logs specified in ErrorLog from command line to follow real time Apache execution:

tail -f APACHE_LOG_DIR/dev-virtual/error.log

At last, make the requests and follow the logs in command line:

Useful Links

  • http://www.thegeekstuff.com/2008/08/9-tips-to-use-apachectl-and-httpd-like-a-power-user/
  • http://2tbsp.com/content/install_apache_2_and_php_5_macports

Leave a Reply

Your email address will not be published. Required fields are marked *