a beginner's guide to setting up a web hosting...

40
A Beginner's Guide to Setting Up A Web Hosting System (Or, the design and implementation of a system for the worldwide distribution of pictures of cats.)

Upload: phungdien

Post on 01-Sep-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

A Beginner's Guide to Setting Up A Web Hosting System

(Or, the design and implementation of a system for the worldwide distribution of pictures of cats.)

Yes, you can download the slides

http://inthebox.webmin.com/files/beginners-guide.pdf

In The Beginning

A brief history of a web request

User types in a URLBrowser breaks the request into pieces:1. Protocol (http, ftp, gopher, etc.)2. Domain name or IP address3. Path

If it's a name, asks the resolver (handwaving here, because what the resolver is and how it behaves varies and almost never matters) for the IP.Resolver queries root name servers for the authoritative name server.Uses new-found name server details to find out the IP address.Sends an HTTP request to server.Server returns data, and browser displays it.

Well, Obviously

The LAMP Stack (LAMPBPDAHOOPTCTTMTWAMAP)1. Linux2. Apache3. MySQL4. PHP/Perl/Python/Ruby (that's Ruby with a P)5. BIND6. Postfix/Sendmail7. Dovecot8. And hundreds of other pieces that come together to make

the world a more awesome place

DNS

You're doing it wrong

How DNS Really Works (The handwaving version)1. You "buy" a domain name2. Registrar creates a record in the root name servers3. Clients query root servers, and then your servers (assuming

root servers have accurate information)4. DNS servers along the way cache the information, making

DNS awesomecrazyfast when you do it right

Installing a name serverThere are many name servers, but BIND serves more than 75% of the world's DNS traffic, so it's probably good enough for us. You don't have to run your own name server, but you need to understand how DNS works, no matter what.

Red Hat, CentOS, Fedora, Scientific Linux, etc.$ sudo yum install bind dnsutils

Debian, Ubuntu$ sudo apt-get update$ sudo apt-get install bind9 dnsutils

Configuring BINDAdd a zone (probably in /var/named/chroot/var/named or similar)$ttl 38400@ IN SOA xenhost.virtualmin.com. ( 1244378204 ; serial 10800 ; refresh 3600 ; retry 604800 ; expire 38400 ) ; negative TTLvirtualmin.com. IN NS ns1.virtualmin.com.virtualmin.com. IN NS ns2.virtualmin.com.

Configuring BIND Continued

Add some records

virtualmin.com. IN A 67.228.198.99www.virtualmin.com. IN A 67.228.198.99mail.virtualmin.com. IN A 67.228.198.98@ IN MX 5 mail

Configuring BIND Continued

Check the configuration

# named-checkconf -t <path to chroot>

Restart BIND

RHEL, CentOS, Fedora, etc.# service bind restart

Debian, Ubuntu# /etc/init.d/bind9 restart

It isn't working!

Use whois to make sure you got it right at the root servers. # whois virtualmin.com

HTTP (A lot simpler than DNS, and I can prove it)$ telnet virtualmin.com 80Trying 67.228.198.99...Connected to virtualmin.com (67.228.198.99).Escape character is '^]'.GET / HTTP/1.1host: www.virtualmin.com ...

Installing Apache

Red Hat, CentOS, Fedora, etc.$ sudo yum install httpd Debian, Ubuntu$ sudo apt-get update$ sudo apt-get install apache2 apache2-mpm-prefork

Configuring Apache

Pretend like the single-site mode of Apache does not exist. Always use VirtualHosts, even if you only want one domain, because some day you'll want a second, I guarantee it.

Configuring Apache VirtualHosts

NameVirtualHost 67.228.198.99:80... <VirtualHost 67.228.198.99:80>ServerName virtualmin.comServerAlias www.virtualmin.comDocumentRoot /home/virtualmin/public_htmlScriptAlias /cgi-bin/ /home/virtualmin/cgi-bin/DirectoryIndex index.html index.htm index.php<Directory /home/virtualmin/cgi-bin>allow from all</Directory></VirtualHost>

CGI Scripts

<VirtualHost 67.228.198.99:80>ServerName virtualmin.comServerAlias www.virtualmin.comDocumentRoot /home/virtualmin/public_htmlScriptAlias /cgi-bin/ /home/virtualmin/cgi-bin/DirectoryIndex index.html index.htm index.php<Directory /home/virtualmin/cgi-bin>allow from all</Directory></VirtualHost>

A Simple CGI Application

#! /usr/bin/perluse warnings;use strict;

print "Content-type:text/html\n\n";

print "<html><head><title>Hello World!</title></head>\n\n";print "<body>\n";print "<p>Hello world!</p>\n";print "</body></html>";

What about PHP?

PHP can run as a CGI, so it should already work in ~/cgi-bin, but you pay a performance penalty. So, we can add mod_php or mod_fcgid to the picture to PHP is always running. I recommend mod_fcgid (and suexec), but I'm going to cover mod_php because it's simpler, and this is a beginner's guide. At the end, I'll tell you how to automatically get a working suexec+fcgid environment for PHP, and any other language that works with fcgid.

Installing mod_php

Red Hat, Fedora, CentOS, etc.$ sudo yum install php Debian, Ubuntu$ sudo apt-get update$ sudo apt-get install php5 libapache2-mod-php5

Configuring PHPIt's probably already done for you. The PHP packages on both Red Hat and Debian based systems include a default configuration that'll probably Just Work(tm).

But, just in case:

Red Hat, CentOS, Fedora:Edit /etc/httpd/conf.d/php.conf, remove the # in front of the DirectoryIndex, AddHandler, and AddType directives.

Debian, Ubuntu:$ sudo a2enmod php5

A Simple PHP Application

This goes in ~/public_html, rather than ~/cgi-bin

<?php phpinfo();?>

Let us now speak of databases

MySQL

A database stores data. A relational database stores data and provides mechanisms to find specific pieces of data based on relationships to other data. MySQL also happens to be required for the vast majority of large web applications (Drupal, Joomla, Wordpress, Ruby On Rails, MediaWiki, etc.).

Installing MySQL

Red Hat, CentOS, Fedora, etc.$ sudo yum install mysql mysql-server php-mysql

Debian, Ubuntu$ sudo apt-get update$ sudo apt-get install mysql-server mysql-common mysql-client php5-mysql

Configuring MySQL

Copy an appropriate /etc/my.cnf (if one does not already exist) from the install directory, and set a root password. Don't touch anything else, until you know what you're doing.# mysqladmin -u root password 'newpassword'# sudo /etc/init.d/mysqld restart

Now we're gonna riff on email for a couple bars

Mail is not a single thing

"My email isn't working" is not a sensible thing to say, unless all mail services are not working.Email is made up of:1. Message Transfer Agent/SMTP Server (Sendmail, Postfix,

Exim, QMail)2. IMAP/POP Server (Dovecot, Cyrus, Courier, etc.)3. Delivery Agent (procmail, binmail, maildrop, postdrop, etc.)4. Optional Spam and Anti-virus servers (SpamAssassin,

ClamAV, etc.)5. Mail Retrieval Agent (i.e. "mail client", Outlook (lookout!),

Thunderbird, RoundCube, Usermin, Mail.app)

Installing a Mail Server (all the pieces)Make sure your system has a fully qualified domain name! (I'm serious.)

Red Hat, Fedora, CentOS, etc.$ sudo yum install postfix procmail dovecot spamassassinThere are no ClamAV packages in RHEL/CentOS. Debian, Ubuntu$ sudo apt-get update$ sudo apt-get install postfix postfix-pcre procmail dovecot spamassassin spamc clamav-base clamav-daemon clamav clamav-freshclam clamav-docs

Configuring a Mail Server

You don't (much). The mail stack packages on the distributions we're talking about almost work right out of the box for simple deployments. If you find yourself following a big complicated tutorial, the tutorial is doing it wrong (or, at least, wrong for you at this time). Handwaving, because there's no way I can cover all of the details of this.

The Promised Land

(Or, the point in our story in which I show you how to do all of this, and a whole lot more, with a single command.)

It was all for your own good

The command

Go here: http://www.virtualmin.com/download

Read the page. Download the Virtualmin GPL install script.

Run it like this:

# /bin/sh install.sh