top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How does apache runs a application when a request comes in?

+2 votes
2,133 views

I have a python web application running on apache2 deployed with mod_wsgi. The application has a thread continuously running. This thread is a ZeroMQ thread and listening to a port in loop. The application is not maintaining session. Now if I open the browser and sends a request to the apache server the data is accepted for the first time. Now when second time I send the request It shows Internal server error. When I checked the error log file for traceback, It shows the ZMQError:- The address already in use.

Does apache reloads the application on each request sent from the browser since so that the ZeroMQ thread is being created everytime and being assigned the port but since the port has already been assigned it shows error....

posted May 6, 2014 by Prakash

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+2 votes
 
Best answer

It looks like your application is using zmq to bind to some port.

As you have suspected already, each request can be run as independent process, thus competing in access to the port to bind to.

There can be so called workers, each running one process processing http/wsgi requests, and each trying to bind.

You shall redesign your app not to use bind, but connect, this will probably require having another process with zeromq serving something you do with that (but this last line is dependent on what you do in your app).

answer May 6, 2014 by anonymous
You mean to say that I should put the ZMQ logic in some another process and each worker process of the apache2 when handling the request should talk to it?? Right?
+2 votes

It sure sounds like that's what's happening. Have you tried putting some logging statements in your code so that you can see what's going on?

If someone else here knows more about mod_wsgi he can update us. Failing that, it seems that mod_wsgi is specifically supported over at:

https://groups.google.com/forum/#!forum/modwsgi

answer May 6, 2014 by anonymous
Yes I Have put logging information in the code and everything is working fine except this part of it.
+1 vote

Long story short, yes. But it's more complicated. Apache can do one of several things to handle multiple requests, by pre-forking subprocesses of by spinning up threads within a listening process or a combination of both. If you first process is still running when your second request comes in, it will fail as you've seen. However, even if it terminates the kernel will hold that socket in a FINWAIT state for some amount of time, usually 2 minutes in Linux.

You might try to keep the process running indefinitely, but poorly written web application can leak resources and quickly cause httpd to crash. Because of this httpd is very strict about enforcing execution
time limits on processes invoked to handle web responses and forcibly killing long running threads. It's actually quite difficult to get a long running process to spawn off from httpd.

I did this for one project, I fork/exec'd off a long running process, then returned a link to the user that when clicked would communicate with the forked process via an RPCXML connection it would listen on to
return the status of the job. It was extremely difficult to get apache to not kill the process, you essentially had to follow all the steps of creating a daemon process to disassociate the subprocess from the apache
daemon, I then coded a watchdog thread to be sure it killed itself off after some amount of time.

A better design would probably be to start you ZMQ process independently of the server, then use something like XMLRPC or something RESTful to allow web requests to communicate with it and quickly receive the results they need. Not knowing more about your requirements, that's the best I can propose.

Hope this helps.

answer May 8, 2014 by anonymous
Thanks, I got what u are saying. It's really helpful.
I got a solution by using this
WsgiDaemonProcess processes=1 threads=10
Now do u think it will work
Also Not requests are accepted at the same time so i think it should work. And in fact at present it is working fine.
Similar Questions
+2 votes

I am developing a web application using flask, Werkzeug and jinja2. I am very much confused with these terms and wanted to know the meaning of the terms and how they are interrelated to the CGI environment variables. What is global variable g and how it is related to the application context and request context.

Also since I don't have much knowledge of developing web apps( I am doing it for first time) any another language also so there is another request if someone could give a reference or make me understand that how the requests are handled, i mean what happens when a request arrives to the web application.
Also if i am not using any openID providers for logging in the user into my website, how can i make the password secure. Should i use any framework for that?

+1 vote

I've tinkered with running a website using Apache on Linux for a few years now, but in my earlier days, I was a little naive and didn't pay too much attention to permissions.

Now that I'd like to host a very small site on a home server, I'm trying to take security seriously. I know I could easily use GoDaddy hosting, but this will pretty much be a static page blog that I'm sure no one will ever visit anyway. Also, it gives me the opportunity to learn.

In the past, I've always configured my virtual host to use a folder in my home directory. I've read that this is better practice, and it's always been easier than changing permissions for /var/www, but one problem with this is that the www-data user does not have permission to this folder.

I've been experimenting the last couple of days with giving ownership of /var/www to www-data and adding myself to the www-data group, but I've had a few hiccups (I'm sure I'm not doing everything correctly).

I've decided an easier route would be to keep the root web directory in my home folder, but change the user that runs Apache to myself. I've done some searching to see if this is recommended against, but really haven't been able to find too much about the issue in general.

Is this something that anyone else does on a public server? There won't be anything hosted on it that would concern me security wise, but it's always nice to know things are as secure as I can make them.

+2 votes

My Apache server host few applications something like :

/var/www/A
/var/www/B

I would like to trace access for a specific application, eg. A. Is it possible?

Or should I use 'LogLevel info' and so log all applications into access.log file (then parsing for specific web page) ?

My config for logging is :

..
ErrorLog /var/log/apache2/error.log
LogLevel info
CustomLog /var/log/apache2/access.log combined
..
+1 vote

We have a set up like Apahce (80,443) redirects the request to Tomcat (8080) using mod_jk.

The new requirement is to route the request from the same apache to another tomcat (8090). Hence I made the different config file for apache with different ports (86,4444) and different worker for mod_jk which routes the request to tomcat.

Now the issue is when I hit the url http://:86 and after providing the credentials, it is redirecting to https://:86 and throwing the below error.

Error in browser:

Secure Connection Failed

An error occurred during a connection to x.x.x.x:86. SSL received a record that exceeded the maximum permissible length. (Error code: ssl_error_rx_record_too_long)

Error in logs:

"x16x03x01" 501

[Thu Jan 08 08:22:46 2015] [debug] ssl_engine_io.c(1523): OpenSSL: I/O error, 11 bytes expected to read on BIO#1bf568 [mem: 1f3930]
[Thu Jan 08 08:22:46 2015] [debug] ssl_engine_kernel.c(1806): OpenSSL: Exit: error in SSLv2/v3 read client hello A
[Thu Jan 08 08:22:46 2015] [info] (70014)End of file found: SSL handshake interrupted by system [Hint: Stop button pressed in browser?!]
[Thu Jan 08 08:22:46 2015] [info] Connection to child 3 closed with abortive shutdown(server pritoolvca1.sw.ericsson.se:443 [2], client 153.88.164.216)
[Thu Jan 08 08:23:53 2015] [error] [client 172.17.136.153] Invalid method in request x16x03x01

Could you please suggest where it might went wrong and the way forward..

+1 vote
# initialize cookie and retrieve cookie from clients browser
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )

if cookie.get('ID') is not None:
 cookieID = cookie['ID'].value
else:
 cookieID = random.randrange(0, 9999)
 cookie['ID'] = cookieID
 cookie['ID']['path'] = '/'
 print( cookie )

I use this code to retrive or set a cookie to the visitor's browser if present and identify him by it.

All work well except the situation where the user visits my webpage by clicking a backlink on another webpage.

Then for some reason the cookieID changes to another value thus a new entry appears into the database when insert happens.

What can I do about that?

...