📄 linux-httpd.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><HTML><HEAD><TITLE>Josh's Linux Guide - Setting Up a Webserver</TITLE> <META NAME="Author" CONTENT="Joshua Go"> <META NAME="Description" CONTENT="How to set up a webserver using the Apache HTTP server and Linux."> <META NAME="Keywords" CONTENT="Joshua, Go, webserver, httpd, apache, Apache, HTTP"></HEAD><LINK REL="stylesheet" TYPE="text/css" HREF="default.css"><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#7F007F"><H2><A NAME="0">Setting Up a Webserver</A></H2><B><FONT SIZE="-1">Last updated: November 1, 1999</FONT></B><BR><B><FONT SIZE="-1">Development stage: Alpha</FONT></B><BR><P>The World Wide Web is probably the primary reason of the Internet'sexplosive growth as a popular communications medium. So why not add tothe wealth by running your own webserver? The most popular webserverby far is <A HREF="http://www.apache.org/" TARGET="_top">Apache</A>,which already comes with many Linux distributions.</P><P>Chances are that you already have Apache installed, so you may skip thedownloading process. Of course, downloading and compiling a new Apachegives you the advantage of knowing how to do it. The process is quitesimple, even the configuration.</P><HR><P>This document is in alpha status, so let me know if it works for you soI can update its status. That way I'll know if it works.</P><HR><!-- Indexes are made manually, and are recommended for new documents. --><!-- The format I use for the index should be consistent on all files. --><!-- The use of anchors is encouraged so people won't have to read theentire document. It's also helpful for being able to jump. --><OL><LI><A HREF="#remove">Removing Old Apache</A><LI><A HREF="#download">Download</A><LI><A HREF="#compile">Compile and Install</A><LI><A HREF="#config">Configuration</A> <UL> <LI><A HREF="#location">Location of Configuration Files on Distributions</A> <LI><A HREF="#virtual">Virtual Hosts</A> <LI><A HREF="#etceehosts">Setup of /etc/hosts</A> </UL><LI><A HREF="#errors">Error Messages</A><LI><A HREF="#content">Creating Content</A><LI><A HREF="#notes">Author's Notes</A></OL><HR><A NAME="remove"></A><H3><A NAME="1">Removing Old Apache</A></H3><P>Should you choose to remove any existing Apache, try typing <B><TT>rpm-e apache</TT></B>, if you're using Red Hat (or maybe another RPM-baseddistribution).</P><P>In Debian, use the <B><TT>apt-get remove apache</TT></B> command.</P><P>Let me know how to remove the default Apache package for otherdistributions so I may include them here. I don't want to lookignorant for a long time.</P><A NAME="download"></A><H3><A NAME="2">Download</A></H3><P>Apache is available all around the world in many FTP sites. There's amirror list at the Apache website. As of now, Apache is at version 1.3.9,and at the rate it constantly develops, this document will probablyfall behind when a new release comes out.</P><P>You can find a list of where you can download Apache at <AHREF="http://www.apache.org/dyn/closer.cgi"TARGET="_top">http://www.apache.org/dyn/closer.cgi</A>. The filenamefor the file you're looking for goes something like"apache_1.3.9.tar.gz".</P><A NAME="compile"></A><H3><A NAME="3">Compile and Install</A></H3><P>You have downloaded Apache. If it's not already, put it in<TT>/usr/local/src/</TT> and untar (<B><TT>tar -zxvf<apache_1.3.9.tar.gz></TT></B>) it. It should extract to somedirectory called "apache-1.3.9" or similar, depending onwhat version number you've downloaded. Now move into the apachedirectory by typing <B><TT>cd apache-1.1.9</TT></B> (or whateverversion you've got).</P><P>Basically you can get Apache compiled by typing <B><TT>./configure ;make</TT></B> in the Apache directory. <B><TT>make install</TT></B> willinstall it to /usr/local/apache by default. In /usr/local/apache/there will be many subdirectories containing various files that youmight want to tinker with.</P><P>At the least, you should make a symbolic link from<TT>/usr/sbin/httpd</TT> to <TT>/usr/local/apache/bin/httpd</TT>(assuming you installed Apache to the default location). Or, you canadd <TT>/usr/local/apache/bin/</TT> to root or user paths from<TT>~root/.bashrc</TT> and <TT>/etc/profile</TT>, respectively.</P><A NAME="config"></A><H3><A NAME="4">Configuration</A></H3><P>Since you've now got Apache installed and ready to go, it's time toconfigure it. Move on down to <TT>/usr/local/etc/httpd/conf/</TT> andcopy all the "*.conf.default" files to their"*.conf" selves. That means you would copy<TT>httpd.conf.default</TT> to <TT>httpd.conf</TT>,<TT>srm.conf.default</TT> to <TT>srm.conf</TT>, and<TT>access.conf.default</TT> to <TT>access.conf</TT>. Those are allthe files that you need to copy. Edit "httpd.conf" with yourfavorite text editor and uncomment the ServerName line. Put in yourhostname or IP address in place of the fake one in the commented line,which probably changes each time you log on. For ServerAdmin, put in<I>your</I> e-mail address so that people visiting your webserver cane-mail you about any problems that arise. What you fill in is prettystraightforward.</P><P>Reading the comments and examples in the *.conf files for Apachewill help you if you need to do any "advanced" tasks such asvirtual domains or extra MIME types (if you don't know what these are,don't worry about them yet).</P><A NAME="location"></A><H4><A NAME="5">Location of Configuration Files on Distributions</A></H4><P>If you're using Red Hat Linux, you should have the configurationfiles in <TT>/etc/httpd/conf/</TT>, if you had Apache installedalready.</P><P>On SuSE 5.2, 5.3 and 6.0 the config files for Apache are located in<TT>/etc/httpd/</TT>. Thanks to Uwe Hermann (<AHREF="mailto:uh1763@bingo.baynet.de">uh1763@bingo.baynet.de</A>) forthis information.</P><P>Thomas Smith writes, "<I>On my Debian 2.1 system, theconfiguration files are in /etc/apache, and are httpd.conf, srm.conf,mime.types, and cron.conf. I would guess that not all of these areessential, but they are the default. The default directory for webpages is /var/www/.</I>"</P><P>For other distributions, I'm not sure. Please let me know if youcan provide this information. You'll probably want to look in the<TT>/etc/</TT> directory in the meantime.</P><A NAME="virtual"></A><H4><A NAME="6">Virtual Hosts</A></H4><P>Many people need to run multiple web servers on one machine insteadof one webserver per Linux machine. First you'll have to get ahostname assigned to the webserver, which can be a subdomain(jgo.local.net if you're on the local.net domain) or a whole newdomain registered through InterNIC (such as dummy.com).</P><P>Once you get past all the domain registration stuff and you've gotmany hostnames assigned to the same IP address, or if the machine isrunning under multiple IP addresses and multiple hostnames, you'reready to set up for virtual hosts.</P><P>Start out by editing <TT>/usr/local/apache/conf/httpd.conf</TT>. Ifyou have Red Hat Linux with Apache installed already, the file is<TT>/etc/httpd/conf/httpd.conf</TT>. You'll see an example shown,looking like this:</P><PRE># VirtualHost: Allows the daemon to respond to requests for more than one# server address, if your server machine is configured to accept IP packets# for multiple addresses. This can be accomplished with the ifconfig # alias flag, or through kernel patches like VIF.# Any httpd.conf or srm.conf directive may go into a VirtualHost command.# See alto the BindAddress entry. #<VirtualHost host.some_domain.com>#ServerAdmin webmaster@host.some_domain.com#DocumentRoot /www/docs/host.some_domain.com#ServerName host.some_domain.com#ErrorLog logs/host.some_domain.com-error_log#TransferLog logs/host.some_domain.com-access_log#<VirtualHost></PRE><P>That's commented out because your machine most probably does notown the hostname <TT>host.some_domain.com</TT> and therefore won'taccept requests for it.</P><P>The basic way that this works is when someone requests a page on,say, jgo.local.net, it won't go to squeeze.local.net, the originalhostname of the machine. Although jgo.local.net <I>is</I> an alias forsqueeze.local.net, somebody asking for http://jgo.local.net won't getthe front page to http://squeeze.local.net because it's specified in<TT>httpd.conf</TT> to serve out <TT>/home/jgo/index.html</TT> when arequest for jgo.local.net is given through HTTP. When a request for205.136.38.27 is given, however, it won't serve out<TT>/home/jgo/index.html</TT> because only the requests forhttp://jgo.local.net will give the web surfer<TT>/home/jgo/index.html</TT>. Some older browsers don't supportvirtual hosts, such as MS Internet Explorer 2.0, which came with earlyversions of Windows 95.</P><P>You can assign as many virtual hosts to your machine as you want,especially if the machine is a top-level domain such aslocal.net. That way you can assign the hostnames jgo.local.net,something.local.net, one.local.net, two.local.net, three.local.net,and anything else you might be able to think of as a hostname. Justkeep in mind that you should make sure the server can handle the loadof all those domains.</P><P>To add subdomains, you need to have an understanding of <AHREF="linux-nameserver.html">BIND</A>, the nameserver. Your domainmust be registered with InterNIC, and the nameserver is in charge ofgetting subdomains set up. It sounds complicated, but it makes a wholelot more sense once you figure it out.</P><A NAME="etceehosts"></A><H4><A NAME="7">Setup of /etc/hosts</A></H4><P>My observations about the setup of /etc/hosts is that each hostnamehas to be part of a higher-level domain; for example, if I wanted toname my machine <TT>neophobia</TT>, I'd either have to make thehigher-level domain "localdomain" or"dyn.ml.org". Here's what each entry would look like:</P><PRE>127.0.0.1 localhost localhost.localdomain</PRE><P>and:</P><PRE>0.0.0.0 neophobia neophobia.dyn.ml.org</PRE><P>People with dynamic IP addresses (most dialup PPP users) can justuse the IP address "0.0.0.0" to represent their InternetIP. So, this is what my hosts file looks like:</P><PRE>127.0.0.1 localhost localhost.localdomain0.0.0.0 neophobia neophobia.dyn.ml.org</PRE><P>After you're done with all that you wanted to do, try to starthttpd by typing <B><TT>httpd</TT></B> as root. It is common and normalfor you to have some errors, so feel free to e-mail me concerning anyother errors that come up after you execute the httpd binary.</P><A NAME="errors"></A><H3><A NAME="8">Error Messages</A></H3><P>When you have <B>httpd</B> already running, the error message itgives will be <TT>bind: Address already in use, httpd: could not bindto port 80</TT> or something similar. To fix this, do <TT>killallhttpd</TT> and then <TT>httpd</TT> to restart it. Make sure you bindit to the correct hostname/IP address (read the paragraphs above).</P><A NAME="content"></A><H3><A NAME="9">Creating Content</A></H3><P>By default you'll have to edit the HTML files in<TT>/usr/local/apache/htdocs/</TT>. The directory for CGI scripts isin <TT>/usr/local/apache/cgi-bin/</TT>.</P><P>On a default Red Hat installation you'll probably find what youneed to edit in <TT>/home/httpd/html/</TT>, along with somedirectories. The CGI-bin will be in <TT>/home/httpd/cgi-bin/</TT>.</P><P>The htdocs-directory (DocumentRoot) is<TT>/usr/local/httpd/htdocs/</TT> on SuSE 5.2, 5.3, and 6.0, accordingto Uwe Hermann (<AHREF="mailto:uh1763@bingo.baynet.de">uh1763@bingo.baynet.de</A>).</P><P>Let me know of the filesystem layout for other distributions. Idon't have immediate access to other distributions.</P><A NAME="notes"></A><H3><A NAME="10">Author's Notes</A></H3><P>There are more complex things you can do with Apache, and here Ionly cover the basics. There are special modules that come with Apachewhich I have little experience with, but I just want to let you knowthat they're available. Read the INSTALL file for instructions onthis.</P><HR><P>Questions, comments, and/or suggestions? You can e-mail me at <AHREF="mailto:jgo@local.net">jgo@local.net</A>.</P><HR><P><B><FONT SIZE="-1">Copyright © 1997-1999 <AHREF="mailto:jgo@local.net">Joshua Go (jgo@local.net)</A>. Allrights reserved. Permission to use, distribute, and copy this document ishereby granted. You may modify this document as long as credit to me isgiven.</FONT></B></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -