Copy Paste http://www.xenocafe.com/
Installing Apache HTTP Web Server
We’ll be using yum to download and install the Apache RPM’s from the CentOS 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
![[graphical representation of executing 'which httpd']](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image1.png)
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
![[graphical representation of executing 'yum install httpd']](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image2.gif)
Two RPM packages will be downloaded from the CentOS 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 Certificate Signing Requests (CSR) by the server’s administrator. The CSR is then signed by a third party, a Certificate Authority (CA), that validates the company’s identity and makes the CSR into a full certificate. There are many CA’s like VeriSign, Thawte, GeoTrust, InstantSSL (aka The Comodo Group), FreeSSL, and many others that sign CSR’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
![[graphical representation of executing 'yum install mod_ssl']](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image3.gif)
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*
![[graphical representation of executing 'yum install php*']](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image4.png)
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
![[graphical representation of executing 'chkconfig --level 235 httpd on']](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image5.png)
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 NetworkClue. 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.
![[graphical representation of executing 'cd /etc/httpd/conf','cp httpd.conf httpd.conf.old','nano httpd.conf']](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image6.gif)
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. Comment 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
![[graphical representation of adding 'ServerAdmin admin@your-domain.com' to httpd.conf]](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image7.gif)
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
ServerName yourhostname
![[graphical representation of adding 'ServerName yourhostname' to httpd.conf]](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image8.gif)
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
![[graphical representation of adding 'NameVirtualHost yourhostname:80' to httpd.conf]](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image9.gif)
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.
<VirtualHost yourhostname:80>
ServerAdmin you@your-domain.com
ServerName your_ip_address
DocumentRoot /www
ErrorLog logs/error_log
CustomLog logs/access_log combined
</VirtualHost>
# include VirtualHosts config files
Include conf/vhosts/*.conf
![[graphical representation of adding an IP based virtual host to httpd.conf and 'Include conf/vhosts/*.conf']](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image10.gif)
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.
/www (root for all hosted domains)
/www/your-domain (domain directory)
/www/your-domain/html (directory for your web site files)
/www/your-domain/html/cgi-bin (CGI directory for executing Perl scripts)
/www/your-domain/databases (databases for this web site stored on a per site basis)
/www/your-domain/logs (web site access and error logs are stored here)
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. Creating 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
![[graphical representation of executing 'mkdir /www','chown root.root /www']](http://www.xenocafe.com/tutorials/linux/centos/apache_web_server/images/Image11.png)
paniang…..paniang ….paniang….dek bahaso e..




















