Payment of ₹ requested by HONEY VIG. Click the link to pay securely via Razorpay: Make 5000 payment Payment of ₹ requested by HONEY VIG. Click the link to pay securely via Razorpay: Make 5000 payment ⭐ If you would like to buy me a coffee, well thank you very much that is mega kind! : https://www.buymeacoffee.com/honeyvig
Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies
Showing posts with label apache. Show all posts
Showing posts with label apache. Show all posts

Saturday, October 3, 2020

Installing Apache Tutorial

 

This indepth tutorial by Blair Ireland covers everything you need to know about installing Apache, from getting started, through to configuring and optomizing it!

Key Takeaways

  • Apache is the most popular web server, widely used due to its open-source nature and robust features, allowing users to view and modify the source code.
  • Installing Apache involves downloading the software from the official Apache website, with separate instructions provided for Unix and Windows platforms.
  • For Unix systems, Apache installation includes downloading a tarball, decompressing it, and compiling the source code using commands in the terminal.
  • Windows users can install Apache using a setup wizard, which simplifies the process, although it’s noted that Windows support is experimental.
  • Configuration of the Apache server is user-friendly, with primary configuration files like httpd.conf, which can be edited to manage server operations and settings.
  • Apache allows the addition of modules like Server Side Includes (SSI) and CGI scripts to enhance functionality, and detailed error documents can be customized for various HTTP responses.
  • Apache’s performance can be optimized by managing file lookups, hostname lookups, and content negotiation, ensuring efficient operation under high traffic.
Tutorial Sections
  • Introduction
  • Starting Off
    • Download Apache
    • Compiling the Source
    • Using Apache

  • Configuring Apache
    • Know your Directives
    • Server Side Includes
    • Make those CGI’s Work
    • Error Documents
    • Content Negotiation
    • A Single Config File
    • Rotating Logs
    • Get some Extras
    • Buy a Book

  • Optimize your Apache Server
    • File Lookups
    • Hostname Lookups
    • Content Negotiation
    • Hardware
    • Server Processes

    Let us get started then, shall we?

    Introduction


    Most people associate quality and popularity with price. On the Web though, this is not the case. What is the most popular, highest quality Web server available?

    • The Netscape-Enterprise server? Wrong.
    • Microsoft-IIS? Wrong again
    • Apache? Bingo, and by a staggering amount.

    According to Netcraft’s December 1999 Web server survey (Slashdot, TheFinancial Times, Linux.com, and JavaSoft not to mention tons of other sites.

    The Apache Project (http://www.apache.org/) started back in 1995 when a group of Webmasters decided they needed a stable, powerful server for an upcoming site (one of these sites was http://www.hotwired.com/). At the time, the most popular server software available on the Web was developed by the National Center for Supercomputing Applications (NCSA). The developer, Rob McCool, left the organization and all development was stalled. At the time, Webmasters were making their own extensions and bug-fixes for the server, but these were not distributed in any way. The group of Webmasters decided to co-ordinate the changes to the server, which later became known as ‘patches’. This is how the Apache Server got its name: A-Patchy Server. A year after the release of the Apache Server, the small group of hackers had created the #1 server on the Internet.

    Being able to view the source code for the server is one of its biggest advantages. Not only can you modify and add features to the server yourself (providing you know C++, or Perl if using mod_perl), but the vast number of developers out there have made modules to add on to the server.

    Due to it’s popularity, we decided to create a group of tips and tutorials to help you install, configure/customize and optimize your Apache server. Since Apache is available for Unix and Windows, we will specify whether a tip is platform-specific.

    Starting Off


    Installing Apache and starting it up is generally thought to be a gruelling task, which it shouldn’t be. In fact, with these tips and tutorials, it should be quite easy for anyone. The installation of the Unix version of Apache is just like installing most other applications, you download the source, compile it, and voila! The Windows version also makes it easy; using the setup wizard, it’s just like installing any other Windows application.

    Anyway, first we need to download a copy of Apache. Since it is relatively small (the size ranges from 1 to 3MB, depending on the platform), this should be a painless procedure. After that, it’s just a matter of installing it. The following tips will make this procedure as painless as possible.

    Download Apache


    To get Apache, obviously you have to go to their site at http://www.apache.org/. Proceed to the downloads (http://www.apache.org/dist/) section or find the closest Apache mirror to you (http://www.apache.org/dyn/closer.cgi) to get your copy.

    If you would like the Unix version of Apache, go through the well-named list of files and grab the gzipped copy (apache_1.3.9.tar.gz). For the Windows version, download the Windows binary setup file (apache_1_3_9_win32.exe).

    Since installing the Windows version is easiest, I will talk about that first. After the file is downloaded, just open it as usual and the setup wizard will open and install the server for you. Since Apache has just started making Windows versions, the code is not as stable as it is for Unix. Windows support is entirely experimental, and is recommended only for experienced users. The Apache Group does not guarantee that this software will work as documented, or even at all. During the installation, you will be asked for

    • the directory to install Apache into
    • the start menu name
    • the installation type

    Typical installs everything except the source code. Minimum doesn’t install the manuals or source code, and Custom allows you to ‘customize’ what is installed.

    Now to the Unix version. Since you downloaded a “tarball” distribution, you have to decompress it. Simply type tar -zxf apache_1.3.9.tar.gz. A directory called apache_1.3.9 will be created. You will have to go to this directory to proceed to the next step and compile the source code.

    Compiling the Source


    With all the source code neatly placed in this directory (and it’s sub-directories), you will have to compile it to make it work. Here we will be using the GNU cc (gcc is the shortform) compiler to compile the source. It is free and distributed with most Unix operating systems. If it is not on your computer, download and install it from http://www.fsf.org/software/gcc/gcc.html.

    Anyway, once in the apache_1.3.9 directory, from the prompt type

    ./configure

    This will create a makefile with the default configuration. To change the configuration, you must edit apache_1.3.9/src/Configuration.tmpl before running configure. You can add/remove modules from the list, and tweak many other options. Normally though, you should just let it use the defaults, especially if this is your first time installing Apache.

    For more information on editing the Configuration file, check out the README.configure file included with the distribution.

    Two more command line commands to execute. Type

    make

    to compile the server, and

    make install

    to put it in the proper directories. The default is /usr/local/apache/, though this can be changed in Configuration.tmpl. Now that your Apache binaries have finally been installed, we can proceed with learning how to use them.

    Using Apache


    After all of this work, we are now ready to start running Apache. There are obviously two methods for running it, one is for Windows and the other is for Unix.

    Windows

    Since Apache is just another Windows application, just go to the start menu, then the Apache Web Server program group. If you are not running Windows NT, just select “Start Apache as console app”. If you are running Apache on NT though, you can hit the “Install Apache as Service (NT only)” option and Apache will be installed as a service on your computer. This means it will automatically start everytime NT is booted up.

    To shutdown the server, there are two other options available in the start menu that should be self-explanatory.

    Unix

    To startup Apache in Unix, all you have to do is run httpd. This would look something like this

    /usr/local/apache/httpd

    but this also depends on where you installed the binary. It will automatically use the configuration file it created on compilation called httpd.conf. If you would like to use another configuration file, you can use the -f argument.

    Ex. /usr/local/apache/httpd -f /usr/local/apache/conf/httpd.conf

    The Apache distribution does come with another method of starting, stopping, and restarting Apache. The script is called apachectl. In the Apache src directory (apache_1.3.9/src), type make at the prompt. You will see a few more files compiled. After make is done, go to the support directory (apache_1.3.9/src/support), where you will find a bunch of new files created. Among them, you’ll find a few helper scripts, including apachectl, htpasswd (used to make Apache passwords for protected directories), and rotatelogs (its use is explained later). There are a few other files, but these are the most important.

    Make a copy of these three files to your Apache binary directory. If you did not change the default settings, this would be located at /usr/local/apache/bin/.

    To start the server, use

    /usr/local/apache/bin/apachectl start

    There is also

    /usr/local/apache/bin/apachectl stop

    and

    /usr/local/apache/bin/apachectl restart

    available, obviously to stop or restart the server.

    To have Apache start upon booting up your system, make a copy of apachectl in your startup directory (typically /etc/rc.d/init.d or /etc/rc3.d/….)

    Check it all out to make sure everything is working. From your browser, check out http://localhost/ right afterwards. You will know if it worked or not if you see anything….

    Phew! Now that everything works, it’s time to configure our server.

    Configuring Apache


    Though it is generally thought to be scary to edit a configuration file for anything, for Apache, it doesn’t have to be. The configuration can be found in the apache_1.3.9/conf/ directory, and, though there are a bunch of files available, you only really need three of them.

    Firstly, there is httpd.conf, which contains directives and configurations relating to the operation of the server as a whole. For example, server logs and server management.

    Next on the list, there is srm.conf. This file contains the configurations for the management of resources in the filesystem, such as aliases, directory indexes, etc.

    Lastly, access.conf. This file contains information on access control in whatever directories you please. All the other files, well you can just leave them be.

    When you first install Apache though, the files are not named exactly like this, they seem to be named name.conf-dist. This means it is the distribution copy of the file. Since we like to have backup copies of anything and everything, just use a command similar to this (from unix)

    cp httpd.conf-dist httpd.conf

    Or, for windows, just copy the file, and rename it.

    Another note, before we begin, you must restart Apache before any changes to the configuration files take effect. This is because the files are loaded upon initiation, so these changes will not be loaded otherwise.

    Know your Directives


    The Apache documentation can be extremely helpful for users just starting to configure their server.

    At http://www.apache.org/docs-1.2/mod/directives.html, you can find a listing of available directives (configurations) for your server, with explanations as well. It should be noted though that the directives listed are for version 1.2, not the new 1.3 version. Most of them should be the same, but some may be gone, and new ones may exist.

    Doesn’t sound like a tip? Well, it’s hard to configure something exactly for your needs without knowing exactly what it can do. So, with that said, this is definitely an important tip.

    There is also a book available containing the directives. The Apache Web Server Installation and Administration Guide, which details the process of installing and administering the Apache Web Server, is a handy desktop companion. It also includes a printed version of the installation and general administration portions of the Apache documentation.

    Knowing your directives can also make your life more convenient. If you don’t like where Apache has set your DocumentRoot (where you put your HTML and other files), you can easily change it in httpd.conf with this directive:

    DocumentRoot /where/you/want/to/put/your/docs

    Or if you want to set where e-mails are to be sent if there are server problems, just use this directive:

    ServerAdmin you@your.address

    Now that should be an excuse to get out and learn more… it will make your life easier.

    Use Server Side Includes


    When you don’t just want to serve your users plain old HTML, but don’t have the expertise yet to write up some CGI scripts (or are just lazy), try out Apache’s Server Side Includes (SSI).

    So what is SSI?

    Basically, it is an HTML document containing special commands within. If it is named appropriately, Apache will pre-parse the document when it is requested, and send the resulting document. SSI is controlled by the module named mod_include.

    With SSI, you may do a variety of things, such as displaying some environment variables, displaying the date, or even including external files within your document.

    Before you can use SSI, you must make sure the mod_include module was compiled with the rest of Apache. It is included by default, but you may have commented it out when editing Configuration.tmpl earlier. If that is the case, uncomment the line, and re-compile Apache. Once that is done, open up your httpd.conf file.

    To use server-parsed HTML files, look for the section found below:

    AddType text/html .shtml
    AddHandler server-parsed .shtml

    Make sure you get rid of the comments in front of the lines (the #’s), as they are commented by default. Notice how they use .shtml? You can change this to any extension you want really, you just have to remember to name your SSI files later with these extensions.

    After you change these directives in the configuration files, restart Apache, and let’s make a test SSI file. Since we are using .shtml as our SSI extension, we will name our file test.shtml

    <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 3.2 Final//EN”>
    <HTML>
    <HEAD><TITLE> Test Server Side Includes </TITLE></HEAD>
    <BODY BGCOLOR=”#FFFFFF”>
    Current Date: <!–#echo var=”DATE_LOCAL” –>
    <P>Document Name: <!–#echo var=”DOCUMENT_NAME” –>
    <P>Environmental Variables:<BR>
    <PRE><!–#printenv –></PRE>
    </BODY>
    </HTML>

    If it looks right, and all your blanks are filled in, SSI is properly enabled on your server. Congratulations!

    For more information on SSI, check out TheScripts.com’s SSI section at http://ssi.thescripts.com/.

    Make those CGI’s Work


    After you graduated from SSI, it’s time to setup your server to use CGI’s. CGI stands for Common Gateway Interface, but for some reason people always confuse it with Perl, the most popular language used for CGI. In fact, CGI can be any language really, such as C/C++, Java, TCL, or Perl, plus many others.

    The module controlling CGI in Apache is called mod_cgi and is compiled by default. If you removed it from Configuration.tmpl though, you must add it back in, and re-compile Apache before you can proceed.

    The first method of enabling CGI is to create a particular directory that contains scripts. A ScriptAlias is just like a normal Alias, except the documents are treated as applications, and not treated as documents when requested by the client.

    A typical ScriptAlias setup looks like so

    ScriptAlias /cgi-bin/ “@@ServerRoot@@/cgi-bin/”

    This makes /cgi-bin/ an alias to your Server Root/cgi-bin/ directory. @@ServerRoot@@ reflects the ServerRoot variable you set near the top of the configuration file. You can write it any way really, such as

    ScriptAlias /cgi-bin/ “/usr/local/apache/cgi-bin/”

    or wherever you desire to put the cgi-bin directory. You would follow the above configuration with the following

    <Directory “/usr/local/apache/cgi-bin/”>
    Options ExecCGI
    AddHandler cgi-script .cgi .pl
    </Directory>

    The AddHandler part allows you to have CGI scripts with .cgi and .pl extensions in this case. Here is the other method. This method allows you to use CGI outsite of ScriptAliased directories. You would use the following configuration;

    AddHandler cgi-script .cgi .pl

    Notice it isn’t found inside a Directory container? This way, the directive AddHandler applies to all of the documents, not just the documents found within a certain directory. Here, we allow .cgi and .pl extensions once again for our CGI scripts. You can use whatever.

    CGI applications, when executed, are run under the same name as the user that owns the Apache server process. By default, the user is called ‘nobody’, as specified by the User and Group Directive. These directives can only work if the Apache server is started by the user root.

    User nobody
    Group #-1

    In most cases, you won’t have to change a thing here. This is not always suitable though, as, if you have multiple users on your system, you might want CGI’s to run under the name of the user. This is excellent for virtual hosting systems, as you could have the CGI run under the customer’s name and access their files.

    To counter this problem, Apache created the suEXEC program, included with the Apache server. To learn more about how to use it for your system, check out http://www.apache.org/docs-1.2/suexec.html.

    Error Documents


    Ever notice how some sites have their own error documents? For instance, try going here (http://www.thescripts.com/nopage.html). You get a 404 error, but not just any 404 error, we have a customized 404 error page. You can do this for any other error you can get as well (we also have a 403 error page). FYI, 404 errors mean the page was not found, and 403 means you do not have access to a particular document.

    So how do we do it? It’s actually quite simple. Instead of using Apache’s default/kinda ugly error pages, you can specify certain files to be returned for certain errors. The directive being used here is called ErrorDocument, and below we find an example

    ErrorDocument 404 /fourohfour.html

    This is, as you might have figured out, the 404 error page. It is located in this case in the htdocs directory, and called fourohfour.html. If you accessed it from the browser, it would be at http://www.yoursite.com/fourohfour.html

    To add another type of error document, for instance, 403, you would just do the following;

    ErrorDocument 403 /fourohthree.html

    So as you can see, the first argument in the directive ErrorDocument is the error number. The second is the location of the page.

    Just a note, it would be best to use full image and link paths in an error document. This is because using relative links/image paths would be inaccurate. For instance, if you went to http://www.thescripts.com/nothere/nothere.html, the fourohfour.html page would be served as if it was located in the nothere directory, yet it is really in the root directory of your html documents. For more information on the ErrorDocument directive, visit http://www.apache.org/docs/mod/core.html

    Content Negotiation


    I know what you are thinking, what is content negotiation? Well, first of all, it’s an often overlooked feature of Apache. It is more accurately known as content selection, and is the selection of documents that best match a client’s browser capabilities, from one of several available documents.

    Why do you need it?

    Well, you don’t… but, if you are aiming your Website at a multi-lingual region, then having it would greatly be to your benefit. Not only will your documents be correctly served to the visitors, but it will also improve their stay at your site.
    By default, content negotiation is compiled in with your server, as it is powered by mod_negotiation. If for some reason you didn’t compile this feature in with Apache, you must go back to your Configuration.tmpl file and put it back in.

    Ok, what do you need now?

    Well, you have to choose what languages you are going to be using, and have content available for both languages. There are actually two methods we can use: using a variants file, which can be found discussed here (http://www.apacheweek.com/features/negotiation), or, by using file extensions.

    For this example, we will be using three different languages, English, French and German.

    Now open up the access.conf file and let’s get started. Firstly, we will need to specify a directory, which we will call international, and have it so that it is set for content negotiation. The international directory will be located just off of the root document directory, which, in our case, is /usr/local/apache/htdocs/.

    <Directory /usr/local/apache/htdocs/international>
    Options MultiViews
    </Directory>

    Options MultiViews sets the directory so that the server does filename pattern matching to choose from among the results. Now, open up httpd.conf so we can add our languages to look for, and how the server will identify them. We will be using the directive AddLanguage

    AddLanguage en .en
    AddLanguage fr .fr
    AddLanguage de .de

    Here we have added English, French, and German, all identified with the extension found to their right.

    By default, quite a few languages are already set. You can comment them out and add to them as needed.

    The LanguagePriority directive allows you to give precedence to some languages in case of a tie during content negotiation. The languages are listed in decreasing order or preference.

    LanguagePriority en fr de

    So how do you name your files now? The best method is to do something like test.html.en for an English document, test.html.fr for a French document, and test.html.de for a German document. When you would like to link to this document, you just use test.html.

    For more information on Content Negotiation, see http://www.apache.org/docs/content-negotiation.html.

    A Single Config File


    Geez, why would this help?

    Well, it would put all your options into one single file, which makes life easy when you want to edit anything. In this case, we will use httpd.conf to store all of our directives. For some commentary on the three configuration files, see http://www.apache.org/info/three-config-files.html.

    Anyways, back to the single config file issue. Right now you are using three configuration files: httpd.conf, access.conf, and srm.conf. You don’t need to. Simply empty the important stuff from the latter two files into httpd.conf, and then also add these two lines:

    AccessConfig /dev/null
    ResourceConfig /dev/null

    This will inform Apache that httpd.conf is the only configuration file. With this in place, you can just simply remove the srm.conf and access.conf files, and proceed with a smile on your face.

    Rotating Logs


    So what are these log files you keep hearing about?

    Every time a user requests a document from your site, the server ‘logs’ a record of this request into a log file. Log files contain vital statistics about users, such as their host, the date/time, and the request line, which contains browser information.

    If your site is even remotely busy, or it’s just been a while since you’ve done anything to the logs, they will probably be rather large. Typically, they can reach many MB’s in a short period of time. Since the best application for log files is for log analysis with other software, large log files can slow down this process to a crawl.

    So, what can you do about it? Use log rotation.

    Log rotation is a procedure that takes a log file, puts its contents into a new file, and clears its own contents afterwards. Since its a rather boring procedure, there have been a few scripts to help you do it, including one from the Apache group itself.

    One script, for example, is a small utility written to allow log files to be quickly split and processed. This utility will create a new log file for each day, and with the date as the extension. Once the new log file is created, the old one can be compressed or moved to a new location. The utility is called logbox, and is available from ftp://ftp.lemuria.org/pub/Code/logbox.tar.gz. You must compile it on your server before you can use it.

    Another method of splitting files is made by the Apache group. Earlier you compiled it, which was also when you compiled apachectl and htpasswd. If you used the default settings, you also copied it to /usr/local/apache/bin/ afterwards. This program is called rotatelogs (how ingenious!), and it is used with the TransferLog directive in Apache.

    In httpd.conf, add something like this:

    LogFormat “%h %l %u %t “%r” %s %b “%{Referer}i” “%{User-agent}i””
    ErrorLog /usr/local/apache/logs/error_log
    TransferLog “|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/access_log 86400”

    Let me explain this…

    The directive LogFormat really isn’t anything to worry about actually. It just specifies the template format for how the access and error logs will look. For more information on the options LogFormat provides, see http://www.apache.org/docs-1.2/mod/mod_log_config.html.

    ErrorLog sets the name of the file to which the server will log any errors it encounters. This is important to look over from time to time to see what might be going wrong with your site/server.

    Now, to the part you’ve been waiting for.

    The TransferLog directive. Here, we are mixing it up with the rotatelogs program at the same time. Technically, there is one argument to TransferLog here, but it can be broken down. The first part is the location to our rotatelogs program (in this case, it is at /usr/local/apache/bin/rotatelogs).

    The second part is the location of our access_log file (The file in which we log our server requests, located at /usr/local/apache/logs/access_log). The last part sets the amount of time before rotating the log files. It is in seconds, and since 86400 seconds is equal to 1 day (24 hours), we rotate the file every day. You can set this to whatever time period you would like. The generated name of the old log file (after each rotation) will be /usr/local/apache/logs/access_log.nnnn in this case, where nnnn is the system time the log started at. After each rotation time, a new, empty log is started.

    Get some Extras


    Since Apache is open source, there are a ton of extras built for it. These extras can greatly enhance the performance of your server, and even give you more possibilities with your Website. Two of the most notable add-on’s include mod_perl, and mod_php

    mod_perl is an excellent binding of the Apache Web server and Perl (http://www.perl.com/), the popular CGI scripting language. The module takes a copy of the Perl interpreter, and embeds it within Apache itself. This not only speeds up existing CGI’s, but it also allows you to write more modules in Perl itself. The Perl scripts are compiled once this way, unlike the usual method. Usually, the scripts are compiled on run-time by the interpreter, which makes them run a little slower from the start. If they are already compiled though, they start instantaneously, making this module an excellent addition to a Website with high levels of traffic.

    mod_php is another great addition to Apache. The powerful server-side scripting language, PHP (http://www.php.net/), which is also open-source/free, can easily be linked with the server upon compilation. PHP scripts can be run like normal CGI’s, but this method allows them to be run at a much greater speed.

    Both of these add-ons must be compiled in with Apache from the start though, as they are modules. If you would like to use them, just download them, edit your Configuration.tmpl file, run configure, and compile as usual.

    There are instructions for these modules though, as they are a little more complicated. You can the documentation at http://perl.apache.org/ and http://www.php.net/ respectively.

    Buy a Book


    I must say this is the easiest place to find excellent tips. Not only are they always available to you (as it should be sitting next to you), but if you don’t have a laptop computer in the washroom, you can just read it there as well.
    There are a few good books, and, as usual, my favourites are published by O’Reilly.

    Apache: The Definitive Guide

    This book boasts one of the members of the Apache development team. It begins with an academic discussion of what Web servers do before walking the reader through the process of installing Apache. The installation of Apache gets a lot of attention, and you are taught about Website security and other preferences.

    Apache Server Bible

    The Apache Server Bible is an excellent guide to administering your Apache Web server, and is aimed at those with no previous Web administration experience. Topics include compiling source code, installation, configuration issues.

    Apache Server for Windows Little Black Book: Little Black Book

    This book will show you how to put Apache’s capabilities to work. The book even moves on to things like CGI, database management, encryption and more.

    Optimize your Apache Server


    Once your Website becomes popular, you will have to start worry about another issue: how to get the most out of your server. With the traffic loads pounding it constantly, you will have to find a way to optimize it to handle the traffic a bit better.

    That is where this part of the tutorial comes into play. Before we begin, I must state that all of these tips will be aimed at unix servers, as Apache is not optimized for Windows at all.

    When looking at speed issues and Apache, we must look at the hardware/operating system, your configuration files, and how you compiled the server from the start. For those of you that want to read all of your performance options, check out http://www.apache.org/docs/misc/perf-tuning.html and http://www.apache.org/docs/misc/perf.html. Also you might want to check out the O’Reilly book, Web Performance Tuning.

    File Lookups


    This is something you might not have even thought of. The two culprits here are simply .htaccess files and symbollic links. For those of you that are looking at the page funny, .htaccess files are a type of access control file. They can contain Apache Directives, and apply to the directory they are in and everything below it, overridden only by other .htaccess files.

    By default, the Apache Directive AccessFileName is set to .htaccess, so it is known as that unless you changed it. So you can really think of it as just another Apache configuration file, though it is optional.

    When returning a document to the client the server looks for an access control file with this name in every directory of the path to the document, if access control files are enabled for that directory. For example, say I was getting a document found at http://www.mysite.com/test/test2/test3/test4.html and my DocumentRoot directive was set to /usr/local/apache/htdocs. Apache would look for an .htaccess file in the following directories;

    /
    /usr
    /usr/local
    /usr/local/apache
    /usr/local/apache/htdocs
    /usr/local/apache/htdocs/test
    /usr/local/apache/htdocs/test/test2
    /usr/local/apache/htdocs/test/test2/test3

    Do you see what I’m getting at?

    With access control files enabled, Apache has to do quite a bit of checking for unnecessary files, which robs you of some performance.

    So how do we fix this problem?

    We can use the Directory container to specify no overrides are acceptable within the container. In this case, we use the root directory.

    <Directory />
    AllowOverride None
    </Directory>

    After that, we may want to be able to still use .htaccess files. If this is the case, you can enable them again,

    <Directory /usr/local/apache/htdocs>
    AllowOverride All
    </Directory>

    And there you go. The AllowOverride directive has a few options actually, to see them, check out http://www.apache.org/docs-1.2/mod/core.html.

    Symbollic links have a similar issue, and the fix is nearly the exact same, except different directives are used.

    <Directory />
    Options FollowSymLinks
    </Directory>

    Followed by

    <Directory /usr/local/apache/htdocs>
    Options -FollowSymLinks +SymLinksIfOwnerMatch
    </Directory>

    Now we got rid of the file lookup problem, as there are no unnecessary file checks.

    Hostname Lookups


    Apache, by default, has the directive HostnameLookups set to off. This is a good thing, as, with it on, each client request to your server would result in a lookup request to the nameserver.

    This is also a problem if you are restricting access to particular sections of your site with the order directive, accompanied by allow and deny. It is best if you use IP addresses in this case, as otherwise, you have to do another nameserver lookup.

    With that out of the way, your Apache server doesn’t have to do any unnecessary DNS stuff, and can stick to it’s job. If your CGI’s, PHP or whatever else might require using hostname lookups, you can use a file container to specify this.

    <Files ~ “.(php3|cgi)$>
    HostnameLookups on
    </Files>

    Content Negotiation


    Here I go again with my fancy words, content negotiation. I know I suggested using it earlier, but that’s only if it would benefit you. Otherwise, you should stay away from it. Why? Since it requires searching for particular files, and not exact names, the searching part puts a little more load on the server, therefore slowing it down just a tad. Also, for directives that ask you for files, such as DirectoryIndex, instead of just putting index, put the actual file name. The example is below;

    DirectoryIndex index

    Index is a wild card, as there is no extension specified, and it has to do some searching again to find the file. Putting a list of names would be a much quicker, more efficient method. Put the most common name first, and in descending order.

    DirectoryIndex index.html index.shtml index.cgi

    Hardware


    As with most software, Apache needs RAM, especially if you get a lot of traffic. If it gets to the point that your operating system has to start swapping memory, with the hard drive, you must either get more RAM or set the directive MaxClients a little lower.

    Swapping memory isn’t necessarily slow, but for the client, it is. They will get irritated, hit stop-reload a few times, and further the load on your server. Not a good thing.

    Besides that, with a fast enough hard drive, network card and CPU, your server should perform quite well (hardware-wise at least). With a little experimentation, you should be able to find an optimal configuration.

    Server Processes


    Settings such as MinSpareServers, MaxSpareServers, StartServers, and KeepAliveTimeout (if KeepAlive is set to ‘On’). The SpareServers directives instruct the server how many ‘child’ processes to create. Apache uses these directives to determine the load it has to adapt to.

    If your server has children waiting for requests, and the amount exceeds the MaxSpareServers setting, some of those child processes will be killed. If the amount is less than MinSpareServers, some child processes will be created.
    The settings of these directives will directly relate to your server performance. If you don’t have enough child processes to handle the current server load, some requests will be delayed, causing your server to be slower. Too many processes will also slow down your server, if you don’t have enough RAM and CPU power to handle it.

  • Frequently Asked Questions (FAQs) about Installing Apache

    What are the prerequisites for installing Apache?

    Before you start the installation process, ensure that you have a functioning Linux operating system. You also need to have sudo privileges to execute commands. It’s also recommended to update your system packages to the latest versions. You can do this by running the command sudo apt update and sudo apt upgrade.

    How can I verify if Apache is installed and running correctly?

    After installing Apache, you can verify its status by using the command sudo systemctl status apache2. If Apache is running correctly, you will see an active status in the output. You can also check by typing your server’s IP address into a web browser. If Apache is correctly installed, you should see the Apache Ubuntu default welcome page.

    How can I configure Apache to start on boot?

    To ensure Apache starts automatically when your system boots, use the command sudo systemctl enable apache2. This command creates a symbolic link that points to the Apache service file, enabling it to start at boot.

    How can I troubleshoot common Apache errors?

    Apache logs can be a great source of information when troubleshooting. The error log, located at /var/log/apache2/error.log, contains messages from Apache with information about events, errors, and diagnostic information. The access log, located at /var/log/apache2/access.log, contains information about requests coming in to your web server.

    How can I secure my Apache server?

    There are several ways to secure your Apache server. One of the most common methods is to install a firewall and limit access to your server. You can also configure Apache to use SSL for secure connections. Regularly updating your server and Apache software can also help keep your server secure.

    How can I host multiple websites on a single Apache server?

    Apache allows you to host multiple websites on a single server using a feature called virtual hosts. Each virtual host file corresponds to a different website. You can create a new virtual host file for each website you want to host.

    How can I customize the Apache welcome page?

    The default Apache welcome page is located in the /var/www/html directory. You can replace the index.html file in this directory with your own HTML file to customize the welcome page.

    How can I install specific Apache modules?

    Apache functionality can be extended with modules. You can install specific modules using the a2enmod command followed by the module name. For example, to install the rewrite module, you would use the command sudo a2enmod rewrite.

    How can I uninstall Apache?

    If you need to uninstall Apache, you can do so with the command sudo apt-get remove apache2. To remove Apache and its configuration files, use the command sudo apt-get purge apache2.

    How can I update Apache to the latest version?

    You can update Apache to the latest version by first updating your system packages with the command sudo apt update. Then, you can upgrade Apache with the command sudo apt upgrade apache2.

    Friday, September 27, 2013

    Custom php.ini settings for PHP FCGI

    If you want to enable custom php.ini settings for PHP FCGI on your server then follow the below steps :
    1. Create a .htaccess file under the directory : /home/username/public_html
    cd /home/username/public_html
    vi .htaccess
    Add the below two lines at the top :
    AddHandler php5-fastcgi .php
    Action php5-fastcgi /cgi-bin/php.fcgi
    save and quit.
    2. Go to the cgi-bin directory : /home/username/public_html/cgi-bin
    cd /home/username/public-html/cgi-bin
    and then copy the default php.ini here
    cp /usr/local/lib/php.ini /home/username/public_html/cgi-bin/
    3. Create php.fcgi file to load custom php.ini
    Please note that you should still be in /home/username/public_html/cgi-bin location.
    vi php.fcgi
    and then add the below code :
    #!/bin/sh
    export PHP_FCGI_CHILDREN=1
    export PHP_FCGI_MAX_REQUESTS=0
    exec /usr/local/cpanel/cgi-sys/php5
    save and quit.
    Now make the file php.cgi executable:
    chmod + x php.fcgi
    4. Change ownership of files to correct user:
    chown username.username * -R
    And if it still does not work for you then follow the below steps as well :
    5. Add paths to php.conf file
    vi /usr/local/apache/conf/php.conf
    and add the below lines :
    Action php5-fastcgi /cgi-bin/php.fcgi
    AddType application/x-httpd-php .php
    save and quit.
    and after adding the above two lines the php.conf file should look like this :
    root@server[/usr/local/apache/conf] # cat php.conf
    # Fastcgi configuration for PHP5
    LoadModule fcgid_module modules/mod_fcgid.so
    MaxRequestsPerProcess 500
    AddHandler fcgid-script .php5 .php4 .php .php3 .php2 .phtml
    Action php5-fastcgi /cgi-bin/php.fcgi
    AddType application/x-httpd-php .php
    FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php5
    FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php4
    FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php
    FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php3
    FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php2
    FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .phtml
    # End of autogenerated PHP configuration.
    6. Distill and rebuild Apache, then restart Apache
    /usr/local/cpanel/bin/apache_conf_distiller –update
    /scripts/rebuildhttpdconf
    /etc/init.d/httpd restart

    Friday, November 18, 2011

    10 Things Every Good Web Developer Should Know (A Checklist)



    10 Things Every Good Web Developer Should Know (A Checklist)
    My favorite thing about web development is the wide variety of skills it uses. Being a good developer means staying on your toes and always learning, because there are so many important skills and these are constantly changing.
    Here’s a checklist of 10 skills that are some of the most important for good web developers to have. These focus a bit on front-end skills, but they apply to all developers:

    1. Regular expressions

    regex
    Regular expressions are used to parse strings, basically to find and replace bits of text. While this may seem trivial, the versatility of regex makes it quite powerful. There are a wide variety of methods for optimizing a regex string, which can mean huge performance differences for your app.
    Regular expressions can be used with just about any language, they work great with Javascript’s replace() and PHP’s preg_replace() to name a few. They are also great for Apache rewrites.
    It’s surprising how few web devs use regex considering how useful it is. So if you haven’t already, make sure to learn regular expressions.

    2. Basic SEO

    SEOmoz
    A knowledge of basic SEO is essential for any good web developer. While you can leave keyword research to your clients, a developer should have sound strategies for targeting those keywords on the page. This means clean markup and good use of </code>, <code><meta></code>, <code><h1></code> and <code><h2></code> tags on the front-end, as well as solid link and directory structure organization on the back-end. </p> <p>The best resources I’ve found for learning SEO are the <a href="http://www.seomoz.org/article/beginners-guide-to-search-engine-optimization">Beginner’s Guide to Search Engine Optimization</a> and <a href="http://www.seomoz.org/article/search-ranking-factors">Search Engine Ranking Factors v2</a>, both from SEOmoz.</p> <p><a name="js-library"></a></p> <h2>3. A good Javascript library</h2> <p><img src="http://jonraasch.com/img/blog/jquery-logo.gif" alt="jQuery javascript library" class="leftImg"></p> <p>Even if you don’t write much Javascript, knowing a good Javascript language can make that little bit so much easier. Whether it’s <a href="http://jquery.com/">jQuery</a> (my personal favorite), <a href="http://mootools.net/">Mootools</a>, or whatever, learning a Javascript library will cut your Javascript dev time in half. First, these libraries take the cross-browser guess-work out of Javascript programming so you can stop feature checking and start animating. Additionally, they provide a ton of easy animation options and make selecting nodes in the DOM a piece of cake.</p> <p>More adventurous Javascripters might even want to tackle a Javascript UI language. There are plugins for the libraries above, but I’d recommend <a href="http://www.dojotoolkit.org/">Dojo with Dijit</a>.</p> <p><a name="cross-browser"></a></p> <h2>4. Cross-browser debugging</h2> <p><img src="http://jonraasch.com/img/blog/cross-browser.png" alt="cross-browser" class="leftImg"></p> <p>In order to reach as many viewers as possible, your websites need to work in all the major browsers (IE6, IE7, IE8, FF2, FF3, Safari 4, Opera 9). Everyone hates cross-browser debugging, but the better you get the less you’ll have to do it.</p> <p>The best cross-browser debugging is preventative, starting with valid HTML and a good reset stylesheet. For my reset CSS, I just use <code>* { margin: 0; padding: 0 }</code> but you can also use <a href="http://meyerweb.com/eric/tools/css/reset/">a more robust option</a>. </p> <p>Beyond this it helps to understand the various <a href="http://www.quirksmode.org/compatibility.html">quirks specific to certain browsers</a>. Top of this list is usually Internet Explorer, and in particular IE6 (yes, still). And while most IE issues boil down to the infamous <a href="http://www.satzansatz.de/cssd/onhavinglayout.html">haslayout bug</a>, you shouldn’t rely on something heavy-handed like <code>* { zoom: 1 }</code> to solve these.</p> <p><a name="images"></a></p> <h2>5. Image processing and optimization</h2> <p><img src="http://jonraasch.com/img/blog/photoshop-cs4.png" alt="photoshop" class="leftImg"></p> <p>Although most developers rely on professional designers for design assets, it is just as important for devs to know their way around Photoshop. A strong knowledge of image processing not only allows you to make quick changes to comps without bouncing them back to the designer, it also helps you to understand what is possible when asking for design changes.</p> <p>One Photoshop skill that is particularly important for webdevs is image optimization. Image file sizes are probably the largest threat to bandwidth and optimizing images can make or break a site’s load time. Photoshop and Illustrator’s “Save for Web” option makes image optimization a lot easier: simply press Ctrl-Alt-Shift-S on PC or Command-Alt-Shift-S on Mac to access this wizard.</p> <p><a name="valid-html"></a></p> <h2>6. Building layouts with valid HTML/CSS (and no tables)</h2> <p><img src="http://jonraasch.com/img/blog/box-model.gif" alt="CSS box model" title="The CSS box model"></p> <p>Let’s face it: it’s not 1996 and even back-end developers need to know how to write good front-end code, meaning valid HTML and CSS—not a bunch of tables. The markup should be clean and validate according to it’s doctype and the CSS should be separated from the HTML, which means no inline styles and especially no <font> tags.</p> <p>For starters learn the <a href="https://developer.mozilla.org/en/CSS_Reference">various CSS tags</a> and the <a href="http://jessey.net/simon/articles/003.html">CSS box model</a>. From there you should learn W3C specifications on how to write valid code.</p> <p>Whenever you write HTML it should naturally be valid xHTML; meaning properly nested nodes, closed image tags with specified alt attributes and other basics. Then you can go on to learn which nodes can be nested within which, and other more complex validation issues. In the end you’ll find that the <a href="http://validator.w3.org/">w3c’s HTML validator</a> hardly ever throws an error, which is a really good feeling.</p> <p><a name="law"></a></p> <h2>7. Copyright / cyber law basics</h2> <p><img src="http://jonraasch.com/img/blog/justice-illustration.png" alt="law" class="leftImg"></p> <p>You don’t need a law degree, but it’s best for all developers to have at least a basic knowledge of copyright and cyber law wherever they do business.</p> <p>Since websites are essentially publications, copyright issues tend to be the most common legal issue for sites. From user generated content with stolen Flickr photos to quoting paragraphs from the New York Times, it’s important to know when you’ll get sued, when you’ll get an angry letter and when you’re okay. It’s also good to know when to sue and when to send an angry letter <img src="http://jonraasch.com/blog/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley"> .</p> <p>Beyond copyright laws it’s good to understand privacy and <a href="http://www.coppa.org/comply.htm">COPPA</a> regulations as well as read carefully through any agreements you have with registrars / hosting companies (and clients!).</p> <p><a name="rewrites"></a></p> <h2>8. Apache rewrites</h2> <p><img src="http://jonraasch.com/img/blog/apache-logo.jpg" alt="apache http" class="leftImg"></p> <p>Apache rewrites make it easy to take messy URLs and make them clean and easy to type (if you know <a href="http://jonraasch.com/blog/10-things-every-web-developer-should-know-2#regex">regular expressions</a>). You can take something ugly like <code>index.php?section=about&page=bio</code> and rewrite it to the much prettier: <code>about/bio</code>.</p> <p>But beyond simply cleaning URL’s, Apache’s mod_rewrite can be used to quickly turn a simple page into a complete app with a directory structure. Simply rewrite the various query string variables to be directories, and you’re all set. </p> <p>However be careful: Apache rewrite issues are notoriously hard to debug. The rewrites can’t provide any debugging info themselves and will often skip over info you include through other methods. So be aware, rewrites can cause a nightmare that you can’t track down.</p> <p><a name="debugger"></a></p> <h2>9. Debugging Software</h2> <p><img src="http://jonraasch.com/img/blog/firebug.jpg" alt="firebug debugger" class="leftImg"></p> <p>A good debugger will make your web programming go so much quicker. </p> <p>When it comes to the front-end, I like <a href="http://getfirebug.com/">Firebug</a>, which allows you to click elements on the page and analyze which styles are being applied and from which stylesheet. You can additionally add or remove styles on the fly, as well as edit the page markup. </p> <p>Firebug also helps with Javascript debugging, providing a better analysis of Javascript errors, as well as a console for running Javascript on the fly and displaying output from <code>console.log()</code>. Finally it provides NET info, showing you the loading time of various front-end assets as well as anything that is 404′ing or otherwise not loading.</p> <p>I don’t use a back-end debugger but I hear <a href="http://xdebug.org/">xDebug</a> is good for PHP.</p> <p><a name="version-control"></a></p> <h2>10. Version control</h2> <p><img src="http://jonraasch.com/img/blog/mercurial.gif" alt="Mercurial VC" class="leftImg"></p> <p>Last but definitely not least, every good developer should know and use version control. Version control not only is the best way to backup files as you work on them, it is really the only way to effectively collaborate with other developers. </p> <p>Version control works by keeping a record of the changes in a directory’s contents. As you work you can commit any changes to this record, or roll back to any previous version.</p> <p>I personally like <a href="http://blog.ianbicking.org/distributed-vs-centralized-scm.html">distributed as opposed to centralized</a> version control, since it provides two levels of VC: you can commit changes to your local repository and then push these changesets to a centralized server. This allows you to make a number of small or potentially unstable changes locally, then push a set of final changes to your team.</p> <p>When it comes to distributed VC I like <a href="http://mercurial.selenic.com/wiki/">Mercurial</a>, and for centralized I like <a href="http://subversion.tigris.org/">Subversion</a>.</p><p> </p><h2>Now what do you think?</h2> <p>Did I leave anything off the checklist above? What do you think of the items on there? They aren’t in any order, but is any item more or less important? Please post any thoughts in the comments below.</p><p> </p></DIV>