Top Banner
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.)
40

A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

Sep 01, 2018

Download

Documents

phungdien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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.)

Page 2: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

Yes, you can download the slides

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

Page 3: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

In The Beginning

Page 4: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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.

Page 5: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

Well, Obviously

Page 6: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 7: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

DNS

You're doing it wrong

Page 8: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 9: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql
Page 10: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 11: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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.

Page 12: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 13: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 14: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

It isn't working!

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

Page 15: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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 ...

Page 16: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 17: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql
Page 18: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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.

Page 19: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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>

Page 20: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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>

Page 21: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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>";

Page 22: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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.

Page 23: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql
Page 24: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 25: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 26: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

A Simple PHP Application

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

<?php phpinfo();?>

Page 27: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

Let us now speak of databases

Page 28: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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.).

Page 29: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 30: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-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

Page 31: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 32: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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)

Page 33: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql
Page 34: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 35: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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.

Page 36: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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.)

Page 37: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql
Page 38: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

It was all for your own good

Page 39: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql

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

Page 40: A Beginner's Guide to Setting Up A Web Hosting Systeminthebox.webmin.com/files/beginners-guide.pdf · server. Uses new-found name ... $ sudo yum install mysql mysql-server php-mysql