Building a secure web server with CentOS 5, part 3

Posted on 05.Jan 2010 by in LAMP, Linux, Web Hosting

Part 1Part 2Part 3Troubleshooting

Part 3: Adding websites

In the following steps, we will base our configuration on a fictitious company called Happy Burger who has already registered the domain name, happyburger.net. We will point www.happyburger.net to the IP address of this web server. When you are creating your own site, substitute the customer name and domain name for that of the actual customer. * If a Happy Burger really exists, then this is in no way associated with them and is purely coincidental.

Create a User Account

The first step is to create a user account that will be associated with this website and be used to authenticate via FTP. When creating the password, make sure that it is at least 8 characters, alphanumeric, mixed case and includes numbers. I usually generate random passwords for this.

# adduser –s /sbin/nologin happyburger
# passwd happyburger

Creating the directory structure

Each website must have the following directory structure in order to support access logs, web statistics, .htpasswd files, CGI scripts and the public web directory.

/home/.sites/happyburger/: This path will contain a directory for each website. Each directory should be named after the customer name, in lowercase.
/home/.sites/happyburger/web/: This path contains the website contents (public root).
/home/.sites/happyburger/web/stats/: This path will contain the Webalizer statistics, and is password protected using .htaccess.
/home/.sites/happyburger/private/: This path is not accessible from the internet, and contains the .htpasswd file.
/home/.sites/happyburger/cgi-bin/: Apache uses this path as the CGI script directory, by using a script alias.
/home/.sites/happyburger/logs/: This path stores the log files that Apache generates.

Create the structure as follows:

# cd /home/.sites
# mkdir happyburger
# cd happyburger
# mkdir web cgi-bin private logs
# cd web
# mkdir stats

Now change the ownership of these directories, substituting <username> (in this case, happyburger):

# cd /home/.sites
# chown <username> happyburger -R

Configuring Apache

As Apache will be configured using multiple ‘virtual hosts’ we need to create a separate configuration file for each virtual host. To do this we will create a vhost directory, and configure the Apache configuration file to read each of these virtual host configurations.

# cd /etc/httpd/vhost (If this directory does not exist then you will need to create it)

Now we will create the virtual host configuration file for this particular website.

# vi happyburger.conf

Now enter the following into the newly created configuration file:

<VirtualHost *:80>
ServerAdmin admin@happyburger.net
DocumentRoot /home/.sites/happyburger/web
ServerName www.happyburger.net
ServerAlias happyburger.net
ServerAlias www.happyburger.com
ServerAlias happyburger.com
ScriptAlias /cgi-bin/ /home/.sites/happyburger/cgi-bin/
<Directory /home/.sites/happyburger/web>
Options FollowSymLinks
Options +Includes +ExecCGI
AllowOverride All
</Directory>
</VirtualHost>

Once this has been saved, we will then need to configure Apache to include this in the main configuration.

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

At the end of the configuration file add the following line:

Include /etc/httpd/vhost/happyburger.conf

Now restart httpd:

# /etc/init.d/httpd restart

Configuring FTP (VSFTP)

Before the new account can login with FTP, you must add the new user to vsftp.user_list which contains a list of all accounts permitted to use the FTP service.

# vi /etc/vsftpd.user_list

Add the new user to the list.

Thanks for reading! Please comment or Tweet this page (see below)


Tags , , , , , ,

17 Responses

  1. Gav says:

    Great walk though, but I had to do the following to access the server on port 80 from anything other than the localhost:
    vi /etc/sysconfig/iptables
    -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
    service iptables restart

    • Ray Heffer says:

      Hi Gav,

      I didn’t include anything on IPTABLES (was being lazy!), but I will certainly think about adding an IPTABLES post with some best practices, logging, etc.

      Thanks for the comment!

      Ray

  2. You know you’ve made me think – is there any further online resource you would recommend looking at?

  3. Keyser Soze says:

    When i try to add a user like above:

    adduser –s /sbin/nologin happyburger

    I get this error:

    bash: adduser: command not found

    How can i solve this?

  4. Keyser Soze says:

    Hi Klauss,

    I like your tutorial very mutch and i have settup my first homeserver. Do you now a great tutorial for backing up the system? I have a windows 2003 server where i want to backup all important data from this linux server like: /home/.sites and all mysql databases.

  5. Well, nice journey i have with you, Ray!
    Thanks alot for your clear tutorial. Now i can manage my VPS and successfully create web server on it. Thanks again!

  6. buzzknow says:

    how to update PHP to 5.3.2?

    thanks :)

  7. P2O2 says:

    Keyser Soze says:
    April 16, 2010 at 11:27
    adduser –s /sbin/nologin happyburger
    I get this error:
    bash: adduser: command not found

    Hi,

    Try to use /usr/sbin/adduser (Scientific Linux 5.5) or /sbin/adduser (the duce knows where…).

    Regards

  8. chalo says:

    Nice read that was….
    Im yet to get this one bit, when a new account [eg mywebsite.com] is added the web server has to be started? or this is the case for a “small” server?

  9. masinius says:

    this was very nice tutorial!!
    i did everything you mentiond in this tutorial, however, after linking the domain to the DNS, it’s still going to the main server, not to any one of the vhosts.
    what does that mean??

  10. Tora Voskamp says:

    I have been hosting my sites mostly with DreamHost for a while now and think they deliver a reliable service. Nowadays almost all of the hosting companies do a good job and there are only a handful that stand out negatively.

  11. Thank you for the post! Very helpful.

  12. maks says:

    hi, thanks for this, but i ran into one problem…
    i’ve pointed to the web browser to ip/folder/index.php and it cant get nothing… but if i point to ip/test.php it reads the file inside the _default folder…
    what am i doing wrong?

  13. I’d like to see a write-up on the top ten VPS suppliers to get actual peoples thoughts of their services.

Leave a Reply