Pages

Showing posts with label Apache. Show all posts
Showing posts with label Apache. Show all posts

Saturday, July 18, 2015

Apache - Host a YUM Repository

There will be cases where we need to download various packages from internet. in most of these cases the YUM repository will be hosted on a web server. In this article we will see how we can Host a YUM repository on a Apache web server.

1) Create a location mkdir /var/www/html/myrepo. The location /var/www/html is the Apache Document root and this is available in Apache configuration file httpd.conf. If you are going for a new location make the necessary changes

2) Copy some of the packages to the location and run the createrepo command

[root@vx111a test]# createrepo /var/www/html/myrepo
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 0 pkgs
Spawning worker 2 with 0 pkgs
Spawning worker 3 with 0 pkgs
Spawning worker 4 with 0 pkgs
Spawning worker 5 with 0 pkgs
Spawning worker 6 with 0 pkgs
Spawning worker 7 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

Once the createrepo is done, we can see a repodata directory in the same /var/www/html/myrepo

[root@vx111a myrepo]# ll
drwxr-xr-x. 2 root root    4096 Jul 17 18:52 repodata
-rw-r--r--. 1 root root 2489408 Jul 17 18:51 zsh-5.0.2-7.el7.x86_64.rpm

3) Once the repodata is created we need to set the permissions on the location as

chmod o+r /var/www/html/myrepo -R
chcon -R httpd_sys_content_t /var/www/html/myrepo

The above last command need to execute only if Selinux is in enforcing mode

4) Once the above steps are done we need to configure the repo file for the client location in /etc/yum.repos.d/myrepo.repo as
[myrepo]
name=my custom repo
baseurl=http://apacheserver/myrepo
enabled=1
gpgcheck=0
5) We can also access the apache server where we can download the packages too.

Hope this helps, More to come
Read More

Apache - mod_proxy_ajp

This summary is not available. Please click here to view the post.
Read More

Apache – Multi Processing Modules

The Multi-Processing modules also called as MPM are the ones that are responsible for binding to network ports on a machine, accepting requests and dispatching children to handle the requests. So when ever a request comes to the Apache server the MPM modules takes the request and assigns one of its Child to the request for processing.

One important thing about this that only one MPM module can be active at any time in the HTTPD server. This is like any other Apache module but only one will be loaded at time.

The reason why MPM came into existence is to allow different ways that a server is build to handle HTTP requests within the Computing Constrains.

How does this Work?
There exists a Single Control Master process which is responsible for launching multiple Child process which takes the incoming HTTP requests for processing. Apache always tried to maintain several spare ( not-in-use ) process which will be ready for serving incoming requests. In this way client does not need to wait for child Processed to be forked before their requests can be severed.

Installing MPM
As said earlier only one MPM can be active at a time. The Installation of MPM can be done when we are building the Apache HTTPD server from source by passing an argument
--with-MPM=<Module Name>

Available MPM
Apache Server supports multiple types of MPM. Of all the available the important ones are Prefork and Worker.

By default Prefork modules is being used as the default one for Apache. There are certain major differences in selecting the MPM for Production uses

Apache Prefork  MPM - This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server that handles requests. This is appropriate for web sites that need to avoid threading for compatibility with non-thread safe libraries. This is said to be the best MPM for isolating requests so that problem with a Single request does not effect any others.

This MPM implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with less system resources than a process-based server.  This also keeps spare process with threads in order to handle more load of requests

Apache Worker MPM – This MPM is best used threaded environment. This is used with Apache with modules loaded in do not have thread safety issues. However, creating processes on Linux is generally fast enough that you don't need to use worker. The worker MPM really shows off it's value on platforms that have very heavy-weight processes, such as AIX.

The Major Differences are

Prefork – uses multiple Child Process with one thread each and each process handles one connection at a time. Each request gets its own Memory-separated Process

Worker – uses multiple Child Process with many thread each. Each thread handles one Connection at a time. This is considered to be faster than the Prefork MPM.

How to find which MPM is loaded
In order to find which MPM the Current HTTPD process is using, we can run the command

[root@localhost ] httpd –V
Server MPM: Prefork

Configurations Files
The configuration of Prefork or Worker MPM exists in the conf.modules.d/ location in the Apache location with name mpm.conf

We can un-comment the LoadModule of the MPM we want to use.

Hope this Helps, More to come on configuring the MPM
Read More

Thursday, July 9, 2015

Apache – mod_proxy_balancer

We have seen many Apache modules which provides various benefits like load balancing support , security, URL rewriting and Proxy passing etc. In this article we will see one more Apache module called mod_proxy_balancer which works as a load balancer. We use this module when we have a stateless applications which does not require any sticky sessions or clustering but need load balancing support.

1) Load the module mod_proxy_balancer by adding the below line to the httpd.conf

LoadModule proxy_balancer_module "/opt/ers40jk/apache2.2/modules/standard/mod_proxy_balancer.so"

2) Once the module is loaded we need to add the set handler element for mod_proxy_balancer element as

<IfModule mod_proxy_balancer.c>
<Location "/balancer-manager">
  SetHandler balancer-manager
  Order deny,allow
  Allow from all
</Location>

3) Now we need to add the URL from Back end so that Apache can send the requests. The configuration looks as

<Proxy balancer://localhost>
         BalancerMember http://172.16.202.95:18011 loadfactor=1
         BalancerMember http://172.16.202.96:18010 loadfactor=2
</Proxy>

4) Once we have added the necessary back end URL to send requests , we need to provide the proxy pass elements so that we can use this in the browser URL

ProxyPass /myApp balancer://localhost/myApp/
</IfModule>

This example is setup for round robin load balancing where the second BalanceMember processes 2 of every 3 requests. You can adjust this and also the load balancing method based on your needs. Some different load balancing methods mentioned in the doc are byrequests, bytraffic and bybusyness.

Here is the total configuration in my httpd.conf file

<VirtualHost *:8580>
  
    DocumentRoot /var/www/virtual/www.sam1.com/html/
    ServerName myproject.local
    ErrorLog logs/dummy-www.sam1.com-8380-error_log
    CustomLog logs/dummy-www.sam1.com-8380-access_log common
 
    <Directory "/var/www/virtual/www.sam1.com/html/">
        Options None
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
        Allow from all
    </Directory>
 
     <IfModule mod_proxy_balancer.c>
          <Location "/balancer-manager">
            SetHandler balancer-manager
            Order deny,allow
            Deny from all
            Allow from all
          </Location>

        <Proxy balancer://localhost>
             BalancerMember http://172.16.202.95:18011 loadfactor=1
             BalancerMember http://172.16.202.96:18010 loadfactor=2
        </Proxy>

             ProxyPass /myApp balancer://localhost/myApp/
       </IfModule>

</VirtualHost>

We access the same application deployed in both tomcat instances back end running on IP address 172.16.202.95 and 96. The application is accessed using myproject.local:8580/myApp


Hope this helps, More to Come J
Read More

Tuesday, June 30, 2015

Apache – SSL Configuration

This summary is not available. Please click here to view the post.
Read More

Serving Static Content in Apache

This summary is not available. Please click here to view the post.
Read More

Monday, October 20, 2014

Apache – Custom Error Page

Error handling is one of the important parts when running a server. The application running inside the server provides the necessary services to the client but it is the responsibility of the server to serve custom pages for errors. In this article we will see how we can configure custom error page 404 for Apache web server along with Tomcat.

1. Create a 404 error page as
<html>
<head> Hello This a 404 Error page </head>
<body bgcolor="green">
</h3> this is green and 404 </h3>
</body>
</html>

Copy the file to /var/www/webroot/ROOT/ location

2. Once the file is created, configure Apache Configuration file as

<VirtualHost *:80>
   ServerAdmin webmaster@dummy-host.example.com
   DocumentRoot /var/www/webroot/ROOT/
   ErrorDocument 404 /404.html
   ServerName dummy-host.example.com
   ErrorLog logs/dummy-host.example.com1-error_log
   CustomLog logs/dummy-host.example.com1-access_log common
   ProxyRequests Off
   ProxyPreserveHost On

   <Proxy *>
    Order deny,allow
    Allow from all
    Options +Indexes
   </Proxy>

   ProxyPass /application1 http://localhost:8080/myApp/
   ProxyPass /application2 http://localhost:8080/Sample-app/test.html

   ProxyPassReverse /application1/ http://localhost:8080/myApp/
   ProxyPassReverse /application2/ http://localhost:8080/Sample-app/test.html
</VirtualHost>

All we need to do is add an ErrorDocument entry to our http.conf file. On the ErrorDocument line, we specify the error code, which in this case is a 404. After that, we can specify either a text message or the page to display and then file to be displayed ( 404.html ). Restart the Apache server.

Now once we access a different path, we see the 404 error page as,


Read More

Wednesday, August 20, 2014

Configure Multiple Instances of Apache (HTTPD) on Same Server

There are many cases where I need to work with multiple instances of Apache Server. This article tells you on how to configure multiple instances of Apache on the same server.

1. Duplicate the Http Configuration File
    cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd1.conf

2. Duplicate the conf.d location
    cp -pr /etc/httpd/conf/conf.d/  /etc/httpd/conf/conf.d2/

3. Change the PID file Name, Listen port number and include the conf.d2 location
    Change PidFile run/httpd.pid to run/httpd.pid2
    Listen 80 to Listen 81
    Include conf.d2/*.conf

4. Start the Second Instance using
    httpd -f /etc/httpd/conf/httpd1.conf -k start

5. Start the First Instance using
     Start httpd start

We can confirm using “netstat –tulpn | grep 80|81” Or we can access the localhost:80 , localhost:81


More to come, happy learning J
Read More

Thursday, August 7, 2014

Apache Load Balancer Module with Tomcat

Load Balancing is one of the important Aspect when running production servers. Load balancing provides many additional benefits in the production environment. In this article we will see how we can configure Apache Http Server to load balance requests that are going to the back end Tomcat server. For the demo purpose I will have multiple tomcat instances with the same application running on the context and configure apache to load balance requests for the application from Apache server.


1. Make sure to load mod_balancer_proxy.so module ion the httpd.conf file like
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

2. Now Configure the Balancer for the Apache Http server like,

<IfModule proxy_module>
ProxyRequests Off
ProxyPass /myApp balancer://mycluster stickysession=JSESSIONID
ProxyPassReverse /myApp balancer://mycluster stickysession=JSESSIONID

<Proxy balancer://mycluster>
BalancerMember http://localhost:8080/myApp route=jvm1
BalancerMember http://localhost:18080/myApp route=jvm2
</Proxy>

</IfModule>

In the above configuration we have defined our web heads and declaring how they will be balanced. The BalancerMember is how we declare our web heads,

All of this will be wrapped in <Proxy> tags which is how apache knows to send it to mod_proxy, the "balancer://mycluster" identifier is only an identifier, you could technically call it what you want as long as you put the "balancer://" prefix

3. Configure the Back end Tomcat instances. Since we have to test the load balancing, I have configured 2 tomcat instances with port: 8080 and 18080. We also need to change the JVM route to jvm1 and jvm2 respectively.

Deploy the application on both tomcat instances. In the above case I deployed a sample application with the context “myApp”.

Once the configuration is done, restart Apache Http server and the backend Tomcat instances.

We can access the application using https://localhost/myApp

Access the application multiple times and check the access logs in the tomcat instances  and we can see

Tomcat 1
0:0:0:0:0:0:0:1 - - [07/Aug/2014:17:45:46 +0530] "GET /myApp/ HTTP/1.1" 200 415

Tomcat 2
0:0:0:0:0:0:0:1 - - [07/Aug/2014:17:45:04 +0530] "GET /myApp/ HTTP/1.1" 200 41

We can see from the above access logs ,that the request is being processed by both tomcat instances

Optional Step

This is a tool packaged with the mod_proxy_balancer tool, and allows you to make configurations from a gui tool through the web browser. Viewable at "http://domain.com/balancer-manager", keep in mind that these changes die after you restart apache. I won't go over how to use this tool, but it is available to you.For this to work we need to add the below statements to http.conf file

<Location /balancer-manager>
   SetHandler balancer-manager
</Location>


More to Come ,Happy Learning
Read More

Apache Server Status – mod_status module

In a production environment we will be maintaining many number of Apache instances for processing the user requests. It is hard for a administrator to manage these multiple instances of the apache servers.

Apache provide various numbers of modules for various operations. There is one module called mod_status.so which provides out-of-the-box functionality for monitoring the performance of Apache installations. In this article we will see how we can configure the mod_status module and how we can read the status.

1. Make sure the mod_status module is loaded like
LoadModule status_module modules/mod_status.so

2.Configure Apache configuration like

ExtendedStatus On

<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from localhost 172.16.101.228
</Location>

3. Now we can access http://localhost/server-status which will give various information regarding the Apache instances like,

We can also use http://localhost/server-status?refresh=15 which will make the status page refresh for every 15 seconds.

The header contains basic information about the Apache installation like

Server Version: Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3
Server Built: Apr 9 2011 08:58:28


There is a status summary containing basic status information like,

Current Time: Thursday, 07-Aug-2014 17:15:35 IST
Restart Time: Thursday, 07-Aug-2014 16:57:40 IST
Parent Server Generation: 0
Server uptime: 17 minutes 55 seconds
Total accesses: 10 - Total Traffic: 34 kB
CPU Usage: u0 s0 cu0 cs0
.0093 requests/sec - 32 B/second - 3481 B/request
1 requests currently being processed, 7 idle workers

__W_____........................................................
.......................................................................
.......................................................................
.......................................................................


The bottom of the status page contains information about current requests like,

Srv   PID   Acc     M      CPU   SS Req Conn  Child  Slot Client VHost Request
0-0 19126 0/2/2 _ 0.00 45     0   0.0  0.01   0.01 localhost localhost.localdomain GET /server-status?refresh=15 HTTP/1.1

The mod_status module helps us to obtain various information about the Apache Installations.

More to Come, happy Learning :-)
Read More

Tuesday, August 5, 2014

Reverse Proxy with apache Web Server and Tomcat

A proxy is a piece of software that supports sending and receiving data on behalf of another application. It’s an intermediate layer on your network that receives requests from within the networks, forwards them to the appropriate host, reads the response, and sends the response back to the requesting host or application within the network.

There are 2 types of proxy available,
  • A forward proxy is an Internet-facing proxy used to retrieve from a wide range of sources.
  • reverse proxy is usually an Internet-facing proxy used as a front-end to control and protect access to a server on a private network. A reverse proxy commonly also performs tasks such as load-balancing, authentication, decryption or caching.

In this article we will see how we can configure Apache as a Proxy server that will connect to the back end Tomcat Servers. For the demo purpose I have deployed 2 applications in the back end Tomcat server.

1. Build Apache server with Proxy modules

Download the Apache HTTP server source
$ tar -xzvf httpd-2.0.36.tar.gz
$ cd /httpd
$ ./configure –enable-module=proxy
If you’re using Apache 2.0, you should use this instead:
$ ./configure –enable-proxy –enable-proxy-ftp –enable-proxy-http –enable-cache –enable-disk-cache
Once the source tree has been prepped, you can compile and install Apache with a quick
$ make
$ make install

2. Add Configurations to the Apache http.conf
a. make sure the LoadModule for the mod_proxy.so is added like

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so


b. add the virtual host element

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    ServerName dummy-host.example.com
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common
    ProxyRequests Off
    ProxyPreserveHost On

    <Proxy *>
     Order deny,allow
     Allow from all   
</Proxy>

  ProxyPass /application1 http://localhost:8080/myApp/
  ProxyPass /application2 http://localhost:8080/Sample-app/test.html
  ProxyPassReverse /application1/ http://localhost:8080/myApp/
  ProxyPassReverse /application2/ http://localhost:8080/Sample-app/test.html
</VirtualHost>

ProxyRequest  and ProxyPassReverse are the 2 elements that make the Reverse Proxy work.

# Disable forward proxy requests
ProxyRequests Off

# Configure reverse proxy requests for myApp
ProxyPass /application1 http://localhost:8080/myApp/
ProxyPassReverse /application1/ http://localhost:8080/myApp/

So now we have configured /application1 in Web server and when we access
http://localhost/application1 the web server will send the requests to
http://localhost:8080/myApp/ which is running in back end Tomcat.

ProxyPass allows you to is basically take a request for a URL, and forward it to another(Usually backend) server. Then it forwards the response back to the client .The last ProxyPassReverse directive captures the responses from the Tomcat server and masks the URL as it would be directly responded by the Apache HTTPd hiding the identity/location of the Tomcat server.


More to Come , Happy Learning J
Read More