Installation

  • Check if Apche is already installed — rpm -qa | grep httpd
  • Install Apache Webserver — yum groupinstall “Web Server”
    yum install httpd
  • Install PHP Support — yum groupinstall “PHP Support”
    yum install php php-mysql
  • List of useful PHP-libraries and -modules — yum search php-
  • To see more details about what each module does — yum info name of the module
  • Start Webserver — service httpd start
    apachectl start
  • set the Apache to run automatically when the server boots — chkconfig httpd on
  • Install LAMP (Linux, Apache, MySQL, PHP) — yum groupinstall “Web Server” “PHP Support” “MySQL Database server” “MySQL Database client”
    yum install php-mysql
  • See PHP on the server by creating testpage — vi /var/www/html/info.php <?php phpinfo(); ?>
  • Visit your site — http://server/info.php
  • Restart Apache — service httpd restart
  • Scrtipt to control the functioning of the Apache httpd daemon — apachectl start|stop|restart|fullstatus|status|configtest
  • Restart / Stop without aborting currently open connections — apachectl graceful |graceful-stop

Configuration

The default configuration (/etc/httpd/conf/httpd.conf) is already preconfigured to get Apache Server running. All you need is to write your index.html and your web pages and place them in the default DocumentRoot ( /var/www/html).

Best Practices

There are numerous ways to organize your Apache configuration files. In some cases this will be dictated by your Linux distribution.

  • Minimize changes to /etc/httpd/conf/httpd.conf. It’s easier to upgrade or migrate if this file has few or no changes.
  • Place virtual host definitions in /etc/httpd/conf/vhosts.conf.
  • Put the document root for each website at /var/www/vhosts/[domain name]/httpdocs. Do not include “www” on the domain name. For example with “exampledomain.com” the document root would be /var/www/vhosts/ exampledomain.com /httpdocs.

Here’s a template for the virtual host definition:

NameVirtualHost *:80

<VirtualHost *:80>
ServerAdmin webmaster@ exampledomain.com
ServerName exampledomain.com
ServerAlias www. exampledomain.com
DocumentRoot /var/www/vhosts/ exampledomain.com /httpdocs
ErrorLog logs/ exampledomain.com -error_log
CustomLog logs/ exampledomain.com -access_log combined
<Directory /var/www/vhosts/ exampledomain.com /httpdocs>
AllowOverride All
</Directory>
</VirtualHost>

Firewall

Make sure the firewall does not block access to port 80 by running:

system-config-securitylevel

Allow WWW(HTTP) as “Trusted Services”

Configuring user based security

To allow only authorized users who have passwords, add the following lines to /etc/httpd/conf/httpd.conf.

Just before the commented option for the UserDir directive:

AuthType Basic
AuthName “Test Passwords”
AuthUserFile /etc/httpd/passdb
Require user user1

Configuring Passwords

To create the passwords database passdb in /etc/httpd and add user1 to it run the following command:

htpasswd -c /etc/httpd/passdb user1

To add another user, run the following command without the -c switch (which creates the database):

htpasswd /etc/httpd/passdb user2

Configuring host based security

Add the following to the Directory if you want to allow access only to your network. If your network for example is 192.168.1.0:

# vi /etc/httpd/conf/httpd.conf
Order allow,deny
Allow from 192.168.1.0/255.255.255.0

Setting permissions for PHP sites

Secure your site by setting the correct permissions on your DocumentRoot:

  • Set Apache user as the owner for your DocumentRoot — chown -R apache:apache /var/www/html/yoursite
  • Modify permissions — chmod -R 400 /var/www/html/yoursitechmod -R a+X /var/www/html/yoursite

In some cases your PHP site requires write access in some folders. For example WordPress sites requires write access into the /wp-content folder to could install plugings, themes or updates.

  • Provide write permissions to WordPress — chmod -R 775 /var/www/html/yoursite/wp-content

Troubleshooting

  • Make sure Apache is running — service httpd status
  • Start Apache — apachectl start
    service httpd start
  • Make sure Apache is starting during the boot process — chkconfig httpd on
  • Test the syntax of the configuration — httpd -t httpd –S
  • view error log file — tail /var/log/httpd/error_log
  • view Access log file — tail /var/log/httpd/access_log
  • restart Apache without disconnecting the user — service httpd reload
    apachectl graceful