📜 ⬆️ ⬇️

Use memory wisely. Part 2. fapws3

In the previous part, we started to fight for the memory on a 256 MB slice in a hurry. The result was, but not as spectacular as the one I achieved this time.

I always guessed that the reason for all my troubles - apache. And the more I tried to tune it, the more convinced I was. Conclusion? Try to replace. But one thing is that the transition should be as smooth as possible, since it is clearly a matter of production.

Since I had experience with nginx, and to be precise - the experience with proxying, this particular web server was chosen. In addition, he has good performance parameters.

The first thing I tried to do was build nginx with mod_wsgi (not to be confused with the same for apache). Collect something, but to make work - no. Constantly lying around with the error "can not be corrupted as a module ...". But it doesn’t matter, to get away from this move was also caused by some attenuation of the mod_wsgi project ... Although what I chose cannot be called actively developing and stable :)
')
After a lot of digging and listening to the guests of the conference pythonua@conference.jabber.ru, the gaze was focused on fapws3 . In short, this is a web server written in python. Theoretically, it can be used just like that, without nginx, but I haven’t yet found a way to create virtual hosts by domains, and nginx handles much better with static.

So, I have archlinux on a slice, respectively, everything below is true for him. pacman is a software manager, you can use your apt-get etc. by choice, unless the package names may vary.

fapws3


There is nothing complicated about installing fapws3. We swing tarball , we will unarchive, we execute python setup.py install . Do not forget to install libev

pacman -S libev

fapws is based on it.

In the archive you will find the examples daddy, and in it django. Take run.py and throw in your project. It is better to immediately check the performance by running it python run.py

There is likely to get an ekspepshn, they say DJANGO_SETTINGS_MODULE is not defined ... But this is solved simply by adding os.environ['DJANGO_SETTINGS_MODULE'] = 'kaexpert.settings' in run.py (of course, replace with your module).

Just need to change the ports. Those. for each site its own port. I started numbering from 8081 and so on. Further it will be required at the nginx settings. By the way, the port is configured in evwsgi.start("127.0.0.1", 8081) (for testing, you can use IP 0.0.0.0 so that you can test operation without nginx, in our case, except for nginx, no one will contact the site directly ... more correct)

nginx


There is nothing complicated here either: pacman -S nginx . Further settings. Since I needed uninterrupted operation of the server, I installed nginx on port 8080, for this we find in /etc/nginx/conf/nginx.conf the line to listen 80 and change 80 to 8080.

All that is required of nginx is to proxy dynamic requests to the server fapws, and distribute the statics yourself. For this we do for each site a server block inside http in /etc/nginx/conf/nginx.conf:
  server {
         listen 80;
         server_name promex.su;
	 location / static {
		 root /srv/http/promex.su/;
	 }
	 location ~ * ^. + \. (jpg | jpeg | gif | png | ico | css | zip | tgz | gz | rar | bz2 | doc | xls | exe | pdf | ppt | txt | tar | mid | midi | wav | bmp | rtf | js | mov) {
		 access_log off;
		 expires 30d;
	         root /srv/http/promex.su/;
	 }
	 location / media {
		 root /usr/lib/python2.6/site-packages/django/contrib/admin/;
	 }

         location / {
	     proxy_pass http://127.0.0.1:8082;
	     proxy_set_header X-Forwarded-Host $ server_name;
	     proxy_set_header X-Real-IP $ remote_addr;
	     proxy_set_header X-Forwarded-For $ remote_addr;
	 }

         #error_page 404 /404.html;

         # redirect server error pages to the static page /50x.html
         #
         error_page 500 502 503 504 /50x.html;
         location = /50x.html {
             root html;
         }
     }


The proxy_pass line is important here, you need to specify the port from run.py in it. In what places the static is tuned, I think clearly, we will not go into details, just correct the paths to our own.

supervisord


Since the fapws3 for each site is a separate daemon, you need to somehow manage all this beast. For this, I took a supervisord . This is something like rc.d, only written in python and where it is more appropriate ... In terms of a person, I’m too lazy to write rc for each site, and then add it to rc.conf. With the supervisor, we only need to add it to rc.d and configure the sites in its configuration (very simple). It is put a little more difficult, through aur, but maybe in your distribution it is like a supported package. Although with my yaourt it all comes down to the yaourt -S supervisord command :) I advise all arch users to use.

It is added to rc.conf as follows: DAEMONS=(syslog-ng network netfs crond sshd supervisord) .

The configuration is very simple, in appearance as win-ini. For me it was enough to add such a block:
  [program: kaexpert]
 command = / srv / http / ka-expert.ru / src / run.py
 process_name =% (program_name) s
 user = http 

command - the command itself, do not forget to just make run.py executable
process_name - process name
user - from which user will be launched (the same as the web server, and nginx is for me under http and not for standard nginx, since everything is sharpened for http, because Apache)

Well that's all. Of course, this is necessary for each site. Personally, I have also uncommented the [inet_http_server] section, which gives the web interface, but it works crookedly for me ... It only performs the informational function, does not work to restart the process, only through the supervisorctl, and that is with the exceptions (apparently, they were dealt with, but laziness).

Total


In total, we have released more than 100MB of memory, now the picture looks like
  total used free shared buffers cached
 Mem: 256,113,142 0 4 32
 - / + buffers / cache: 75 180
 Swap: 511 27,484

After the reboot, the swap should go away in mem, but until you get confused - let the uptime tick :)

In general, and difficult, we got a huge savings in memory and an increase in speed in general. Subjectively, the pages began to load 5 times faster, the tester did not measure, it suits me :)

Good luck!

Via Retta Inc.

PS> Plus in karma will not interfere - there are always good people who need an invite :)

Source: https://habr.com/ru/post/54464/


All Articles