Showing posts with label iptables. Show all posts
Showing posts with label iptables. Show all posts

Wednesday, March 27, 2013

Barebones apache install for CentOS

Barebones apache install for CentOS


This article describes how to install an apache web server on CentOS with no extras. It's intended only for users who are experienced administrators or who just want a basic web server install with no details on including modules like PHP or customizing apache for their site.




Why "barebones"?


A barebones article is intended for users who just want to get a software package up and running with the default options and no frills. It's best used by either experienced Linux administrators or users needing to get a package installed to satisfy a prerequisite without going through extensive customization. Most users are advised to use the more in-depth tutorials found elsewhere in the Slicehost articles repository so they can better learn the software they are implementing.

For a more comprehensive survey of this topic, check the links in the "Further reading" section at the end of the article.

Installing apache


Run the following commands:
sudo yum install httpd

Adding iptables rules for apache


The Slicehost articles on configuring CentOS slices leave ports 80 and 443 open.

If you are using the CentOS default iptables rules or have modified the Slicehost default rules, you can add rules for apache with the following commands:
sudo /sbin/iptables -I INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo /sbin/iptables -I OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
sudo /sbin/iptables -I INPUT -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo /sbin/iptables -I OUTPUT -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT

To save those rules so they'll take effect next time the slice is rebooted, run:
sudo service iptables save

Starting and stopping apache


You can start apache with:
sudo /usr/sbin/apachectl start

Similarly, you can stop apache with:
sudo /usr/sbin/apachectl stop

To restart apache gracefully, so existing connections aren't broken but new ones will use any recent configuration changes, run:
sudo /usr/sbin/apachectl graceful

Executing apachectl by itself will show what options can be passed to the command.
/usr/sbin/apachectl

Starting apache at boot time


Ensure apache will start when the slice reboots by running:
sudo /sbin/chkconfig httpd on

Where to put documents


Apache will serve documents for the default site from the directory:
/var/www/html

This directory is called apache's "document root". You can make documents available via the web by putting them in that directory or in a subdirectory. If you were to have "www.example.com" pointing to your slice you could see the default index file in the document root by going to this URL:
http://www.example.com

To access files or subdirectories in the document root, think of the base URL for your web server as an alias for the document root, then add a path to the URL telling the web server to look deeper. For example, if "www.example.com" gets you to the document root of "/var/www/html", to view the file at "/var/www/html/mysite/mypage.html" you would use the URL:
http://www.example.com/mysite/mypage.html

When no filename is specified in the URL apache will look for a default page like "index.html" or "index.htm" in the target directory before returning the default welcome page, an error, or a listing of files in the directory (depending on your configuration).

Log files


Apache's log files are located in the directory:
/var/log/httpd

By default only the root user has access to that directory, so you will need to use sudo to get a directory listing or view files.

The log file that records errors is:
/var/log/httpd/error_log

The log file that records page accesses to the default site is:
/var/log/httpd/access_log

Configuration files


Apache's configuration files are located in:
/etc/httpd

The main configuration file for the web server is:
/etc/http/conf/httpd.conf

The main configuration file sets up the default web site that will be served by apache, as well as defining any mods that will be enabled. It's well-commented so it's worth skimming the file to see what directives are included. Some highlights are:
Listen 80

The "Listen" directive tells apache to listen to a port, an IP address, or a combination of the two. You can include more than one Listen directive. By default, with just the one Listen directive configured, apache will listen to port 80 on all available IP addresses.
DocumentRoot "/var/www/html"

The DocumentRoot directive, as it happens, tells apache where the document root is located. The document root is where apache will look first for files to serve (see the earlier section, "Where to put documents", for more on the document root).
<Directory "/var/www/html">

The Directory directive starts a configuration block that applies the options it contains only to the defined directory and its subdirectories. There can be more than one Directory block in the httpd.conf file. A brief example of a full Directory block is the default "/" Directory entry, which is very restrictive:
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>

Friday, March 15, 2013

General tips for working with iptables



General tips for working with iptables


Need to make some changes to your iptables rules running on one of your servers? Don't worry. While iptables can be a bit risky in that it'll let you add rules without checking your logic there are a few steps that can make your life much easier. Also, as just a general tip: Whenever you are working with modifying network access to a machine or just managing a server in general it pays to have local and/or remote console access to the machine.



  • Use iptables-save and iptables-restore to load your rules unless you are using some custom shell script to load your iptables rules. Even if you are using a custom script to manage your iptables rules knowing and using these commands can be very useful.


 

 

  • Before tinkering with rules always find out where the current rule set in use is saved or save a copy yourself. It gives you a safe place to fall back to if your new rules crash and burn in a fiery ball of death.

  • To save out the current rules just run
    iptables-save > {filename}

    and tada! You now have a safe restore point should your editing go awry.

  • Edit a copy of the save file. This gives you a point of reference if you end up in the unenviable position of trying to figure out why your rule changes didn’t work as expected. diff {saved rules} {new rules} is also your friend when trying to spot what went wrong with your edits. Aren't you glad you have a copy of the new and the original rules?

  • Load your new rule set with
    iptables-restore < {filename}

    If things go belly up don't lose your cool. Just hit the remote console (I did tell you to make sure you have console access right?) and use iptables-restore to reload the saved copy of your known good rule set. You DID save a copy of the previous rule set like you were supposed to right?


 

 


Other things to remember:


  • If iptables-restore doesn't like your syntax it'll let you know without implementing the changes and breaking the current rules. See why this is better than hacking at the live rules?

  • Always remember to check the default behavior for your rule chains! This is important because a default DENY (different than having a deny statement as your last rule) will block all traffic through a chain if you run an
    iptables -F

    to flush all the active rules leaving you without access to your machine and forced to use the console in shame.

  • When adding rules order is VERY important. If you block or allow access to something and then later reverse your decision it won't matter. First come, first served. Everyone else gets sent home hungry.

  • When troubleshooting your rules, or in general if it won't impact performance on your machine, it is usually a good idea to have a rule for logging rejected packets such as
    -A INPUT -j LOG --log-level 6

    so that you can see when your rules are denying traffic by checking /var/log/messages or wherever your OS flavor prefers to dump iptables info.

  • ALWAYS save your rules out to wherever the OS expects to load rules from on boot if you want your current rule changes to survive a reboot. Iptables isn't magic. If your rules aren't in the start up scripts they won't get loaded properly at boot time.


 

 


If you have improvements, corrections or suggestions of what has worked well for you please share


General tips for working with iptables



General tips for working with iptables


Need to make some changes to your iptables rules running on one of your servers? Don't worry. While iptables can be a bit risky in that it'll let you add rules without checking your logic there are a few steps that can make your life much easier. Also, as just a general tip: Whenever you are working with modifying network access to a machine or just managing a server in general it pays to have local and/or remote console access to the machine.



  • Use iptables-save and iptables-restore to load your rules unless you are using some custom shell script to load your iptables rules. Even if you are using a custom script to manage your iptables rules knowing and using these commands can be very useful.


 

 

  • Before tinkering with rules always find out where the current rule set in use is saved or save a copy yourself. It gives you a safe place to fall back to if your new rules crash and burn in a fiery ball of death.

  • To save out the current rules just run
    iptables-save > {filename}

    and tada! You now have a safe restore point should your editing go awry.

  • Edit a copy of the save file. This gives you a point of reference if you end up in the unenviable position of trying to figure out why your rule changes didn’t work as expected. diff {saved rules} {new rules} is also your friend when trying to spot what went wrong with your edits. Aren't you glad you have a copy of the new and the original rules?

  • Load your new rule set with
    iptables-restore < {filename}

    If things go belly up don't lose your cool. Just hit the remote console (I did tell you to make sure you have console access right?) and use iptables-restore to reload the saved copy of your known good rule set. You DID save a copy of the previous rule set like you were supposed to right?


 

 


Other things to remember:


  • If iptables-restore doesn't like your syntax it'll let you know without implementing the changes and breaking the current rules. See why this is better than hacking at the live rules?

  • Always remember to check the default behavior for your rule chains! This is important because a default DENY (different than having a deny statement as your last rule) will block all traffic through a chain if you run an
    iptables -F

    to flush all the active rules leaving you without access to your machine and forced to use the console in shame.

  • When adding rules order is VERY important. If you block or allow access to something and then later reverse your decision it won't matter. First come, first served. Everyone else gets sent home hungry.

  • When troubleshooting your rules, or in general if it won't impact performance on your machine, it is usually a good idea to have a rule for logging rejected packets such as
    -A INPUT -j LOG --log-level 6

    so that you can see when your rules are denying traffic by checking /var/log/messages or wherever your OS flavor prefers to dump iptables info.

  • ALWAYS save your rules out to wherever the OS expects to load rules from on boot if you want your current rule changes to survive a reboot. Iptables isn't magic. If your rules aren't in the start up scripts they won't get loaded properly at boot time.


 

 


If you have improvements, corrections or suggestions of what has worked well for you please share