SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Downloaden Sie, um offline zu lesen
12/20/12                       Apache Web Server Tutorial for Linux




    Home Tutorials Forum Blog Advertising Links Contact About                                                                       Search



       Apache Web Server Tutorial for Linux
       Written by Tony Bhimani
       Posted on August 2, 2006

       Requirements
       *nix based Operating System
       Apache HTTP Server
       mod_ssl Apache Interface
       PHP Scripting Language

       Contents
       Introduction
       Installing Apache HTTP Web Server
       Installing mod_ssl for Secure Sockets Layer (SSL) Support
       Installing PHP for Dynamic Web Pages
       Setting Apache to Start on Bootup with chkconfig
       C onfiguring Apache Server Settings (httpd.conf)
       C reating the Web Site Directory Structure
       C reating VirtualHost C onfiguration Files
       Starting Apache and Viewing the Web Site
       Other Things to Know for Using Apache
       C onclusion



       Introduction
       The Apache Web Server is one of the most popular open-source HTTP Servers that exists today. It's a powerful, secure, and
       fully featured web server that can be found hosting anything from personal web sites to corporate domains. If you have a DSL
       or C able connection with a static IP then you can use the Apache web server to host your web site (as long as your ISP
       doesn't have a problem with you running a server on their network). This tutorial will teach you how to set up your own
       Apache web server on Linux so you can host your own web sites either at home or work. You'll learn how to install the Apache
       HTP Server with SSL and PHP support, use chkconfig to set Apache to auto-start on boot, edit the Apache httpd.conf
       configuration file for your server settings, create a directory structure for your web site, create the VirtualHost configuration
       file for your web site, start the Apache service, verify your site is working, go over the init script options, and finally show you
       some additional options and settings for Apache such as graceful restarts, htaccess usage, user name and password directory
       protection, and custom error pages. The two things I won't be covering are setting up DNS for your domain and configuring
       your firewall to open ports 80 and 443 for HTTP and SSL respectively, however you can read my Bind DNS tutorial and
       iptables firewall tutorial for information on how to do it. I'll be using C entOS 4.2 and Apache 2.x will be installed using yum
       instead of source. Let's get started.



       Installing Apache HTTP Web Server
       We'll be using yum to download and install the Apache RPM's from the C entOS repository. Apache shouldn't be loaded on your
       server unless you selected it during the operating system installation process. If you know you have Apache installed then you
       can skip this section, but if it's not or you're not sure then you should perform the following tests to see if the Apache web
       server is on your system. To do this we'll use the which command to look for the httpd binary in the environment variable
       PATH.

       which httpd




       If you see something similar to the above image then the Apache binary doesn't seem to be found on the server. Our next
       step is to install Apache by using yum. Issue the next command.

       yum install httpd




w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                       1/18
12/20/12                       Apache Web Server Tutorial for Linux




       Two RPM packages will be downloaded from the C entOS repository (httpd-2.0.52-22.ent.centos4.i386.rpm and httpd-suexec-
       2.0.52-22.ent.centos4.i386.rpm) and installed automatically. If everything goes well you should now have the Apache web
       server on your system. You might also want to load the Apache documentation so you'll have the man pages available. This is
       optional but highly recommended. Use yum again and download the Apache manual (yum install httpd-manual). Next we'll
       use yum to install mod_ssl for Secure Sockets Layer (SSL) support.



       Installing mod_ssl for Secure Sockets Layer (SSL) Support
       Secure Sockets Layer, also known as SSL for short, provides encrypted commnications between hosts. SSL can be found in e-
       commerce web sites and any other site that requires sensitive information being transmitted protected from eavesdropping. In
       a nutshell, the hosts negotiate a protocol they both can understand and then switch to secure communications. The data
       passed back and forth between the hosts is encrypted with a 128-bit key (hence 128-bit SSL). In a typical SSL setup, the web
       server has SSL support enabled and a certificate that verifies the company's identity. These certificates are initially generated
       as C ertificate Signing Requests (C SR) by the server's administrator. The C SR is then signed by a third party, a C ertificate
       Authority (C A), that validates the company's identity and makes the C SR into a full certificate. There are many C A's like
       VeriSign, Thawte, GeoTrust, InstantSSL (aka The C omodo Group), FreeSSL, and many others that sign C SR's. Prices vary
       from company to company, but as long as they support the mainstream browsers you shouldn't run into any problems. If
       you'd like to learn more about Secure Sockets Layer then you should check out the Wikipedia Transport Layer Security page.

       You may be wondering where does mod_ssl fits into all of this? On Linux and Unix distributions, there is a software package
       called OpenSSL that performs all the cryptography and mod_ssl is the Apache interface to OpenSSL. It's mod_ssl that makes it
       possible for Apache to have SSL capability. We'll now install the mod_ssl RPM (mod_ssl-2.0.52-22.ent.centos4.i386.rpm) for
       Apache using yum.

       yum install mod_ssl




w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                    2/18
12/20/12                       Apache Web Server Tutorial for Linux




       mod_ssl should now be installed. If you didnt have OpenSSL installed then it should have been picked up as a dependency and
       was installed as well. We have one more package to install and that's the PHP scripting language.



       Installing PHP for Dynamic Web Pages
       PHP is a scripting language for creating dynamic web pages. PHP allows you to include code in your web pages to be
       processed server-side and the resulting HTML is sent to the user's web browser. With PHP you can add database access, read
       and write files to the server's filesystem, generate dynamic graphics like graphs and security image codes, and create security
       control mechanisms for fencing off parts of your web site. PHP has many other uses and that's what makes it a powerful tool
       for creating dynamic web pages.

       If you're not a programmer or will only he hosting a static HTML web pages, then PHP won't be of any use to you. However, if
       you want to learn PHP then you should install the PHP modules for Apache. We'll use yum one last time to install PHP on our
       server. There are many PHP RPM modules, one being the actually binary and modules for Apache and the rest are support
       features for MySQL, XML, ODBC , and others to tie those subsystems into PHP. It's a good idea to install all the PHP modules
       since you never know what you'll need now or in the future. Now we'll use yum to install PHP.

       yum install php*




w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                  3/18
12/20/12                       Apache Web Server Tutorial for Linux




       As you can see above, we used a wildcard to tell yum to install anything starting with the word 'php'. On my server yum
       downloaded and installed fifteen packages, however yours may be different depending on its configuration. Now that PHP is
       installed we can move on to setting the Apache service to automatically start on bootup or in the event of a reboot.



       Setting Apache to Start on Bootup with chkconfig
       The Apache web server will need to be set to autostart when the server boots. The Apache RPM didn't set this up for us so
       we'll have to do it ourselves using chkconfig (Note: you can also use setup to turn on the Apache service). We're going to
       have Apache start on run levels 2, 3, and 5.

       chkconfig --level 235 httpd on
       chkconfig --list httpd




       The second chkconfig command lists the run levels Apache is configured to start on. If you don't have X Windows installed
       then you may want to omit run level 5 (Multi-User Mode - boot up in X Windows). To learn more about Linux Run Levels you
       may want to check out this page on NetworkC lue. Now that Apache is installed and set to start up we'll move on to configuring
       the Apache web server by editing httpd.conf, the Apache server configuration file.



       Configuring Apache Server Settings (httpd.conf)
       Apache's main configuration file is called httpd.conf and is located in /etc/httpd/conf/. The default httpd.conf will work without
       any changes, however we want to customize Apache a little bit. Our main focus is to setup the use of Virtual Hosts so we can
       run as many web sites as we want using a single IP address. Also, we want to simplify the management of our Virtual Hosts
       without cluttering httpd.conf with our entries. Before we get to any of that we will first configure our Apache web server.

       I will make the assumption that you have a single WAN based IP address. For this example I'm using 192.168.1.210 with a
       hostname of node2.centos (yes, it's a LAN IP but pretend it's WAN). You can use your IP or hostname from /etc/hosts, but I
       suggest using your hostname because if you ever change IP's all you have to do is update /etc/hosts with the new address. If
       you do use an IP then you'll have to change all instances of it in httpd.conf. If you don't know what your IP address is or never
       set up /etc/hosts, you can find your address by using ifconfig (look at the number to the right of 'inet addr:').

       We'll start with opening httpd.conf in the nano text editor, but before that you should make a copy of your existing httpd.conf
       file. If you encounter any problems you'll still have the original to fall back to.

       cd /etc/httpd/conf
       cp httpd.conf httpd.conf.old
       nano httpd.conf

       Apache's httpd.conf is filled with many helpful comments to tell you what each configuration directive does. Scroll down to line
       133 as shown in the picture below. FYI, if you ever want to know what line number you're on in nano, press CTRL-C and nano
       will show you. We're looking for the line that says 'Listen 80'. We won't be changing this directive since we want Apache listen
       on all IP addresses set up on the server. I wanted to show you where to change it if you needed to bind Apache to only one
       address.




w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                     4/18
12/20/12                       Apache Web Server Tutorial for Linux




       Move on down to line number 235 where it says 'ServerAdmin root@localhost'. The Apache ServerAdmin directive is for the
       administrator's email address of the server. End users encountering any problems with the server would use this email
       address to notify the sysadmin. C omment out the existing ServerAdmin line with a pound symbol and enter a new line below
       with your email address. For this example I used admin@node2.centos.

       #ServerAdmin root@localhost
       ServerAdmin admin@your-domain.com




       Below the ServerAdmin directive is ServerName. The Apache ServerName directive is for Apache to identify itself which is
       typically the hostname of the server. You'll want to specify your hostname or IP address instead of a valid DNS name
       especially if you're hosting many web sites. There are some people that may disagree with this method but I think it's better to
       keep all the generic information in httpd.conf and use specifics in the VirtualHost configuration files. Add a new blank line
       below the commented ServerName directive and add your hostname or IP. For this server I used my hostname node2.centos.

       #ServerName new.host.name:80
w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                   5/18
12/20/12                   Apache Web Server Tutorial for Linux
       ServerName yourhostname




       The next directive we'll edit is very close to the end of the file. Scroll all the way down to line number 1005 (tip: use the Page
       Down key on your keyboard) and look for '#NameVirtualHost *:80'. The NameVirtualHost directive tells Apache that we want
       to use name-based virtual hosting, or in simpler terms, a bunch of web sites all using the same IP address. Virtual hosting is
       made possible because when a web browser goes to a site, for example www.xenocafe.com, the web site address is passed as
       part of the HTTP header (Host: www.xenocafe.com). This allows Apache to distinguish between different hosts sharing the
       same IP address. Add a couple blank lines underneath '#NameVirtualHost *:80' and on the line right below put
       NameVirtualHost and your hostname or IP address followed by a :80. The ':80' means we're using name-based virtual hosting
       for the HTTP protocol. SSL will be configured through VirtualHost configuration files due to its nature. You'll learn why when we
       create our virtual hosts. For this server I used my hostname node2.centos.

       #NameVirtualHost *:80
       NameVirtualHost yourhostname:80



                                                                                                                                            6/18
We're almost done. The last thing we need to do is create a default virtual host to respond to requests when someone vists our
IP address and not our domain name. You may or may not want to add this virtual host and it's totally up to you, but I
personally don't want anyone going directly to my IP address. I prefer they visit my web site by name only. A VirtualHost
entry can have many directives which I'll explain later, but what you need to know for now is the example I provide responds
by IP address visits. At the very end of the file there is a line that instructs Apache to load any configuration files found in the
/etc/httpd/conf/vhosts/ directory (which we'll create later). This is my way of keeping the virtual hosts separate from
httpd.conf and most importantly, making hosts easier to manage.

<itaHs yuhsnm:0
 Vrulot orotae8>
   SreAmnyuyu-oancm
   evrdi o@ordmi.o
   SreNm yu_pades
   evrae ori_drs
   DcmnRo /w
   ouetot ww
   Erro lg/ro_o
   roLg oserrlg
   CsoLglg/ceslgcmie
   utmo osacs_o obnd
<Vrulot
 /itaHs>

#icueVrulot cni fls
  nld itaHss ofg ie
Icuecn/hss*cn
 nld ofvot/.of
12/20/12                       Apache Web Server Tutorial for Linux




       We're done editing httpd.conf. Let's save our changes (CTRL-O) and exit nano (CTRL-X). Now we'll move on to creating a web
       site directory structure and user account for SSH access and S/FTP file uploads.



       Creating the Web Site Directory Structure
       Our web site directory schema should be simple yet structured. The common approach would be to use /home as the root but
       we won't be doing that. Our web site parent root will be /www and from within it create a directory for each domain we'll be
       hosting. Within each domain directory there will be a set of common directories (html, html/cgi-bin, databases, and logs).
       Reading what I wrote doesn't make much sense so here it is visually. For this example and the rest of the tutorial, we'll say we
       acquired the domain your-domain.com and will be configuring our server for it.

       /w
        ww             (otfralhse dmis
                       ro o l otd oan)
       /w/ordmi
        wwyu-oan       (oandrcoy
                       dmi ietr)
       /w/ordmi/tl
        wwyu-oanhm     (ietr fryu wbst fls
                       drcoy o or e ie ie)
       /w/ordmi/tlcibn(G drcoyfreeuigPr srps
        wwyu-oanhm/g-i CI ietr o xctn el cit)
       /w/ordmi/aaae
        wwyu-oandtbss  (aaae frti wbst soe o aprst bss
                       dtbss o hs e ie trd n  e ie ai)
       /w/ordmi/os
        wwyu-oanlg     (e st acs aderrlg aesoe hr)
                       wb ie ces n ro os r trd ee

       The databases directory is optional. If you read my mapping mysql databases tutorial then you can migrate your databases
       outside the default /var/lib/mysql into the databases directory. This will allow you to store your databases on a per site basis
       in the web site's home directory.

       Creating the Directory Root (one-time only)

       We'll start creating our web site directory structure by making /www. C reating this directory is a one-time process and will be
       home to all our domains we're hosting (any domains we add will be stored in the /www directory).

        mkdir /www
        chown root.root /www
w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                   8/18
12/20/12                 Apache Web Server Tutorial for Linux
        chown root.root /www




       After we create the directory we chown it to uid:root gid:root.

       Creating Web Site Domain Directories

       For each web site you're hosting you'll create a directory structure within the parent /www. The directory should be named the
       same as the domain name with or without the TLD (.com, .net, etc). I'll let you decide which way you want your directories to
       be named, but I tend to prefer them without the TLD. Now we'll create our domain's directories.

       cd /www
       mkdir your-domain
       cd your-domain
       mkdir html
       mkdir html/cgi-bin
       mkdir databases
       mkdir logs




       Now that the directory structure is created we'll need to add a system user that owns the directory base and can log in to
       upload files.

       Adding a Linux User Account for the Web Site

       The user account will be able to FTP or Secure FTP (SFTP) into the server to upload and download web site content files like
       HTML, C GI scripts, and PHP pages. This user account will have ownership of the domain's directory base and be locked into it.
       The user name we'll assign will be the same as the domain name (your-domain) to make it easier to distinguish between it and
       the other Linux accounts, however you can use any name you want for your account. One thing to keep in mind is when you
       create this account you'll have to make a choice whether this account will be granted SSH access. If you want to disallow SSH
       access then you'll need to change the shell to /sbin/nologin. SSH login attempts will fail when you use /sbin/nologin as the
       account's shell (valid login credentials will be accepted but the connection will be terminated). To create the web site system
       account, use one of the following commands.

       useradd -d /www/your-domain/ domain_user
       or
       useradd -d /www/your-domain/ -s /sbin/nologin domain_user




        The first command will set the account's home directory (-d path) to /www/your-domain and creates a new system account
        with the user name of domain_user (last parameter of the command). The shell will be set to the default, which is typically
w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php
12/20/12                       Apache Web Server Tutorial for Linux
       with the user name of domain_user (last parameter of the command). The shell will be set to the default, which is typically
       bash unless you changed it. The second command is identical to the first except that the shell (-s shell) is set to /sbin/nologin
       to prevent SSH logins.

       After the account has been created you'll need to set a password using the passwd command.

       passwd domain_user
       [enter password]
       [confirm password]




       Now that the system account is ready we'll move on to changing the directory base ownership.

       Changing Web Site Directory Ownership & Permissions

       We've created the directories, added the account, the only thing left to do is change the ownership of the directories to the
       new user account. Right now the directories are owned by root and if you were to try using FTP to upload some files you'd get
       the dreaded Permission Denied error. To change ownership of the domain directory and all its subdirectories we'll use the
       chown command with the -R flag for recursive.

       cd /www
       chown -R domain_user:domain_user your-domain




       If you created the databases directory then you'll need to change the owner to the mysql user otherwise MySQL won't be able
       to write to the database files. All directories, subdirectories, and files starting from databases should be owned by the mysql
       user (or whatever user account you have the MySQL server running as).

       cd your-domain
       chown -R mysql:mysql databases




w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                    10/18
That's it for creating our directory structure. If you have more domains to add then repeat the steps we went through except
       for the first one of creating the /www directory root. Next we'll create the VirtualHost configuration files to let Apache know the
       details of our web site.



       Creating VirtualHost Configuration Files
       A VirtualHost configuration file tells Apache the specifics about your web site. It contains information like what IP and port
       number your site runs on, the server name and any aliases, the local filesystem path (or document root) of your web pages,
       location of your cgi-bin, file names and paths of your access and error logs, custom error documents for HTTP errors like 404
       File Not Found, and can have many other items. For every web site you want Apache to host you must create a virtual host
       configuration. You've already gotten a taste of a virtual host config when we added the default VirtualHost for handling IP
       address requests in httpd.conf, so what we're going to be doing now is nothing new. I'm now going to show you an easy way
       of managing all your virtual hosts.

       The VirtualHost Configuration File Directory (/etc/httpd/conf/vhosts)

       When I first learned Linux from a friend some years ago, he showed me how to configure Apache so I could set up my web
       server. I looked at his httpd.conf and he had all his virtual hosts in there, so I figured there was nothing unusual about that as
       I'm sure there are many people that did the same thing. He hosted a lot of domains for friends, his business, and his own
       stuff, and I noticed that trying to locate a web site in his massive httpd.conf file was nothing short of a headache. Although I
       didn't doubt his knowledge of Apache I kept thinking there had to be an easier way of managing virtual hosts. Through reading
       the online Apache Web Server documentation I came across the Include directive.

       The Include directive, much like in C /C ++, PHP, and other languages, allows you to insert the contents of another file into the
       calling file. However, in the case of Apache, the contents of a single file or of several files located in a directory that match the
       Include statement would be included. If you remember when we were editing httpd.conf, at the very end we added the line
       'Include conf/vhosts/*.conf'. That line tells Apache to look in /etc/httpd/conf/vhosts for any *.conf files and include their
       contents as part of httpd.conf. This allows us to separate out our VirtualHost definitions as their own unique files, on a per
       domain basis, from the main httpd.conf file. This makes administration easy for several reasons. Finding a virtual host is easy
       because all you do is look in the vhosts directory for its file, disabling a domain is as simple as renaming the file so it doesn't
       end in .conf, and adding new domains is accomplished by creating a new virtual host file (copy an exisiting file and edit the
       changes). Include makes virtual host management a piece of cake.

       We'll now create the /etc/httpd/conf/vhosts directory (this is a one-time process).

       cd /etc/httpd/conf
       mkdir vhosts
       cd vhosts




       We can start creating VirtualHost configuration files now. I'll show you how to create one for the HTTP protocol as well as one
       for Secure Sockets Layer (SSL).

       Creating a HTTP Virtual Host

       When creating virtual host config files we should use a good naming convention that's as descriptive as possible. What's
       worked for me is the format of host.domain.tld.port.conf or in simple terms your-domain.com.80.conf. You may have noticed I
       excluded host from the file name. When you visit a domain such as redhat.com, or www.redhat.com, they most likely take you
       to the same web site, so www as a host can be excluded. The only time I use host as part of the file name is when I am
       adding a subdomain such as forum.example.com or blog.example.com and so forth. Let's create the HTTP virtual host file.

       nano your-domain.com.80.conf

       Type in or copy/paste the configuration data below. Be sure to replace yourhostname with your system's hostname or IP
       address, ServerAdmin with your email address, ServerName and ServerAlias with your real domain name, and your-domain
       with the directory name you created earlier for your web site. When you're done, save your changes (CTRL-O) and exit nano
       (CTRL-X).

       <itaHs yuhsnm:0
        Vrulot orotae8>
          SreAmnamnyu-oancm
          evrdi di@ordmi.o
          SreNm yu-oancm
          evrae ordmi.o
w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                   11/18
12/20/12                       Apache Web Server Tutorial for Linux
          SreNm yu-oancm
          evrae ordmi.o
          SreAiswwyu-oancm
          evrla w.ordmi.o
          DcmnRo /w/ordmi/tl
          ouetot wwyu-oanhm
          SrpAis/g-i//w/ordmi/tlcibn
          citla cibn wwyu-oanhm/g-i/
          Erro /w/ordmi/oserrlg
          roLg wwyu-oanlg/ro_o
          CsoLg/w/ordmi/osacs_o cmie
          utmo wwyu-oanlg/ceslg obnd
       <Vrulot
        /itaHs>




       I'll clarify what each line does in the virtual host configuration file between the <VirtualHost></VirtualHost> tags.

                    ServerAdmin is used to specify the server administrator's email address. User's will see this address on server
                    generated error pages.
                    ServerName is the name of the server or DNS name. Apache will match this name against the HTTP host header
                    sent by the user's browser. If it's a match then this is the configuration data used to process the request.
                    ServerAlias is identical to ServerName except ServerName is used once and ServerAlias can be used many
                    times. If your web site will respond to more than one DNS name, make sure you use one ServerName directive
                    and all other names are specified using ServerAlias. DNS names can be separated by commas for a single
                    ServerAlias directive (ServerAlias dns_name1,dns_name2,dns_name3) or you can use many ServerAlias directives
                    for each DNS name placed on a line each their own.
                    DocumentRoot defines the file system path to where the web page files are located. This is the same directory
                    where you will place your HTML, PHP, and image files. For our setup, this is the /www/your-domain/html directory.
                    ScriptAlias defines the directory for executing cgi scripts, commonly known as cgi-bin. It takes two parameters
                    with the first being the cgi-bin directory from your web site root path and the second being the full physical
                    directory path on the filesystem. You can have as many cgi-cin directories as you need and they don't need to be
                    called cgi-bin.
                    ErrorLog defines the file to log any errors Apache encounters. Errors can include PHP script failures and files that
                    don't exist.
                    CustomLog defines the file to log any requests to the server. Requests include accessing pages, images, and
                    other files like robots.txt. The second parameter sets the log entry format. We use the combined format since it is
                    used by many Web Server Log Analysis scripts like Awstats.

       You should read the online Apache 2.0 documentation for more information on these directives as well as other Apache web
       server features.

       Creating a SSL Virtual Host

       When we installed mod_ssl, a file called ssl.conf was written to /etc/httpd/conf.d that contains basic SSL configuration
       information. We're going to use this as our template for adding a SSL based virtual host. What we'll do is make a copy of it to
       our vhosts directory and remove the non-website specific information since it should only be loaded once by Apache. This non-
       website specific information pertains to loading the mod_ssl Apache module among other things while everything else in the
       file relates to configuring the SSL web site (what we're interested in).

       Let's make a copy of ssl.conf to the vhosts directory which you should still be in (cd /etc/httpd/conf/vhosts). Following the
       same format for our virtual host config files (host.domain.tld.port.conf) as before, we will be naming our SSL virtual host file
       as your-domain.com.443.conf. The port number for SSL is 443 so we'll use that as opposed to port 80 when we created the
       HTTP VirtualHost. After that we'l open the config file in nano so we can begin editing it.

       cp /etc/httpd/conf.d/ssl.conf ./your-domain.com.443.conf
       nano your-domain.com.443.conf




w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                12/18
12/20/12                       Apache Web Server Tutorial for Linux




       Now comes the fun part. We need to delete a good portion of this file starting from the top and all the way down to the part
       that says '### SSL Virtual Host C ontext' (roughly 83 lines total). Instead of using the DEL key, you can use CTRL-K to cut the
       text line by line in nano. Starting from the top of the file, remove the lines of text until you reach the blank line above '###
       SSL Virtual Host C ontext'. Your file should look like the image below when you're done.




w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                              13/18
12/20/12                   Apache Web Server Tutorial for Linux



       Next we'll edit the default web site specific directives to mold them to our web site. They're actually the same directives we
       used when creating our HTTP virtual host, so you can see how easy this will be. I should point out that we have been using our
       hostname for the VirtualHost tag, but in the case of SSL I'd advise against unless you want to set up each host and their
       binding IP address in /etc/hosts (it's a good idea if you have many IP's). Secure Sockets Layer doesn't work at the Application
       layer of the OSI model like HTTP does, so the Host part of the HTTP header doesn't apply. So in regards to name-based virtual
       hosting with SSL, it's non-existent. Replace '_default_' with your IP address when editing your-domain.com.443.conf. The
       same rules apply as before with ServerName, ServerAlias, and the your-domain directory.

       Here is a stripped down version of our SSL VirtualHost file minus the comments.

       <itaHs yui:4>
        Vrulot orp43
          DcmnRo "wwyu-oanhm"
          ouetot /w/ordmi/tl
          SreNm yu-oancm
          evrae ordmi.o
          SreAiswwyu-oancm
          evrla w.ordmi.o
          SrpAis/g-i//w/ordmi/tlcibn
          citla cibn wwyu-oanhm/g-i/

           Erro lg/s_ro_o
           roLg osslerrlg
           TaseLglg/s_ceslg
           rnfro osslacs_o
           Lgee wr
           oLvl an

           SLnieo
           SEgn n
           SLihrut AL!D:EPR5:C+S:HG:MDU:LW+Sv:EP
           SCpeSie L:AH!XOT6R4RA+IH+EIM+O:SL2+X

           SLetfctFl /t/tp/ofslctsre.r
           SCriiaeie echtdcn/s.r/evrct
           SLetfctKyie/t/tp/ofslkysre.e
           SCriiaeeFl echtdcn/s.e/evrky

           <ie ~".cistlptlpp?$>
           Fls  (g|hm|hm|h3)"
             SLpin +tEvas
              SOtos SdnVr
           <Fls
           /ie>

           <ietr "vrwwcibn>
           Drcoy /a/w/g-i"
             SLpin +tEvas
              SOtos SdnVr
           <Drcoy
           /ietr>

           StnI Ue-gn "*SE* 
           eEvf srAet .MI."
                nkeaieslucensudw 
                oeplv s-nla-hton
                dwgae10frersos-.
                onrd-. oc-epne10

           CsoLglg/s_eus_o 
           utmo osslrqetlg
               "t% %SLPOOO} %SLCPE} % %"
                % h {S_RTCLx {S_IHRx "r" b
       <Vrulot
        /itaHs>




        This configuration uses mod_ssl's default SSL certificate and private key for encrypted communications. It's not signed by a
        trusted authority nor does it have your personal or company credentials. Your web browser will complain about the certificate
        and throw up a dialog box when you visit your web site (unless you instruct the browser to save the certificate). If you're
w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                              14/18
12/20/12                      Apache Web Server Tutorial for Linux
        and throw up a dialog box when you visit your web site (unless you instruct the browser to save the certificate). If you're
        thinking about doing e-commerce then you'll need to invest in a real SSL certificate because nothing kills a customer's
        confidence level like a warning screen while making a purchase online. What you would do is generate your C ertificate Signing
        Request (C SR) on your server and then send the data to a C ertificate Authority (C A) like Verisign. Once you pay the fees and
        prove your identity, they'll sign your certificate to make it legitimate and then you can replace the values of SSLC ertificateFile
        and SSLC ertificateKeyFile with your files (in the case of C omodo SSL, you'd have to set the SSLC AC ertificateFile directive with
        their Bundle C ertificate file as well). On the other hand, if you're setting up SSL for your own personal use or for an intranet at
        work, self-signed SSL certificates work just fine. There is no difference between legitimate and self-signed certs except for the
        C A taking your money and stamping it with their sign of approval. The encryption protection is the same either way.

       Save your changes (CTRL-O) to your-domain.com.443.conf and exit nano (CTRL-X). I don't have a tutorial up for generating
       SSL certificates using OpenSSL, but I will soon. You can check Google for a site that details those instructions or wait for mine
       to be posted. Also, you should read up on the Apache SSL/TLS Encryption documention for more information about the
       directives and virtual host configurations for Apache SSL.



       Starting Apache and Viewing the Web Site
       Apache is set up, our directories have been created, and the VirtualHost config files are done, now is the time to start the
       Apache web server and see if everything worked. It's taken us a long time to get here and if all goes well then Apache should
       start without any errors and your HTTP and SSL web sites should be available.

       service httpd start




       If Apache failed to start for you, you should double check httpd.conf, your directories, and your virtual host configs for any
       typos or syntax mistakes. Apache can be cryptic at times when errors occur. You can verify virtual host files using httpd -S
       (or for older an older Apache version use httpd -t -D DUMP_VHOSTS). In the next section I'll show you some tips for
       tracking down Apache errors.

       httpd -S




       With the Apache web server running you should have already opened ports 80 and 443 on your firewall so you can connect to
       Apache with a web browser to view your web site. Open up your favorite browser and navigate to your site. You should get the
       default C entOS Apache 2 Test Page unless you have already logged in and uploaded your content via S/FTP.
w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                   15/18
12/20/12                       Apache Web Server Tutorial for Linux




       Standard HTTP works so how about our SSL version of the web site? C hange the http:// to https:// in the address bar of your
       browser. You should be prompted by the security dialog I mentioned before. This is just a warning about your self-signed SSL
       certificate not being trusted by your browser.




w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                           16/18
12/20/12




       C lick OK in FireFox or Yes in Internet Explorer to continue to the secure web site.




       As you can see up in the FireFox address bar, there is a little yellow padlock icon that informs us that we are on a secure web
       site. All communications with this site will be 128-bit encrypted. At this time you may want to log in to your web site via Secure
       FTP (SFTP) or regular FTP to upload your web site files to the html directory using the system account you created earlier.
       You're done! You've successfully configured the Apache web server on Linux.



       Other Things to Know for Using Apache
       Here are some common questions that users new to the Apache HTTP Server may ask (well, the questions I could think of at
       the time).

       How can I restart Apache so that it sees the changes I made to httpd.conf and/or virtual hosts?
       The Apache init script (/etc/init.d/httpd) accepts a number of options, four of the important ones being start, stop, restart, and
       graceful.

                  start tells the init script to start the Apache process and spawns the child processes.
                  stop tells the init script to terminate the Apache's running process and in turn kills off the child processes. Any
                  existing connections to the server will be terminated.
                  restart tells the init script to stop the Apache process and start it back up. Any changes to httpd.conf or virtual
                  hosts will be read back in. Any existing connections will be terminated, so if users are in the middle of a download
                  the transfers will stop.
                  graceful tells the init script to alert Apache to reload httpd.conf and virtual hosts. Existing connections will not be
                  terminated but changes won't be noticed until a new connection is established (pulling up another page or file from
                  the server). Requests are handled by Apache child workers.

        There are two ways of using these options. You can use the init script or the service command. To use the init script, enter this
w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                17/18
12/20/12                     Apache Web Server Tutorial for Linux
        There are two ways of using these options. You can use the init script or the service command. To use the init script, enter this
        on the prompt /etc/init.d/httpd option, such as to restart the Apache server use /etc/init.d/httpd restart from the
        command line. To use the service command, which is what I showed when we started the Apache service, it's service httpd
        option, such as to do a graceful restart use service httpd graceful from the command line.

       Apache won't start. It keeps saying FAILED when I try and start the service. What can I do to fix it?
       This is probably one of the most difficult tasks to do since sometimes Apache doesn't give an error or it's too cryptic when it
       does. Errors usually occur when something has changed, either there is a problem with httpd.conf or your virtual hosts. One of
       my friends enountered an Apache failure and it turned out to be one of its modules got deleted somehow. So the best course
       of action is to make a copy of your httpd.conf and virtual hosts before you ever change them. If you mess them up you can
       always replace the old copy to get Apache back up and running.
                    To try and troubleshoot Apache failures, you should take a look in syslog (tail -n 100 /var/log/messages).
                    See if there are any messages from the httpd process, and if so, does it give you an idea of what may be wrong?
                    I've been able to track down errors by doing this one thing. Get the error and look for it on Google. It's unlikely
                    you're the first person to experience it.
                       If it's a config file error, you can test your httpd.conf and virtual hosts with httpd -S (or on older Apache versions
                       use httpd -t -D DUMP_VHOSTS). If there is a problem with your files, Apache will tell you when you run the test.
                       Another useful hint is to try starting Apache in debug mode using httpd -e debug and see what it prints to the
                       screen. If there is an error it should be shown to you. Once again, if you see the error, use Google to find an
                       answer.
                       If all else fails, there is trial and error. Move your httpd.conf and virtual host files to another directory and reinstall
                       the Apache RPM's. Once Apache is back up with default settings, start making changes to the new httpd.conf and
                       virtual hosts from the old files. Each time you make a change, save your changes and restart Apache. Like I said,
                       it's trial and error and you may be able to track down the problem.

       Can I use Active Server Pages (ASP) or ColdFusion (CFML) with Apache on Linux?
       Why would you want to? Those are weak and useless languages (my opinion of course). You should use PHP instead, however,
       if you have the need to use them, there are ports from third party companies that let you use those languages with Apache on
       Linux. For Active Server Pages, Sun Microsystems has a product called Sun ONE Active Server Pages (a former C hiliSoft
       product) that let's you use ASP on Linux. They have a free trial version you can download and try out. C oldFusion is no longer
       a Macromedia product and is now maintained by Adobe Systems. You can download a free trial version after you register for
       an account.

       [more to be added]



       Conclusion
       I think I may have went overboard with all the information in this Apache tutorial because it took me four days off and on to
       prepare this document. I tried to be as thorough as possible but I realize I may have missed some of the little things like in
       the Q&A section above. Nevertheless, you now have the skills to configure Apache from the ground up from editing httpd.conf,
       creating a structured web site directory schema, and adding HTTP and SSL virtual hosts. You also learned a little bit about
       Secure Sockets Layer and SSL web server certificates. In case you didn't know, if you run into problems or ever need any
       help you can always post your questions to the Linux category of the forum and I will try and respond to them as as possible.




         How would you rate the use fulne ss of this conte nt?                                Ave rage rating: 2.30 out of 9.

                   1       2     3    4     5     6     7     8      9
         Poor                                                             O utstanding

         O ptional: Te ll us why you rate d the conte nt this way.                             1 2 3 4 5 6 7 8 9
                                                                                              12223 pe ople have rate d this conte nt.




         C haracte rs re m aining: 1024
                                                                             Subm it


                                                                                                      This page has been viewed 193,416 times


     C opyright © 2004-2012 Xe noC afe . All R ights R e se rve d.




w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php                                                                          18/18

Weitere ähnliche Inhalte

Was ist angesagt?

Apache web server
Apache web serverApache web server
Apache web serverzrstoppe
 
Apache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya KulkarniApache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya Kulkarniwebhostingguy
 
Apache Server Tutorial
Apache Server TutorialApache Server Tutorial
Apache Server TutorialJagat Kothari
 
Apache Presentation
Apache PresentationApache Presentation
Apache PresentationAnkush Jain
 
Apache Web server Complete Guide
Apache Web server Complete GuideApache Web server Complete Guide
Apache Web server Complete Guidewebhostingguy
 
Web server installation_configuration_apache
Web server installation_configuration_apacheWeb server installation_configuration_apache
Web server installation_configuration_apacheShaojie Yang
 
Apache web server installation/configuration, Virtual Hosting
Apache web server installation/configuration, Virtual HostingApache web server installation/configuration, Virtual Hosting
Apache web server installation/configuration, Virtual Hostingwebhostingguy
 
Apache web server
Apache web serverApache web server
Apache web serverSabiha M
 
Setting up a web server in Linux (Ubuntu)
Setting up a web server in Linux (Ubuntu)Setting up a web server in Linux (Ubuntu)
Setting up a web server in Linux (Ubuntu)Zakaria Hossain
 
Web Server(Apache),
Web Server(Apache), Web Server(Apache),
Web Server(Apache), webhostingguy
 
Utosc2007_Apache_Configuration.ppt
Utosc2007_Apache_Configuration.pptUtosc2007_Apache_Configuration.ppt
Utosc2007_Apache_Configuration.pptwebhostingguy
 
Apache Manager Table of Contents
Apache Manager Table of ContentsApache Manager Table of Contents
Apache Manager Table of Contentswebhostingguy
 

Was ist angesagt? (20)

Apache ppt
Apache pptApache ppt
Apache ppt
 
Apache web server
Apache web serverApache web server
Apache web server
 
Apache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya KulkarniApache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya Kulkarni
 
Apache Web Server Setup 2
Apache Web Server Setup 2Apache Web Server Setup 2
Apache Web Server Setup 2
 
Apache Server Tutorial
Apache Server TutorialApache Server Tutorial
Apache Server Tutorial
 
Apache Presentation
Apache PresentationApache Presentation
Apache Presentation
 
Apache Web server Complete Guide
Apache Web server Complete GuideApache Web server Complete Guide
Apache Web server Complete Guide
 
Apache Web Server Setup 3
Apache Web Server Setup 3Apache Web Server Setup 3
Apache Web Server Setup 3
 
Apache web service
Apache web serviceApache web service
Apache web service
 
Web server installation_configuration_apache
Web server installation_configuration_apacheWeb server installation_configuration_apache
Web server installation_configuration_apache
 
Apache web server installation/configuration, Virtual Hosting
Apache web server installation/configuration, Virtual HostingApache web server installation/configuration, Virtual Hosting
Apache web server installation/configuration, Virtual Hosting
 
Apache web server
Apache web serverApache web server
Apache web server
 
Apache error
Apache errorApache error
Apache error
 
Apache ppt
Apache pptApache ppt
Apache ppt
 
Apache Web Server Setup 1
Apache Web Server Setup 1Apache Web Server Setup 1
Apache Web Server Setup 1
 
Setting up a web server in Linux (Ubuntu)
Setting up a web server in Linux (Ubuntu)Setting up a web server in Linux (Ubuntu)
Setting up a web server in Linux (Ubuntu)
 
Web Server(Apache),
Web Server(Apache), Web Server(Apache),
Web Server(Apache),
 
Utosc2007_Apache_Configuration.ppt
Utosc2007_Apache_Configuration.pptUtosc2007_Apache_Configuration.ppt
Utosc2007_Apache_Configuration.ppt
 
Apache Web Server Setup 4
Apache Web Server Setup 4Apache Web Server Setup 4
Apache Web Server Setup 4
 
Apache Manager Table of Contents
Apache Manager Table of ContentsApache Manager Table of Contents
Apache Manager Table of Contents
 

Ähnlich wie Apache web server tutorial for linux

Configuration of Apache Web Server On CentOS 8
Configuration of Apache Web Server On CentOS 8Configuration of Apache Web Server On CentOS 8
Configuration of Apache Web Server On CentOS 8Kaan Aslandağ
 
Wamp & LAMP - Installation and Configuration
Wamp & LAMP - Installation and ConfigurationWamp & LAMP - Installation and Configuration
Wamp & LAMP - Installation and ConfigurationChetan Soni
 
Apache windows
Apache windowsApache windows
Apache windowsmexxixxo
 
Diva23
Diva23Diva23
Diva23diva23
 
Create dynamic sites with PHP & MySQL
Create dynamic sites with PHP & MySQLCreate dynamic sites with PHP & MySQL
Create dynamic sites with PHP & MySQLkangaro10a
 
Using aphace-as-proxy-server
Using aphace-as-proxy-serverUsing aphace-as-proxy-server
Using aphace-as-proxy-serverHARRY CHAN PUTRA
 
WP Sandbox Presentation WordCamp Toronto 2011
WP Sandbox Presentation WordCamp Toronto 2011WP Sandbox Presentation WordCamp Toronto 2011
WP Sandbox Presentation WordCamp Toronto 2011Alfred Ayache
 
Meeting 14. web server ii
Meeting 14. web server iiMeeting 14. web server ii
Meeting 14. web server iiSyaiful Ahdan
 
Apache ppt
Apache pptApache ppt
Apache pptReka
 
How to Install LAMP in Ubuntu 14.04
How to Install LAMP in Ubuntu 14.04How to Install LAMP in Ubuntu 14.04
How to Install LAMP in Ubuntu 14.04Sanjary Edu
 
APACHE
APACHEAPACHE
APACHEARJUN
 
Application Note APLX-LMW-0403: Interfacing the Apache Web ...
Application Note APLX-LMW-0403: Interfacing the Apache Web ...Application Note APLX-LMW-0403: Interfacing the Apache Web ...
Application Note APLX-LMW-0403: Interfacing the Apache Web ...webhostingguy
 

Ähnlich wie Apache web server tutorial for linux (20)

Configuration of Apache Web Server On CentOS 8
Configuration of Apache Web Server On CentOS 8Configuration of Apache Web Server On CentOS 8
Configuration of Apache Web Server On CentOS 8
 
Wamp & LAMP - Installation and Configuration
Wamp & LAMP - Installation and ConfigurationWamp & LAMP - Installation and Configuration
Wamp & LAMP - Installation and Configuration
 
Apache windows
Apache windowsApache windows
Apache windows
 
Diva23
Diva23Diva23
Diva23
 
Appache.ppt
Appache.pptAppache.ppt
Appache.ppt
 
Create dynamic sites with PHP & MySQL
Create dynamic sites with PHP & MySQLCreate dynamic sites with PHP & MySQL
Create dynamic sites with PHP & MySQL
 
Appache.ppt
Appache.pptAppache.ppt
Appache.ppt
 
Using aphace-as-proxy-server
Using aphace-as-proxy-serverUsing aphace-as-proxy-server
Using aphace-as-proxy-server
 
Its3 Drupal
Its3 DrupalIts3 Drupal
Its3 Drupal
 
WP Sandbox Presentation WordCamp Toronto 2011
WP Sandbox Presentation WordCamp Toronto 2011WP Sandbox Presentation WordCamp Toronto 2011
WP Sandbox Presentation WordCamp Toronto 2011
 
Meeting 14. web server ii
Meeting 14. web server iiMeeting 14. web server ii
Meeting 14. web server ii
 
Babitha.4appach
Babitha.4appachBabitha.4appach
Babitha.4appach
 
Babitha.4appach
Babitha.4appachBabitha.4appach
Babitha.4appach
 
Apache ppt
Apache pptApache ppt
Apache ppt
 
How to Install LAMP in Ubuntu 14.04
How to Install LAMP in Ubuntu 14.04How to Install LAMP in Ubuntu 14.04
How to Install LAMP in Ubuntu 14.04
 
APACHE
APACHEAPACHE
APACHE
 
Its3 Drupal
Its3 DrupalIts3 Drupal
Its3 Drupal
 
Lamp
LampLamp
Lamp
 
Application Note APLX-LMW-0403: Interfacing the Apache Web ...
Application Note APLX-LMW-0403: Interfacing the Apache Web ...Application Note APLX-LMW-0403: Interfacing the Apache Web ...
Application Note APLX-LMW-0403: Interfacing the Apache Web ...
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 

Kürzlich hochgeladen

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Kürzlich hochgeladen (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Apache web server tutorial for linux

  • 1. 12/20/12 Apache Web Server Tutorial for Linux Home Tutorials Forum Blog Advertising Links Contact About Search Apache Web Server Tutorial for Linux Written by Tony Bhimani Posted on August 2, 2006 Requirements *nix based Operating System Apache HTTP Server mod_ssl Apache Interface PHP Scripting Language Contents Introduction Installing Apache HTTP Web Server Installing mod_ssl for Secure Sockets Layer (SSL) Support Installing PHP for Dynamic Web Pages Setting Apache to Start on Bootup with chkconfig C onfiguring Apache Server Settings (httpd.conf) C reating the Web Site Directory Structure C reating VirtualHost C onfiguration Files Starting Apache and Viewing the Web Site Other Things to Know for Using Apache C onclusion Introduction The Apache Web Server is one of the most popular open-source HTTP Servers that exists today. It's a powerful, secure, and fully featured web server that can be found hosting anything from personal web sites to corporate domains. If you have a DSL or C able connection with a static IP then you can use the Apache web server to host your web site (as long as your ISP doesn't have a problem with you running a server on their network). This tutorial will teach you how to set up your own Apache web server on Linux so you can host your own web sites either at home or work. You'll learn how to install the Apache HTP Server with SSL and PHP support, use chkconfig to set Apache to auto-start on boot, edit the Apache httpd.conf configuration file for your server settings, create a directory structure for your web site, create the VirtualHost configuration file for your web site, start the Apache service, verify your site is working, go over the init script options, and finally show you some additional options and settings for Apache such as graceful restarts, htaccess usage, user name and password directory protection, and custom error pages. The two things I won't be covering are setting up DNS for your domain and configuring your firewall to open ports 80 and 443 for HTTP and SSL respectively, however you can read my Bind DNS tutorial and iptables firewall tutorial for information on how to do it. I'll be using C entOS 4.2 and Apache 2.x will be installed using yum instead of source. Let's get started. Installing Apache HTTP Web Server We'll be using yum to download and install the Apache RPM's from the C entOS repository. Apache shouldn't be loaded on your server unless you selected it during the operating system installation process. If you know you have Apache installed then you can skip this section, but if it's not or you're not sure then you should perform the following tests to see if the Apache web server is on your system. To do this we'll use the which command to look for the httpd binary in the environment variable PATH. which httpd If you see something similar to the above image then the Apache binary doesn't seem to be found on the server. Our next step is to install Apache by using yum. Issue the next command. yum install httpd w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 1/18
  • 2. 12/20/12 Apache Web Server Tutorial for Linux Two RPM packages will be downloaded from the C entOS repository (httpd-2.0.52-22.ent.centos4.i386.rpm and httpd-suexec- 2.0.52-22.ent.centos4.i386.rpm) and installed automatically. If everything goes well you should now have the Apache web server on your system. You might also want to load the Apache documentation so you'll have the man pages available. This is optional but highly recommended. Use yum again and download the Apache manual (yum install httpd-manual). Next we'll use yum to install mod_ssl for Secure Sockets Layer (SSL) support. Installing mod_ssl for Secure Sockets Layer (SSL) Support Secure Sockets Layer, also known as SSL for short, provides encrypted commnications between hosts. SSL can be found in e- commerce web sites and any other site that requires sensitive information being transmitted protected from eavesdropping. In a nutshell, the hosts negotiate a protocol they both can understand and then switch to secure communications. The data passed back and forth between the hosts is encrypted with a 128-bit key (hence 128-bit SSL). In a typical SSL setup, the web server has SSL support enabled and a certificate that verifies the company's identity. These certificates are initially generated as C ertificate Signing Requests (C SR) by the server's administrator. The C SR is then signed by a third party, a C ertificate Authority (C A), that validates the company's identity and makes the C SR into a full certificate. There are many C A's like VeriSign, Thawte, GeoTrust, InstantSSL (aka The C omodo Group), FreeSSL, and many others that sign C SR's. Prices vary from company to company, but as long as they support the mainstream browsers you shouldn't run into any problems. If you'd like to learn more about Secure Sockets Layer then you should check out the Wikipedia Transport Layer Security page. You may be wondering where does mod_ssl fits into all of this? On Linux and Unix distributions, there is a software package called OpenSSL that performs all the cryptography and mod_ssl is the Apache interface to OpenSSL. It's mod_ssl that makes it possible for Apache to have SSL capability. We'll now install the mod_ssl RPM (mod_ssl-2.0.52-22.ent.centos4.i386.rpm) for Apache using yum. yum install mod_ssl w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 2/18
  • 3. 12/20/12 Apache Web Server Tutorial for Linux mod_ssl should now be installed. If you didnt have OpenSSL installed then it should have been picked up as a dependency and was installed as well. We have one more package to install and that's the PHP scripting language. Installing PHP for Dynamic Web Pages PHP is a scripting language for creating dynamic web pages. PHP allows you to include code in your web pages to be processed server-side and the resulting HTML is sent to the user's web browser. With PHP you can add database access, read and write files to the server's filesystem, generate dynamic graphics like graphs and security image codes, and create security control mechanisms for fencing off parts of your web site. PHP has many other uses and that's what makes it a powerful tool for creating dynamic web pages. If you're not a programmer or will only he hosting a static HTML web pages, then PHP won't be of any use to you. However, if you want to learn PHP then you should install the PHP modules for Apache. We'll use yum one last time to install PHP on our server. There are many PHP RPM modules, one being the actually binary and modules for Apache and the rest are support features for MySQL, XML, ODBC , and others to tie those subsystems into PHP. It's a good idea to install all the PHP modules since you never know what you'll need now or in the future. Now we'll use yum to install PHP. yum install php* w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 3/18
  • 4. 12/20/12 Apache Web Server Tutorial for Linux As you can see above, we used a wildcard to tell yum to install anything starting with the word 'php'. On my server yum downloaded and installed fifteen packages, however yours may be different depending on its configuration. Now that PHP is installed we can move on to setting the Apache service to automatically start on bootup or in the event of a reboot. Setting Apache to Start on Bootup with chkconfig The Apache web server will need to be set to autostart when the server boots. The Apache RPM didn't set this up for us so we'll have to do it ourselves using chkconfig (Note: you can also use setup to turn on the Apache service). We're going to have Apache start on run levels 2, 3, and 5. chkconfig --level 235 httpd on chkconfig --list httpd The second chkconfig command lists the run levels Apache is configured to start on. If you don't have X Windows installed then you may want to omit run level 5 (Multi-User Mode - boot up in X Windows). To learn more about Linux Run Levels you may want to check out this page on NetworkC lue. Now that Apache is installed and set to start up we'll move on to configuring the Apache web server by editing httpd.conf, the Apache server configuration file. Configuring Apache Server Settings (httpd.conf) Apache's main configuration file is called httpd.conf and is located in /etc/httpd/conf/. The default httpd.conf will work without any changes, however we want to customize Apache a little bit. Our main focus is to setup the use of Virtual Hosts so we can run as many web sites as we want using a single IP address. Also, we want to simplify the management of our Virtual Hosts without cluttering httpd.conf with our entries. Before we get to any of that we will first configure our Apache web server. I will make the assumption that you have a single WAN based IP address. For this example I'm using 192.168.1.210 with a hostname of node2.centos (yes, it's a LAN IP but pretend it's WAN). You can use your IP or hostname from /etc/hosts, but I suggest using your hostname because if you ever change IP's all you have to do is update /etc/hosts with the new address. If you do use an IP then you'll have to change all instances of it in httpd.conf. If you don't know what your IP address is or never set up /etc/hosts, you can find your address by using ifconfig (look at the number to the right of 'inet addr:'). We'll start with opening httpd.conf in the nano text editor, but before that you should make a copy of your existing httpd.conf file. If you encounter any problems you'll still have the original to fall back to. cd /etc/httpd/conf cp httpd.conf httpd.conf.old nano httpd.conf Apache's httpd.conf is filled with many helpful comments to tell you what each configuration directive does. Scroll down to line 133 as shown in the picture below. FYI, if you ever want to know what line number you're on in nano, press CTRL-C and nano will show you. We're looking for the line that says 'Listen 80'. We won't be changing this directive since we want Apache listen on all IP addresses set up on the server. I wanted to show you where to change it if you needed to bind Apache to only one address. w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 4/18
  • 5. 12/20/12 Apache Web Server Tutorial for Linux Move on down to line number 235 where it says 'ServerAdmin root@localhost'. The Apache ServerAdmin directive is for the administrator's email address of the server. End users encountering any problems with the server would use this email address to notify the sysadmin. C omment out the existing ServerAdmin line with a pound symbol and enter a new line below with your email address. For this example I used admin@node2.centos. #ServerAdmin root@localhost ServerAdmin admin@your-domain.com Below the ServerAdmin directive is ServerName. The Apache ServerName directive is for Apache to identify itself which is typically the hostname of the server. You'll want to specify your hostname or IP address instead of a valid DNS name especially if you're hosting many web sites. There are some people that may disagree with this method but I think it's better to keep all the generic information in httpd.conf and use specifics in the VirtualHost configuration files. Add a new blank line below the commented ServerName directive and add your hostname or IP. For this server I used my hostname node2.centos. #ServerName new.host.name:80 w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 5/18
  • 6. 12/20/12 Apache Web Server Tutorial for Linux ServerName yourhostname The next directive we'll edit is very close to the end of the file. Scroll all the way down to line number 1005 (tip: use the Page Down key on your keyboard) and look for '#NameVirtualHost *:80'. The NameVirtualHost directive tells Apache that we want to use name-based virtual hosting, or in simpler terms, a bunch of web sites all using the same IP address. Virtual hosting is made possible because when a web browser goes to a site, for example www.xenocafe.com, the web site address is passed as part of the HTTP header (Host: www.xenocafe.com). This allows Apache to distinguish between different hosts sharing the same IP address. Add a couple blank lines underneath '#NameVirtualHost *:80' and on the line right below put NameVirtualHost and your hostname or IP address followed by a :80. The ':80' means we're using name-based virtual hosting for the HTTP protocol. SSL will be configured through VirtualHost configuration files due to its nature. You'll learn why when we create our virtual hosts. For this server I used my hostname node2.centos. #NameVirtualHost *:80 NameVirtualHost yourhostname:80 6/18
  • 7. We're almost done. The last thing we need to do is create a default virtual host to respond to requests when someone vists our IP address and not our domain name. You may or may not want to add this virtual host and it's totally up to you, but I personally don't want anyone going directly to my IP address. I prefer they visit my web site by name only. A VirtualHost entry can have many directives which I'll explain later, but what you need to know for now is the example I provide responds by IP address visits. At the very end of the file there is a line that instructs Apache to load any configuration files found in the /etc/httpd/conf/vhosts/ directory (which we'll create later). This is my way of keeping the virtual hosts separate from httpd.conf and most importantly, making hosts easier to manage. <itaHs yuhsnm:0 Vrulot orotae8> SreAmnyuyu-oancm evrdi o@ordmi.o SreNm yu_pades evrae ori_drs DcmnRo /w ouetot ww Erro lg/ro_o roLg oserrlg CsoLglg/ceslgcmie utmo osacs_o obnd <Vrulot /itaHs> #icueVrulot cni fls nld itaHss ofg ie Icuecn/hss*cn nld ofvot/.of
  • 8. 12/20/12 Apache Web Server Tutorial for Linux We're done editing httpd.conf. Let's save our changes (CTRL-O) and exit nano (CTRL-X). Now we'll move on to creating a web site directory structure and user account for SSH access and S/FTP file uploads. Creating the Web Site Directory Structure Our web site directory schema should be simple yet structured. The common approach would be to use /home as the root but we won't be doing that. Our web site parent root will be /www and from within it create a directory for each domain we'll be hosting. Within each domain directory there will be a set of common directories (html, html/cgi-bin, databases, and logs). Reading what I wrote doesn't make much sense so here it is visually. For this example and the rest of the tutorial, we'll say we acquired the domain your-domain.com and will be configuring our server for it. /w ww (otfralhse dmis ro o l otd oan) /w/ordmi wwyu-oan (oandrcoy dmi ietr) /w/ordmi/tl wwyu-oanhm (ietr fryu wbst fls drcoy o or e ie ie) /w/ordmi/tlcibn(G drcoyfreeuigPr srps wwyu-oanhm/g-i CI ietr o xctn el cit) /w/ordmi/aaae wwyu-oandtbss (aaae frti wbst soe o aprst bss dtbss o hs e ie trd n e ie ai) /w/ordmi/os wwyu-oanlg (e st acs aderrlg aesoe hr) wb ie ces n ro os r trd ee The databases directory is optional. If you read my mapping mysql databases tutorial then you can migrate your databases outside the default /var/lib/mysql into the databases directory. This will allow you to store your databases on a per site basis in the web site's home directory. Creating the Directory Root (one-time only) We'll start creating our web site directory structure by making /www. C reating this directory is a one-time process and will be home to all our domains we're hosting (any domains we add will be stored in the /www directory). mkdir /www chown root.root /www w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 8/18
  • 9. 12/20/12 Apache Web Server Tutorial for Linux chown root.root /www After we create the directory we chown it to uid:root gid:root. Creating Web Site Domain Directories For each web site you're hosting you'll create a directory structure within the parent /www. The directory should be named the same as the domain name with or without the TLD (.com, .net, etc). I'll let you decide which way you want your directories to be named, but I tend to prefer them without the TLD. Now we'll create our domain's directories. cd /www mkdir your-domain cd your-domain mkdir html mkdir html/cgi-bin mkdir databases mkdir logs Now that the directory structure is created we'll need to add a system user that owns the directory base and can log in to upload files. Adding a Linux User Account for the Web Site The user account will be able to FTP or Secure FTP (SFTP) into the server to upload and download web site content files like HTML, C GI scripts, and PHP pages. This user account will have ownership of the domain's directory base and be locked into it. The user name we'll assign will be the same as the domain name (your-domain) to make it easier to distinguish between it and the other Linux accounts, however you can use any name you want for your account. One thing to keep in mind is when you create this account you'll have to make a choice whether this account will be granted SSH access. If you want to disallow SSH access then you'll need to change the shell to /sbin/nologin. SSH login attempts will fail when you use /sbin/nologin as the account's shell (valid login credentials will be accepted but the connection will be terminated). To create the web site system account, use one of the following commands. useradd -d /www/your-domain/ domain_user or useradd -d /www/your-domain/ -s /sbin/nologin domain_user The first command will set the account's home directory (-d path) to /www/your-domain and creates a new system account with the user name of domain_user (last parameter of the command). The shell will be set to the default, which is typically w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php
  • 10. 12/20/12 Apache Web Server Tutorial for Linux with the user name of domain_user (last parameter of the command). The shell will be set to the default, which is typically bash unless you changed it. The second command is identical to the first except that the shell (-s shell) is set to /sbin/nologin to prevent SSH logins. After the account has been created you'll need to set a password using the passwd command. passwd domain_user [enter password] [confirm password] Now that the system account is ready we'll move on to changing the directory base ownership. Changing Web Site Directory Ownership & Permissions We've created the directories, added the account, the only thing left to do is change the ownership of the directories to the new user account. Right now the directories are owned by root and if you were to try using FTP to upload some files you'd get the dreaded Permission Denied error. To change ownership of the domain directory and all its subdirectories we'll use the chown command with the -R flag for recursive. cd /www chown -R domain_user:domain_user your-domain If you created the databases directory then you'll need to change the owner to the mysql user otherwise MySQL won't be able to write to the database files. All directories, subdirectories, and files starting from databases should be owned by the mysql user (or whatever user account you have the MySQL server running as). cd your-domain chown -R mysql:mysql databases w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 10/18
  • 11. That's it for creating our directory structure. If you have more domains to add then repeat the steps we went through except for the first one of creating the /www directory root. Next we'll create the VirtualHost configuration files to let Apache know the details of our web site. Creating VirtualHost Configuration Files A VirtualHost configuration file tells Apache the specifics about your web site. It contains information like what IP and port number your site runs on, the server name and any aliases, the local filesystem path (or document root) of your web pages, location of your cgi-bin, file names and paths of your access and error logs, custom error documents for HTTP errors like 404 File Not Found, and can have many other items. For every web site you want Apache to host you must create a virtual host configuration. You've already gotten a taste of a virtual host config when we added the default VirtualHost for handling IP address requests in httpd.conf, so what we're going to be doing now is nothing new. I'm now going to show you an easy way of managing all your virtual hosts. The VirtualHost Configuration File Directory (/etc/httpd/conf/vhosts) When I first learned Linux from a friend some years ago, he showed me how to configure Apache so I could set up my web server. I looked at his httpd.conf and he had all his virtual hosts in there, so I figured there was nothing unusual about that as I'm sure there are many people that did the same thing. He hosted a lot of domains for friends, his business, and his own stuff, and I noticed that trying to locate a web site in his massive httpd.conf file was nothing short of a headache. Although I didn't doubt his knowledge of Apache I kept thinking there had to be an easier way of managing virtual hosts. Through reading the online Apache Web Server documentation I came across the Include directive. The Include directive, much like in C /C ++, PHP, and other languages, allows you to insert the contents of another file into the calling file. However, in the case of Apache, the contents of a single file or of several files located in a directory that match the Include statement would be included. If you remember when we were editing httpd.conf, at the very end we added the line 'Include conf/vhosts/*.conf'. That line tells Apache to look in /etc/httpd/conf/vhosts for any *.conf files and include their contents as part of httpd.conf. This allows us to separate out our VirtualHost definitions as their own unique files, on a per domain basis, from the main httpd.conf file. This makes administration easy for several reasons. Finding a virtual host is easy because all you do is look in the vhosts directory for its file, disabling a domain is as simple as renaming the file so it doesn't end in .conf, and adding new domains is accomplished by creating a new virtual host file (copy an exisiting file and edit the changes). Include makes virtual host management a piece of cake. We'll now create the /etc/httpd/conf/vhosts directory (this is a one-time process). cd /etc/httpd/conf mkdir vhosts cd vhosts We can start creating VirtualHost configuration files now. I'll show you how to create one for the HTTP protocol as well as one for Secure Sockets Layer (SSL). Creating a HTTP Virtual Host When creating virtual host config files we should use a good naming convention that's as descriptive as possible. What's worked for me is the format of host.domain.tld.port.conf or in simple terms your-domain.com.80.conf. You may have noticed I excluded host from the file name. When you visit a domain such as redhat.com, or www.redhat.com, they most likely take you to the same web site, so www as a host can be excluded. The only time I use host as part of the file name is when I am adding a subdomain such as forum.example.com or blog.example.com and so forth. Let's create the HTTP virtual host file. nano your-domain.com.80.conf Type in or copy/paste the configuration data below. Be sure to replace yourhostname with your system's hostname or IP address, ServerAdmin with your email address, ServerName and ServerAlias with your real domain name, and your-domain with the directory name you created earlier for your web site. When you're done, save your changes (CTRL-O) and exit nano (CTRL-X). <itaHs yuhsnm:0 Vrulot orotae8> SreAmnamnyu-oancm evrdi di@ordmi.o SreNm yu-oancm evrae ordmi.o w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 11/18
  • 12. 12/20/12 Apache Web Server Tutorial for Linux SreNm yu-oancm evrae ordmi.o SreAiswwyu-oancm evrla w.ordmi.o DcmnRo /w/ordmi/tl ouetot wwyu-oanhm SrpAis/g-i//w/ordmi/tlcibn citla cibn wwyu-oanhm/g-i/ Erro /w/ordmi/oserrlg roLg wwyu-oanlg/ro_o CsoLg/w/ordmi/osacs_o cmie utmo wwyu-oanlg/ceslg obnd <Vrulot /itaHs> I'll clarify what each line does in the virtual host configuration file between the <VirtualHost></VirtualHost> tags. ServerAdmin is used to specify the server administrator's email address. User's will see this address on server generated error pages. ServerName is the name of the server or DNS name. Apache will match this name against the HTTP host header sent by the user's browser. If it's a match then this is the configuration data used to process the request. ServerAlias is identical to ServerName except ServerName is used once and ServerAlias can be used many times. If your web site will respond to more than one DNS name, make sure you use one ServerName directive and all other names are specified using ServerAlias. DNS names can be separated by commas for a single ServerAlias directive (ServerAlias dns_name1,dns_name2,dns_name3) or you can use many ServerAlias directives for each DNS name placed on a line each their own. DocumentRoot defines the file system path to where the web page files are located. This is the same directory where you will place your HTML, PHP, and image files. For our setup, this is the /www/your-domain/html directory. ScriptAlias defines the directory for executing cgi scripts, commonly known as cgi-bin. It takes two parameters with the first being the cgi-bin directory from your web site root path and the second being the full physical directory path on the filesystem. You can have as many cgi-cin directories as you need and they don't need to be called cgi-bin. ErrorLog defines the file to log any errors Apache encounters. Errors can include PHP script failures and files that don't exist. CustomLog defines the file to log any requests to the server. Requests include accessing pages, images, and other files like robots.txt. The second parameter sets the log entry format. We use the combined format since it is used by many Web Server Log Analysis scripts like Awstats. You should read the online Apache 2.0 documentation for more information on these directives as well as other Apache web server features. Creating a SSL Virtual Host When we installed mod_ssl, a file called ssl.conf was written to /etc/httpd/conf.d that contains basic SSL configuration information. We're going to use this as our template for adding a SSL based virtual host. What we'll do is make a copy of it to our vhosts directory and remove the non-website specific information since it should only be loaded once by Apache. This non- website specific information pertains to loading the mod_ssl Apache module among other things while everything else in the file relates to configuring the SSL web site (what we're interested in). Let's make a copy of ssl.conf to the vhosts directory which you should still be in (cd /etc/httpd/conf/vhosts). Following the same format for our virtual host config files (host.domain.tld.port.conf) as before, we will be naming our SSL virtual host file as your-domain.com.443.conf. The port number for SSL is 443 so we'll use that as opposed to port 80 when we created the HTTP VirtualHost. After that we'l open the config file in nano so we can begin editing it. cp /etc/httpd/conf.d/ssl.conf ./your-domain.com.443.conf nano your-domain.com.443.conf w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 12/18
  • 13. 12/20/12 Apache Web Server Tutorial for Linux Now comes the fun part. We need to delete a good portion of this file starting from the top and all the way down to the part that says '### SSL Virtual Host C ontext' (roughly 83 lines total). Instead of using the DEL key, you can use CTRL-K to cut the text line by line in nano. Starting from the top of the file, remove the lines of text until you reach the blank line above '### SSL Virtual Host C ontext'. Your file should look like the image below when you're done. w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 13/18
  • 14. 12/20/12 Apache Web Server Tutorial for Linux Next we'll edit the default web site specific directives to mold them to our web site. They're actually the same directives we used when creating our HTTP virtual host, so you can see how easy this will be. I should point out that we have been using our hostname for the VirtualHost tag, but in the case of SSL I'd advise against unless you want to set up each host and their binding IP address in /etc/hosts (it's a good idea if you have many IP's). Secure Sockets Layer doesn't work at the Application layer of the OSI model like HTTP does, so the Host part of the HTTP header doesn't apply. So in regards to name-based virtual hosting with SSL, it's non-existent. Replace '_default_' with your IP address when editing your-domain.com.443.conf. The same rules apply as before with ServerName, ServerAlias, and the your-domain directory. Here is a stripped down version of our SSL VirtualHost file minus the comments. <itaHs yui:4> Vrulot orp43 DcmnRo "wwyu-oanhm" ouetot /w/ordmi/tl SreNm yu-oancm evrae ordmi.o SreAiswwyu-oancm evrla w.ordmi.o SrpAis/g-i//w/ordmi/tlcibn citla cibn wwyu-oanhm/g-i/ Erro lg/s_ro_o roLg osslerrlg TaseLglg/s_ceslg rnfro osslacs_o Lgee wr oLvl an SLnieo SEgn n SLihrut AL!D:EPR5:C+S:HG:MDU:LW+Sv:EP SCpeSie L:AH!XOT6R4RA+IH+EIM+O:SL2+X SLetfctFl /t/tp/ofslctsre.r SCriiaeie echtdcn/s.r/evrct SLetfctKyie/t/tp/ofslkysre.e SCriiaeeFl echtdcn/s.e/evrky <ie ~".cistlptlpp?$> Fls (g|hm|hm|h3)" SLpin +tEvas SOtos SdnVr <Fls /ie> <ietr "vrwwcibn> Drcoy /a/w/g-i" SLpin +tEvas SOtos SdnVr <Drcoy /ietr> StnI Ue-gn "*SE* eEvf srAet .MI." nkeaieslucensudw oeplv s-nla-hton dwgae10frersos-. onrd-. oc-epne10 CsoLglg/s_eus_o utmo osslrqetlg "t% %SLPOOO} %SLCPE} % %" % h {S_RTCLx {S_IHRx "r" b <Vrulot /itaHs> This configuration uses mod_ssl's default SSL certificate and private key for encrypted communications. It's not signed by a trusted authority nor does it have your personal or company credentials. Your web browser will complain about the certificate and throw up a dialog box when you visit your web site (unless you instruct the browser to save the certificate). If you're w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 14/18
  • 15. 12/20/12 Apache Web Server Tutorial for Linux and throw up a dialog box when you visit your web site (unless you instruct the browser to save the certificate). If you're thinking about doing e-commerce then you'll need to invest in a real SSL certificate because nothing kills a customer's confidence level like a warning screen while making a purchase online. What you would do is generate your C ertificate Signing Request (C SR) on your server and then send the data to a C ertificate Authority (C A) like Verisign. Once you pay the fees and prove your identity, they'll sign your certificate to make it legitimate and then you can replace the values of SSLC ertificateFile and SSLC ertificateKeyFile with your files (in the case of C omodo SSL, you'd have to set the SSLC AC ertificateFile directive with their Bundle C ertificate file as well). On the other hand, if you're setting up SSL for your own personal use or for an intranet at work, self-signed SSL certificates work just fine. There is no difference between legitimate and self-signed certs except for the C A taking your money and stamping it with their sign of approval. The encryption protection is the same either way. Save your changes (CTRL-O) to your-domain.com.443.conf and exit nano (CTRL-X). I don't have a tutorial up for generating SSL certificates using OpenSSL, but I will soon. You can check Google for a site that details those instructions or wait for mine to be posted. Also, you should read up on the Apache SSL/TLS Encryption documention for more information about the directives and virtual host configurations for Apache SSL. Starting Apache and Viewing the Web Site Apache is set up, our directories have been created, and the VirtualHost config files are done, now is the time to start the Apache web server and see if everything worked. It's taken us a long time to get here and if all goes well then Apache should start without any errors and your HTTP and SSL web sites should be available. service httpd start If Apache failed to start for you, you should double check httpd.conf, your directories, and your virtual host configs for any typos or syntax mistakes. Apache can be cryptic at times when errors occur. You can verify virtual host files using httpd -S (or for older an older Apache version use httpd -t -D DUMP_VHOSTS). In the next section I'll show you some tips for tracking down Apache errors. httpd -S With the Apache web server running you should have already opened ports 80 and 443 on your firewall so you can connect to Apache with a web browser to view your web site. Open up your favorite browser and navigate to your site. You should get the default C entOS Apache 2 Test Page unless you have already logged in and uploaded your content via S/FTP. w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 15/18
  • 16. 12/20/12 Apache Web Server Tutorial for Linux Standard HTTP works so how about our SSL version of the web site? C hange the http:// to https:// in the address bar of your browser. You should be prompted by the security dialog I mentioned before. This is just a warning about your self-signed SSL certificate not being trusted by your browser. w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 16/18
  • 17. 12/20/12 C lick OK in FireFox or Yes in Internet Explorer to continue to the secure web site. As you can see up in the FireFox address bar, there is a little yellow padlock icon that informs us that we are on a secure web site. All communications with this site will be 128-bit encrypted. At this time you may want to log in to your web site via Secure FTP (SFTP) or regular FTP to upload your web site files to the html directory using the system account you created earlier. You're done! You've successfully configured the Apache web server on Linux. Other Things to Know for Using Apache Here are some common questions that users new to the Apache HTTP Server may ask (well, the questions I could think of at the time). How can I restart Apache so that it sees the changes I made to httpd.conf and/or virtual hosts? The Apache init script (/etc/init.d/httpd) accepts a number of options, four of the important ones being start, stop, restart, and graceful. start tells the init script to start the Apache process and spawns the child processes. stop tells the init script to terminate the Apache's running process and in turn kills off the child processes. Any existing connections to the server will be terminated. restart tells the init script to stop the Apache process and start it back up. Any changes to httpd.conf or virtual hosts will be read back in. Any existing connections will be terminated, so if users are in the middle of a download the transfers will stop. graceful tells the init script to alert Apache to reload httpd.conf and virtual hosts. Existing connections will not be terminated but changes won't be noticed until a new connection is established (pulling up another page or file from the server). Requests are handled by Apache child workers. There are two ways of using these options. You can use the init script or the service command. To use the init script, enter this w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 17/18
  • 18. 12/20/12 Apache Web Server Tutorial for Linux There are two ways of using these options. You can use the init script or the service command. To use the init script, enter this on the prompt /etc/init.d/httpd option, such as to restart the Apache server use /etc/init.d/httpd restart from the command line. To use the service command, which is what I showed when we started the Apache service, it's service httpd option, such as to do a graceful restart use service httpd graceful from the command line. Apache won't start. It keeps saying FAILED when I try and start the service. What can I do to fix it? This is probably one of the most difficult tasks to do since sometimes Apache doesn't give an error or it's too cryptic when it does. Errors usually occur when something has changed, either there is a problem with httpd.conf or your virtual hosts. One of my friends enountered an Apache failure and it turned out to be one of its modules got deleted somehow. So the best course of action is to make a copy of your httpd.conf and virtual hosts before you ever change them. If you mess them up you can always replace the old copy to get Apache back up and running. To try and troubleshoot Apache failures, you should take a look in syslog (tail -n 100 /var/log/messages). See if there are any messages from the httpd process, and if so, does it give you an idea of what may be wrong? I've been able to track down errors by doing this one thing. Get the error and look for it on Google. It's unlikely you're the first person to experience it. If it's a config file error, you can test your httpd.conf and virtual hosts with httpd -S (or on older Apache versions use httpd -t -D DUMP_VHOSTS). If there is a problem with your files, Apache will tell you when you run the test. Another useful hint is to try starting Apache in debug mode using httpd -e debug and see what it prints to the screen. If there is an error it should be shown to you. Once again, if you see the error, use Google to find an answer. If all else fails, there is trial and error. Move your httpd.conf and virtual host files to another directory and reinstall the Apache RPM's. Once Apache is back up with default settings, start making changes to the new httpd.conf and virtual hosts from the old files. Each time you make a change, save your changes and restart Apache. Like I said, it's trial and error and you may be able to track down the problem. Can I use Active Server Pages (ASP) or ColdFusion (CFML) with Apache on Linux? Why would you want to? Those are weak and useless languages (my opinion of course). You should use PHP instead, however, if you have the need to use them, there are ports from third party companies that let you use those languages with Apache on Linux. For Active Server Pages, Sun Microsystems has a product called Sun ONE Active Server Pages (a former C hiliSoft product) that let's you use ASP on Linux. They have a free trial version you can download and try out. C oldFusion is no longer a Macromedia product and is now maintained by Adobe Systems. You can download a free trial version after you register for an account. [more to be added] Conclusion I think I may have went overboard with all the information in this Apache tutorial because it took me four days off and on to prepare this document. I tried to be as thorough as possible but I realize I may have missed some of the little things like in the Q&A section above. Nevertheless, you now have the skills to configure Apache from the ground up from editing httpd.conf, creating a structured web site directory schema, and adding HTTP and SSL virtual hosts. You also learned a little bit about Secure Sockets Layer and SSL web server certificates. In case you didn't know, if you run into problems or ever need any help you can always post your questions to the Linux category of the forum and I will try and respond to them as as possible. How would you rate the use fulne ss of this conte nt? Ave rage rating: 2.30 out of 9. 1 2 3 4 5 6 7 8 9 Poor O utstanding O ptional: Te ll us why you rate d the conte nt this way. 1 2 3 4 5 6 7 8 9 12223 pe ople have rate d this conte nt. C haracte rs re m aining: 1024 Subm it This page has been viewed 193,416 times C opyright © 2004-2012 Xe noC afe . All R ights R e se rve d. w w w .xenocafe.com/tutorials/linux/centos/apache_w eb_server/index.php 18/18