.htaccess – Deny IP & Block IP Range

The .htaccess file is a configuration file for the Apache web server, that can be used to restrict access to a web-site from a specific IP or a range of IP addresses.

In this note i will show how to deny access from one or several IP addresses via .htaccess file and how to block access from a range of IP addresses or from entire subnets. (more…)

mod_wsgi – Find Out The Version

The mod_wsgi is an Apache module that provides a WSGI compliant interface and serves for hosting Python based web applications under Apache (for example Django).

To find out which version of mod_wsgi is installed or compiled, it is required to create a simple WSGI application.

From the following article you will learn how to create and run on Apache a simple WSGI application that prints the version of mod_wsgi. (more…)

Apache Modules – List All Enabled (Loaded)

Apache has a lot of modules (or “mods”) that extend its core functionality for special purposes.

How to list installed Apache modules – is a common question as it is very often required to check which Apache modules are loaded/enabled.

List of installed Apache modules can be found using special option for httpd/apachectl (CentOS, RHEL etc.) or apache2/apache2ctl (Ubuntu, Debian etc.) commands. (more…)

List All VHosts in Apache

If you have to troubleshoot any Apache issues, it is always a good idea to start with checking of the currently enabled Virtual Hosts.

This can be done with the -S option used on the httpd/apache2 or on the apachectl/apache2ctl.

-S option shows which ports and which IP addresses or domain names are configured for each website being served, shows where the configuration file for each VirtualHost is located and performs a syntax check.

Get a List of All VHosts in Apache

Use one of the following commands to get a list of all VHosts in Apache on RHEL, CentOS, Fedora etc.:

$ httpd -S
$ apachectl -S

List All Virtual Hosts Served by Apache2

Use one of the following commands to list all Virtual Hosts served by Apache2 on Ubuntu, Debian, Linux Mint etc.:

$ apache2 -S
$ apache2ctl -S

In the output of the above commands you’ll see all currently enabled Virtual Hosts and the result of a syntax check:

VirtualHost configuration:
1.2.3.4:80          is a NameVirtualHost
default server default (/etc/apache2/sites-enabled/default.conf:1)
port 80 namevhost example.com (/etc/apache2/sites-enabled/example.conf:1)
port 443 namevhost example.com (/etc/apache2/sites-enabled/example.conf:15)
Syntax OK

Redirect Site to Maintenance Page using Apache and HTAccess

In this article you will find how to redirect all traffic and all visitors of your site to a maintenance page during site updates.

I’ll show how to create a maintenance page, how to put the site into a maintenance mode and how to bring it back online without restarting Apache.

You’ll learn how to redirect Apache Vhost’s traffic to a maintenance page using either VirtualHost configuration file or using .HTAccess file. (more…)

Apache Web-Server Installation on CentOS/RHEL

This guide explains how to install Apache Web Server on CentOS/RHEL based systems.

Apache Server Installation

Execute the following command to install the latest Apache Server from basic CentOS/RHEL repositories:

# yum install httpd

Set the Apache Server daemon to start at boot.

# chkconfig httpd on

Basic Apache Configuration

Backup the Apache configuration file httpd.conf.

# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.backup

Open the Apache configuration file and un-comment the line, containing the text “NameVirtualHost *:80”.

# vi /etc/httpd/conf/httpd.conf

Save and close the file.

Firewall opening for Apache Server

Add the rules to IPTABLES.

# vi /etc/sysconfig/iptables

Append the following lines before the REJECT line, to open http and https ports 80 and 443:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

Save and close the file. Restart the firewall.

# service iptables restart

Confirming the Apache Server Installation

Start the Apache HTTP Server daemon.

# service httpd start

Visit http://localhost/ in your web browser, if you’ve installed server on your local machine, or enter the server’s IP address. You should see an Apache Test Page.

Moving SSL Certificate from IIS to Apache

This procedure will help you to move or copy your SSL certificate, installed on an IIS server to an Apache server.

Step 1: Export IIS certificate into a .PFX file

  • Run mmc.exe
  • Click the ‘Console’ menu and then click ‘Add/Remove Snap-in’.
  • Click the ‘Add’ button and then choose the ‘certificates’ snap-in and click on ‘Add’.
  • Select ‘Computer Account’ then click ‘Next’.
  • Select ‘Local Computer’ and then click ‘OK’.
  • Click ‘Close’ and then click ‘OK’.
  • Expand the menu for ‘Certificates’ and click on the ‘Personal’ folder.
  • Right click on the certificate that you want to export and select ‘All tasks’ -> ‘Export’.
  • A wizard will appear. Make sure you check the box to include the private key and continue through with this wizard until you have a .PFX file.

Step 2: Extract the private key

Export the private key file from the .PFX file.

$ openssl pkcs12 -in filename.pfx -nocerts -out key.pem

Step 3: Extract the certificate file

Export the certificate file from the .PFX file.

$ openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem

Step 4: Remove the passphrase

This command removes the passphrase from the private key so Apache won’t prompt you for your passphase when it starts.

$ openssl rsa -in key.pem -out server.key

Extra Steps

Make sure that the following lines are present in your apache virtual host configuration file and they are correct:

SSLEngine on
SSLOptions +StrictRequire
SSLCertificateFile /path/to/certificate/cert.pem
SSLCertificateKeyFile /patch/to/key/server.key

Don’t forget to restart apache at the end.

HowTo: Install LAMP on CentOS/RHEL

In this guide, I will show you, how to install LAMP (Linux, Apache, MySQL, PHP) – a stack of free, open source software for building a web server for general purpose.

1. Install Apache HTTP Server

yum install httpd

Backup the apache configuration file ‘httpd.conf’.

cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.backup

Set the apache service to start at boot.

chkconfig httpd on

Open the httpd configuration file and un-comment the line, containing the text “NameVirtualHost *:80”.

vi /etc/httpd/conf/httpd.conf

Add the rules to IPTABLES.

vi /etc/sysconfig/iptables

Append the following lines before the REJECT line, to open http and https ports 80 and 443:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

Save and close the file. Restart the firewall.

service iptables restart

Start the Apache HTTP Server daemon.

service httpd start

Visit http://localhost/ in your web browser, if you’ve installed server on your local machine, or enter the server’s IP address. You should see an Apache Test Page.

2. Install MySQL

yum install mysql-server

Set the MySQL service to start at boot.

chkconfig mysqld on

Start the MySQL service.

service mysqld start

Set the root password for MySQL.

mysqladmin -u root password NEWPASSWORD

Test connectivity to MySQL.

mysql -u root -p

3. Install PHP

yum install php php-mysql

Restart Apache.

service httpd restart

You should now have the latest PHP installed:

php -v