Apache

From KnowledgeNerd
Jump to navigation Jump to search

Apache is a open source HTTP Server.

Installation

  • Get the package from:

http://apache.org

Configuring and administration

Apache 2.0 configuration files are in the directory /etc/apache2/

  • main configuration file: apache2.conf (httpd.conf exists for compatibility issues)

folders:

sites-available - A list of configuration files - one per site. A blank install will contain the file default. The system admin can have as many sites here as they need - however - they will not all be active.

sites-enabled - A list of symlinks to configuration files in sites-available. A blank install will contain a symlink 000default to sites-available/default. The sites listed here are the sites which will be active. The site to be used if no virtual hosts match will be the first file found (hence the 000 on 000default).

mods-available - A list of configuration files - one or more per module. Each dpkg installed module will add files here. e.g. php4.conf and php4.load are added with the libapache2-mod-php package. Again - the system admin can install whatever modules they wish - however - until they are set available they will not be active.

mods-enabled - A list of symlinks to configuration files in modes-available. Only modules linked in here will be activated on the webserver.

htaccess

.htaccess file configuration examples: Htaccess

Website Configuration File

Example of website configuration file: httpd.conf or /etc/apache2/sites-enabled/websitename.conf


Commands e.g. Restart Apache

  • Use the chkconfig command to configure Apache to start at boot:
$chkconfig httpd on -- not working on ubuntu server
  • Use the following scripts to control Apache:
start apache ............... $/etc/init.d/httpd start
stop apache ................ $/etc/init.d/httpd stop
restart apache ............. $/etc/init.d/httpd restart

Ubuntu server:
start apache ............... $/etc/init.d/apache2 start
stop apache ................ $/etc/init.d/apache2 stop
restart apache ............. $/etc/init.d/apache2 restart
reload apache's settings ... $/etc/init.d/apache2 reload
force to reload settings ... $/etc/init.d/apache2 force-reload
start cacheclean ........... $/etc/init.d/apache2 start-htcacheclean
stop cacheclean?? .......... $/etc/init.d/apache2 stop-htcacheclean
apache's status ............ $/etc/init.d/apache2 status
   if running prints e.g. "Apache is running (pid 6135)" else no output.
Enable site ................ $a2ensite
Disable site ............... $a2dissite
Enable mod ................. $a2enmod
Disable mod ................ $a2dismod

PHP:
php-fastcgi restart ........ $/etc/init.d/php-fastcgi restart

Mac OS X:
The apachectl command act as Apache HTTP Server control interface (ctl = control tool?).
httpd command seems to work somehow like alias to apachectl. type httpd -h to see available options.

start apache ............... $sudo apachectl start
stop apache ................ $sudo apachectl stop
restart apache ............. $sudo apachectl restart
show apache settings ....... $sudo apachectl -S
show apachectl commands .... $sudo apachectl -?
list compiled modules ...... $httpd -l  (list every module that is compiled into the server)
check if apache is running . $ps aux | grep httpd (you should find apache process running in /usr/sbin/httpd)

  • You can test whether the Apache process is running with
pgrep httpd
$ps ax | grep httpd

Ubuntu server:
pgrep apache2
$ps ax | grep apache2

Restart php-fastcgi

For example when new module is loaded to server you have to restart php-fastcgi to start using the module

/etc/init.d/php-fastcgi restart

Restart php

Nginx#Restart_PHP

Symlinks

Symlinks...

Virtual Hosts

Add virtual hosts / aliases.

Windows

1. Uncomment the following include statement from bin\apache\apache2.4.9\conf\httpd.conf

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

2. Edit bin\apache\apache2.4.9\conf\extra\httpd-vhosts.conf and check that the default localhost settings are right for your www-root:

# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "d:/wamp/www"
    ServerName localhost
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
</VirtualHost>

then add the following code for your examplesite.local alias:

<VirtualHost *:80>
    ServerAdmin webmaster@examplesite.local
    DocumentRoot "d:/wamp/www/examplesite"
    ServerName examplesite.local
    ServerAlias www.examplesite.local
    ErrorLog "logs/examplesite-error.log"
    CustomLog "logs/examplesite-access.log" common
</VirtualHost>

3. Edit your hosts-file and add following line:

127.0.0.1	      examplesite.local

Enable Apache's url rewrite module (mod_rewrite)

Use a2enmod command (as in "apache2 enable module <module>")

a2enmod rewrite

restart apache to activate new configuration

/etc/init.d/apache2 restart

Logs (Access And Error)

find -name error.log
find -name access.log

Example locations for mysql and php error logs + access log:

./var/log/mysql/error.log
./var/log/apache2/error.log
./var/log/apache2/access.log

FAQ

PHP-Error Messages Not Showing In Browser Or Log Files

Check from the php.ini that following settings are true:

display_errors = On
display_startup_errors = On
log_errors = On

Reload Apache's settings:

/etc/init.d/apache2 reload


Fatal error: Call to undefined function curl_init() in /var/www/viddler/phpviddler/phpviddler.php on line

  • You need to install PHP/CURL module (and sometimes cURL also)
  • Install the php5-curl package
sudo apt-get install php5-curl
  • After installing PHP/CURL module you need to restart php-fastcgi
/etc/init.d/php-fastcgi restart
  • Older php-curl
$sudo apt-get install php-curl
  • To install cURL
sudo apt-get install curl
  • You need to add more repositories if you got following error:
Reading package lists... Done
Building dependency tree... Done
E: Couldn't find package php4-gd

You can add extra repositories using the Synaptic Package Manager System --> Administration --> Synaptic Package Manager --> settings --> repositories Install from SPM by typing "php5-curl" to quick search.

or from command line:

Make a copy of repositories list sudo cp -p /etc/apt/sources.list /etc/apt/sources.list_backup

edit list and add needed rows gksudo gedit /etc/apt/sources.list

Update package list sudo apt-get update

  • restart the apache web server
$sudo /etc/init.d/apache2 restart

Test If CLI-PHP Is Installed On The Server

Print php version:

php -v

Try to execute command line php:

php -r "echo 'Hello World';"