Sunday 9 February 2014

Apache httpd server how to complete guide (part 1 linux on fedora ubuntu)

httpd server

HTTP/1.1 (RFC2616) compliant web server support for DBM databases as well, as relational databases and LDAP for authentication Allows you to set up files, or even CGI scripts, which are returned by the server in response to errors and problems, e.g. setup a script to intercept 500 Server Errors and perform on-the-fly diagnostics for both users and yourself Documentation httpd.apache.org/docs/2.2/

COMMANDS

/etc/init.d/httpd start /etc/init.d/httpd stop /etc/init.d/httpd restart /etc/init.d/httpd reload /etc/init.d/httpd status service httpd reload sudo service httpd restart Restarting httpd (via systemctl): [ OK ] [root@servo user5]# service httpd status httpd.service - LSB: start and stop Apache HTTP Server Loaded: loaded (/etc/rc.d/init.d/httpd) Active: active (running) since Thu, 17 May 2012 19:46:07 +0200; 14s ago Process: 2552 ExecStart=/etc/rc.d/init.d/httpd start (code=exited, status=0/SUCCESS) CGroup: name=systemd:/system/httpd.service ├ 2557 /usr/sbin/httpd ├ 2571 /usr/sbin/httpd ├ 2572 /usr/sbin/httpd ├ 2573 /usr/sbin/httpd └ 2574 /usr/sbin/httpd [root@servo user5]#

VARIABLES

* Document Root /var/www/html * Configuration Files /etc/httpd/conf /etc/httpd/conf.d/welcome.conf * HTML location /var/www/html/ Configuration: All of the files for the sites are located in directories that exist underneat /srv/www symbolically link these directories into other locations. LOCALHOST http://localhost reads virtualhost reads /srv/www/example.com/public_html/index.php If not reads Apache /etc/httpd/conf.d/welcome.conf

Configuration

Apache web server But there is a rather nice and quick debugging trick for getting Apache to just dump out a list of all its running virtual hosts. The command line is simply this: # httpd -D DUMP_VHOSTS Which outputs: VirtualHost configuration: 10.241.53.10:443 secure.website.com (/etc/httpd/conf.d/secure_website.conf:4) wildcard NameVirtualHosts and _default_ servers: *:80 is a NameVirtualHost default server alpha.website.example.com (/etc/httpd/conf.d/website.conf:1) port 80 namevhost beta.website.example.com (/etc/httpd/conf.d/website.conf:1) port 80 namevhost gamma.website.example.com (/etc/httpd/conf.d/website.conf:10) port 80 namevhost delta.website.example.com (/etc/httpd/conf.d/website.conf:19) port 80 namevhost website.sky.com (/etc/httpd/conf.d/website.conf:29) Hurrah! That’s a list of the URLs of all virtual hosts, and the conf.d files in which they’re configured. And better yet, if you run this command with the “-t” switch, it will instead parse the configuration files, and not the running configuration: # httpd -t -D DUMP_VHOSTS So you can test your configuration (note that it also performs a syntax check) before loading it. Finally, when you’re satisfied that your changes are correct, reload the configuration: # apachectl configtest # apachectl restart Or, on Fedora: # apachectl configtest # service httpd reload Special bonus Apache debug information! If you run httpd on the command line with the “-L” switch, you get a list of all recognised directives, their descriptions, and the contexts in which they are allowed. It’s extremely handy to have this information to hand, and to not have to revert to the manual every time. # httpd -L , or , or , or .... top - 21:31:18 up 7 min, 2 users, load average: 0.05, 0.55, 0.40 Tasks: 165 total, 1 running, 164 sleeping, 0 stopped, 0 zombie Cpu(s): 0.9%us, 1.3%sy, 0.0%ni, 97.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1926428k total, 518752k used, 1407676k free, 49900k buffers Swap: 6158332k total, 0k used, 6158332k free, 268676k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1951 root 20 0 76456 15m 7292 S 1.2 0.8 0:06.29 Xorg 2436 user5 20 0 458m 18m 14m S 0.6 1.0 0:03.65 cairo-dock 9 root 20 0 0 0 0 S 0.3 0.0 0:01.26 kworker/1:0 1071 root 20 0 3352 488 376 S 0.3 0.0 0:00.16 irqbalance 1822 root 20 0 92580 13m 8420 S 0.3 0.7 0:00.29 httpd 2689 user5 20 0 182m 9364 7484 S 0.3 0.5 0:00.88 lxterminal 2897 user5 20 0 2728 1156 888 R 0.3 0.1 0:00.78 top 1 root 20 0 5632 3812 1808 S 0.0 0.2 0:01.92 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.07 ksoftirqd/0 4 root 20 0 0 0 0 S 0.0 0.0 0:01.76 kworker/0:0 6 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 7 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0 8 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/1 10 root 20 0 0 0 0 S 0.0 0.0 0:00.09 ksoftirqd/1 11 root 20 0 0 0 0 S 0.0 0.0 0:03.09 kworker/0:1 12 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/1 13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 cpuset 14 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper 15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns 16 root 20 0 0 0 0 S 0.0 0.0 0:00.00 sync_supers 17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 bdi-default 18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd 19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd 20 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ata_sff 21 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khubd 22 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md 23 root 20 0 0 0 0 S 0.0 0.0 0:01.30 kworker/1:1 26 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kswapd0 27 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd 28 root 39 19 0 0 0 S 0.0 0.0 0:00.01 khugepaged 29 root 20 0 0 0 0 S 0.0 0.0 0:00.00 fsnotify_mark 30 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto 36 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kthrotld 43 root 20 0 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_0 [user5@servo ~]$ sudo service htpd status [sudo] password for user5: htpd: unrecognized service [user5@servo ~]$ sudo service httpd status httpd.service - LSB: start and stop Apache HTTP Server Loaded: loaded (/etc/rc.d/init.d/httpd) Active: active (running) since Sat, 29 Dec 2012 21:25:20 +0100; 6min ago Process: 1549 ExecStart=/etc/rc.d/init.d/httpd start (code=exited, status=0/SUCCESS) CGroup: name=systemd:/system/httpd.service ├ 1822 /usr/sbin/httpd ├ 1908 /usr/sbin/httpd ├ 1909 /usr/sbin/httpd ├ 1910 /usr/sbin/httpd └ 1911 /usr/sbin/httpd [user5@servo ~]$ sudo service mysqld status mysqld.service - LSB: start and stop MySQL server Loaded: loaded (/etc/rc.d/init.d/mysqld) Active: active (running) since Sat, 29 Dec 2012 21:25:25 +0100; 7min ago Process: 1550 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS) Main PID: 1803 (mysqld) CGroup: name=systemd:/system/mysqld.service ├ 1596 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysq... └ 1803 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log... [user5@servo ~]$ APACHA [user5@servo ~]$ rpm -q httpd httpd-2.2.21-1.fc15.i686 rpm -q httpd If rpm generates output similar to the following, the apache server is already installed: [user5@servo ~]$ rpm -q httpd httpd-2.2.21-1.fc15.i686 Alternatively, if rpm generates a "package httpd is not installed" then the next step, obviously, is to install it. This can be performed either from the command-line or using the Add/Remove Software tool. To use this tool, open the System->Administration desktop menu and select Add/Remove Software. Enter your password if prompted to do so. Click in the Find text field and search for httpd. After the search completes the Apache HTTP Server should be listed in the search results. Click on the toggle next to the httpd server. Finally, click on Apply in the toolbar to begin the installation. To install Apache from the command-line start a terminal window (Applications->System Tools->Terminal) and run the following command at the command prompt: su - yum install httpd /sbin/service httpd status If you would like the Apache httpd service to start automatically when the system boots, this can be achieved by running the following command: /sbin/chkconfig --level 3 httpd on Configuring a Basic Fedora Linux Firewall To launch the standard Fedora configuration tool, open the desktop System menu and click on Administration and select Firewall. Alternatively, the tool can be launched from the command-line as follows: system-config-firewall Apache Configuration If you are feeling lazy and want to use a GUI, Fedora provides a graphical interface for configuring Apache. Install system-config-httpd: yum install system-config-httpd Then, run the tool as root: /usr/bin/system-config-httpd You can also configure the httpd.conf file manually. See the Apache documentation for details on how to configure your web server. vim /etc/httpd/conf/httpd.conf vim /etc/httpd/conf.d/README Installing Apache Web Server The basic Apache functionality, serving static web sites, is provided by the httpd rpm package. To install Apache, run: su -c "/usr/bin/yum install httpd" Unless the requirement is to run a very basic web site, the more convenient way of installing the Apache web server is: su -c "/usr/bin/yum groupinstall 'Web Server'" In addition to thehttpd package, the command above installs software needed to run Apache with the support for database driven web sites, support for common web scripting languages, such as PHP, perl, and python, Apache documentation provided by httpd-manual rpm package and support for serving secure, encrypted content through HTTPS protocol. The Web Server package group can be installed during Fedora installation, using the option to customize software packages." To start the Apache server, run: su -c "systemctl start httpd.service" To test the correct operation of the Apache server, point the web browser to http://localhost. If the browser displays Fedora Test Page, the Apache is installed correctly. To configure the Apache server to start at the boot time, run: su -c "systemctl enable httpd.service" Configuring Apache web server There are a few characteristic directories that contain files needed for proper operation of the Apache web server: /etc/httpd:: The location of Apache configuration files, referred to as ServerRoot. /usr/lib/httpd/modules:: The location of various Apache modules, loaded on demand from the main configuration file. /var/www:: Default location for storing web site content. /var/log/httpd:: The location of the Apache log files. The main Apache configuration file is /etc/httpd/conf/httpd.conf. At the minimum, there are only two directives in this file that need to be specified to enable Apache to serve the content over the Internet, The name to which server responds and the location of the web site content on the system. To serve the web content for www.example.com, these two entries are: ServerName www.example.com:80 DocumentRoot "/var/www/html" Reload the configuration file for these changes to take effect: su -c "systemctl reload httpd.service" This configuration assumes that www.example.com resolves correctly in DNS and that the content for the web site is in the /var/www/html, the default DocumentRoot in Fedora." /etc/httpd/conf/httpd.conf file includes instructions for almost all of the configuration options in the form of comments, ie. the lines beginning with # character. This feature makes the configuration file very long and does not allow quick changes to it. However, the Include directive within a file provides a way for splitting the configuration file into smaller, more manageable sections. The line: Include conf.d/*.conf causes the httpd daemon to read all *.conf files placed in the /etc/httpd/conf.d directory, in addition to to a main configuration file, during start up process. The common use of conf.d/*.conf files is to have separate configuration files for various Apache extensions or virtual hosts. /etc/httpd/conf/httpd.conf includes numerous options for configuring the Apache web server. Other notable options are: Performance tuning: MaxClients limits the number of allowed simultaneous connections to the server and works together with the ServerLimit option. KeepAlive allows for a number of concurrent HTTP requests over a single TCP connection. TimeOut instructs the httpd daemon when to stop responding if it is under heavy load. Apache performance tuning is the art of managing the trade-off against the benefit. It requires good understanding of the server's capabilities and seldom improves by including arbitrary parameters. Log configuration: ErrorLog points to the location where the server's errors are logged. LogLevel sets the verbosity of the ErrorLog entries. CustomLog points to the location where the requests are logged. The Apache web server logs the requests and errors to /var/log/httpd/access_log and /var/log/httpd/error_log by default. Other configuration options: AddLanguage associates files with certain extension to certain languages. Useful when the web server serves pages in multiple languages. LoadModule loads dynamically shared objects. ScriptAlias specifies the location of CGI scripts. ServerAdmin specifies who is the server administrator. AddHandler maps scripts to script handlers, such as .cgi, .php, etc. Refer to the Apache documentation in the Additional Information section for the extensive list of the Apache configuration options and their usage. Virtual Hosts The Apache web server has the ability to serve the content for multiple sites from the single server through the concept of Virtual Hosts. Virtual hosts can be configured in two ways: IP based Virtual Hosts: Each virtual host has its own IP address and port combination. Required for serving HTTPS requests, due to restrictions in the SSL protocol. Name based Virtual Hosts: All virtual hosts share the common IP address. The Apache web server responds to the request by mapping the host header in the request to ServerName and ServerAlias directives in the particular virtual host's configuration file. The example of the simple name based virtual hosts configuration: # global configuration options NameVirtualHost *:80 ServerName localhost ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/virtual/example.com/html ServerName foobar.com ServerAlias www.foobar.com DocumentRoot /var/www/virtual/foobar.com/html The order in which the virtual hosts are listed is significant to the extent that the Apache will always serve the content from the first listed virtual host in case the request was made for the site that is resolvable in DNS but not defined as a ServerName or a ServerAlias. Once the first VirtualHost is defined, all of the content served by Apache must also be moved into virtual hosts. Security Considerations Apache File Security By default, the httpd daemon runs as the user and group apache. Therefore, all files that the httpd needs to access to operate properly must be accessible by user apache. The safe way to accomplish this is to set the ownership on all of the files to another user and allow read-only access to all other users. For example, to allow read-only access to www.foobar.com content, so it can be served over the Internet, run the following: su -c "/bin/chown -R root.root /var/www/virtual/foobar.com" su -c "/bin/chmod 755 /var/www/virtual/foobar.com /var/www/virtual/foobar.com/html" su -c "/bin/chmod 644 /var/www/virtual/foobar.com/html/*" In case the content should be readable by the Apache and nobody else, the good practice is to change the group ownership to group apache and deny access to others. User-level ownership on files should be granted to the apache user only if the web server is expected to modify the files, for example, through the use of CGI scripts. Apache Access Controls To control the access to the content served by the Apache web server, use the Order, Deny, and Allow directives, within the Directory container directive. To allow access to the content of www.foobar.com: Order deny,allow The Order directive controls the behavior of how the access to the content is evaluated and sets the default precedence if Allow and Deny directives are not defined: Order deny,allow defaults to "allow access" Order allow,deny defaults to "deny access" The latter value always overrides the former. For example, to allow access to all hosts on the 192.168.1 subnet and deny the host with the 192.168.1.66 IP address, add these options: Order allow,deny Allow from 192.168.1. Deny from 192.168.1.66 SELinux Notes The best way to avoid SELinux errors while running Apache is to store the Apache related files in the default system locations. If this is not possible, the solution is to change the SELinux context on non-standard directories, using default ones as a reference: su -c "/usr/bin/chcon -R --reference=/etc/httpd/conf /path/to/new/conf" or su -c "/usr/bin/chcon -R --reference=/var/www/html /path/to/site/content" 4.1. The Apache HTTP Server and SELinux When SELinux is enabled, the Apache HTTP Server (httpd) runs confined by default. Confined processes run in their own domains, and are separated from other confined processes. If a confined process is compromised by an attacker, depending on SELinux policy configuration, an attacker's access to resources and the possible damage they can do is limited. The following example demonstrates the httpd processes running in their own domain. This example assumes the httpd package is installed: Run getenforce to confirm SELinux is running in enforcing mode: $ getenforce Enforcing The getenforce command returns Enforcing when SELinux is running in enforcing mode. Run service httpd start as the root user to start httpd: # service httpd start Starting httpd: [ OK ] Run ps -eZ | grep httpd to view the httpd processes: $ ps -eZ | grep httpd unconfined_u:system_r:httpd_t:s0 2850 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2852 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2853 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2854 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2855 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2856 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2857 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2858 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2859 ? 00:00:00 httpd The SELinux context associated with the httpd processes is unconfined_u:system_r:httpd_t:s0. The second last part of the context, httpd_t, is the type. A type defines a domain for processes and a type for files. In this case, the httpd processes are running in the httpd_t domain. SELinux policy defines how processes running in confined domains, such as httpd_t, interact with files, other processes, and the system in general. Files must be labeled correctly to allow httpd access to them. For example, httpd can read files labeled with the httpd_sys_content_t type, but can not write to them, even if Linux permissions allow write access. Booleans must be turned on to allow certain behavior, such as allowing scripts network access, allowing httpd access to NFS and CIFS file systems, and httpd being allowed to execute Common Gateway Interface (CGI) scripts. When /etc/httpd/conf/httpd.conf is configured so httpd listens on a port other than TCP ports 80, 443, 488, 8008, 8009, or 8443, the semanage port command must be used to add the new port number to SELinux policy configuration. The following example demonstrates configuring httpd to listen on a port that is not defined in SELinux policy configuration for httpd, and, as a consequence, httpd failing to start. This example also demonstrates how to then configure the SELinux system to allow httpd to successfully listen on a non-standard port that is not already defined in the policy. This example assumes the httpd package is installed. Run each command in the example as the root user: Run service httpd status to confirm httpd is not running: # service httpd status httpd is stopped If the output differs, run service httpd stop to stop the process: # service httpd stop Stopping httpd: [ OK ] Run semanage port -l | grep -w http_port_t to view the ports SELinux allows httpd to listen on: # semanage port -l | grep -w http_port_t http_port_t tcp 80, 443, 488, 8008, 8009, 8443 Edit /etc/httpd/conf/httpd.conf as the root user. Configure the Listen option so it lists a port that is not configured in SELinux policy configuration for httpd. In this example, httpd is configured to listen on port 12345: # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 Listen 127.0.0.1:12345 Run service httpd start to start httpd: # service httpd start Starting httpd: (13)Permission denied: make_sock: could not bind to address 127.0.0.1:12345 no listening sockets available, shutting down Unable to open logs [FAILED] An SELinux denial similar to the following is logged to /var/log/messages: setroubleshoot: SELinux is preventing the httpd (httpd_t) from binding to port 12345. For complete SELinux messages. run sealert -l f18bca99-db64-4c16-9719-1db89f0d8c77 For SELinux to allow httpd to listen on port 12345, as used in this example, the following command is required: # semanage port -a -t http_port_t -p tcp 12345 Run service httpd start again to start httpd and have it listen on the new port: # service httpd start Starting httpd: [ OK ] Now that SELinux has been configured to allow httpd to listen on a non-standard port (TCP 12345 in this example), httpd starts successfully on this port. To prove that httpd is listening and communicating on TCP port 12345, open a telnet connection to the specified port and issue a HTTP GET command, as follows: # telnet localhost 12345 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.0 HTTP/1.1 200 OK Date: Tue, 31 Mar 2009 13:12:10 GMT Server: Apache/2.2.11 (Fedora) Accept-Ranges: bytes Content-Length: 3918 Content-Type: text/html; charset=UTF-8 [...continues...]

VirtualHost

/etc/httpd/conf.d/vhost.conf configure virtual hosting can host multiple domains (or subdomains) with the server. These websites can be controlled by different users. Configure Name-based Virtual Hosts By default, Apache listens on all IP addresses available to it. Create virtual host entries for each site that we need to host with this server. ErrorLog and CustomLog entries are suggested for more fine-grained logging, but are not required. If they are defined (as shown above), the logs directories must be created before you restart Apache. 2 examples for sites at "example.com" and "example.org" defined in /etc/httpd/conf.d/vhost.conf ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /srv/www/example.com/public_html/ ErrorLog /srv/www/example.com/logs/error.log CustomLog /srv/www/example.com/logs/access.log combined ServerAdmin webmaster@example.org ServerName example.org ServerAlias www.example.org DocumentRoot /srv/www/example.org/public_html/ ErrorLog /srv/www/example.org/logs/error.log CustomLog /srv/www/example.org/logs/access.log combined VirtualHosts GET / HTTP/1.1 Host: www.ruturaj.net EXAMPLE ServerName ruturaj.net ServerAlias www.ruturaj.net DocumentRoot /www/domains/ruturaj.net CustomLog logs/ruturaj.net-access_log combined ErrorLog logs/ruturaj.net-error_log DirectoryIndex index.php ServerAdmin ruturaj@ruturaj.net NameVirtualHost 67.66.65.64:8080 Once you've set the NameVirtualHost, you need to set the actual VirtualHost configuration as well. there is just once change to be made... ... > Important: You should note that all the domains, ruturaj.net, www.ruturaj.net, yourname.com, should always resolve an IP address on which NameVirtualHost is defined. Without which, the configuration does not make any sense. Use name-based virtual hosting: NameVirtualHost *:80 VirtualHost *:80> ServerName www.example.net ServerAlias example.com Redirect permanent / http://www.example.com/
ServerName www.example.com DocumentRoot

httpd conf and .htaccess

"httpd.conf" and ".htaccess"in httpd.conf in apache-install-dir/conf ServerName Sets the default server name, generally FQDN (Fully Qualified Domain Name) of the machine, or the IP Adress Directory This is a setting which encloses any of the settings for the given directory AllowOverride The AllowOverride allows the user, to override some of the settings by using their own file. This own file is the magical .htaccess file. By default it is set to None, which means the user can't override the settings by specifying the .htaccess file in the directory. But you can change the AllowOverride None setting to AllowOverride All OptionsThis directive takes several options Indexes: This allows a directory listing FollowSymLinks This allows apache to follow symbolic links, symbolic links are nothing but links in *nix systems, eg. "files" in /etc/ can point to /files/myfiles/files You can use both these options at once by Options +Indexes -FollowSymLinks The above setting will allow directory listing but won't allow Symbolic links. So "+" to apply and "-" to remove the setting AccessFileName About the magic file .htaccess, This is the place where you specify the name of the file, By default it is ".htaccess" The . "period" start is to make it a hidden file in *nix systems Denying files To deny files over the web, is the job of the server, in apache, we can do exactly by using the Files directive. Order allow,deny Deny from all Satisfy All Note the ~ sign, this is used when you are giving a regular expression to match the files., Once the files are selected, they can be denied by using the Deny directive. The above regex is to deny all the files that start with a ".ht" Access

Logs

To create access logs, we need to specify the format of the log, and the file path. First we need to set the LogFormat directive.The most common is the "combined" log, which logs ip, user, time error code, referer and user agent LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined Note: the log format has been given a name "combined", feel free to create different formats for your needs and name it accordingly Then we need to set the filename of the log, CustomLog /usr/local/apache/logs/access_log common The second parameter of the CustomLog directive which sets the filename of the log is the log format name, that we defined earlier. Server-Status When you want to look at the current status of the server, ie whom is it responding to, what pages is it serving, how many servers are running... and so on.. There is no better way than to set server-status To enable it ... SetHandler server-status Order deny,allow Deny from all Allow from 192.168.0.84 check the configuration, it is allowing only IP 84 to check the stats and others are forbidden. You can set your IP as you wish. If you want even more info. you can set the Extended status ExtendedStatus On Configurations ServerName: this is the main servername, it should be domain name ServerAlias: this is an alias, eg www.ruturaj.net should mean same as ruturaj.net on HTTP You can set anything like default.ruturaj.net as well. Just make sure that default.ruturaj.net points to 67.66.65.64 DocumentRoot: This is the main directory that points to ruturaj.net domain, this is the file system path to the directory CustomLog: This is the access_log for ruturaj.net, remember, we'd set the variable of "combined" log format, we are useing it here, if you want a different format, you can specify the LogFormat before specifying the CustomLog directive ErrorLog: Any errors while serving are logged in this file DirectoryIndex: Defines the default document page for root, eg when you do http://ruturaj.net/ it tells the server to serve "index.php", so you can set it whatever you want default-page.html, default.pl, etc. Multiple DirectoryIndex directives Allows you to say DirectoryIndex index.html index.cgi, which instructs the server to either send back index.html or run index.cgi when a directory URL is requested error log /usr/local/apache/logs/error_log ErrorLog directive in your config files for the location on your server. If your problem involves the server crashing and generating a core dump, please include a backtrace (if possible). As an example, # cd ServerRoot # dbx httpd core (dbx) where mod_info list any directives The mod_info module allows you to use a Web browser to see how your server is configured. Among the information it displays is the list of modules and their configuration directives. The "current" values for the directives are not necessarily those of the running server; they are extracted from the configuration files themselves at the time of the request. mod_rewrite mod_rewrite as described in the "Canonical Hostnames" section of the URL Rewriting Guide. http://httpd.apache.org/docs/current/rewrite/rewrite_guide.html#canonicalurl. allow, deny Order deny,allow Deny from all Allow from env=linked_from_here Directive listen Listen 8080 Alternatively you can also specify the IP on which it should listen. Listen 67.66.65.64:8080 Now if you want to run a Name-based VirtualHost on a specific, you make sure that you set the NameVirtualHost directive to a specific port as well.

.htaccess

The file's extension is "htaccess" and has no initial filename (*nix's legacy system) Any configurations that are present in the .htaccess file are available in all the subfolders of the Folder where .htaccess is code in the .htaccess file DirectoryIndex rutu-default.php All the sub directories or folders in directory ruturaj.net will have rutu-default.php page as the default index page. To tell Apache which is the standard Configuration file modify the entry in the httpd.conf file. AccessFileName is the parameter which specifies which file is the ".htaccess" file by default, the value of the parameter is set to ".htaccess" AccessFileName .htaccess There is also another parameter, AllowOverride, which tells Apache whether to read and implement the AccessFileName. You need to make the foll. settings in your VirtualHost or Directory mapping as AllowOverride All This will enable the implementation of the .htaccess file.

URL Rewriting

grab the specific content from the URL and then map that content, id, or whatever from the url as a get parameter's value to a specific page. In the above scenario, check the URL: /news/75-news-title.html. What is commonly done is the content_id the key by which the content is mapped in the content table is placed in the URL along with the title. As in this case Content ID: 75 Title Text: news-title (The hyphens are to make it readable instead of %20 for space) So lets assume that we have a page called news.php in which we will give a get parameter as newsid. All we got to do now is write the URL Rewrite rule using Apache's mod_rewrite engine. We'll use the RewriteCond and RewriteRule RewriteEngine On RewriteCond %{REQUEST_URI} /news/([0-9]+).*\.html$ RewriteRule (.*) /news.php?newsid=%1 [L] Now Let us look how we built the thing. First we used the Server Variable REQUEST_URI, to match the pattern with the request. The variable is referenced using %{SERVER_VARIABLE} format. The RewriteCond is basically a If condition, which means if the condition is true, only then the condition or rules below that statement will be executed. That means the pattern should match for the rule to work The regular expression pattern we made was accepting a integer value after /news/, After that integer value any text can come. But should end with .html. As emphasized by the $ at the end. Now if the Condition works, we need to write the rule for it, so we use RewriteRule. The first argument is .*, which means accept any URL The second argument is the actual mapping of the news.php with the newsid parameter. Check that we've used %1 which means the first back reference of the RewriteCond regex pattern Since our pattern was /news/([0-9]+).*\.html$ and had just one class in it, that class i.e. ([0-9]+) should be referenced by %1 in the RewriteRule directive

No comments:

Post a Comment