These forums are read-only!
How easy to convert from Apache to Nginx?
  • Let me first say that I'm a relative noob at server set up, BUT I'm a smart noob and a quick learner. I am currently running a test Wordpress blog on my 256 slice running Hardy 8.04 LTS with the latest Apache and PHP. Like others I have read about I am experiencing quite a bit of swap usage and have tried as many tweaks as possible to minimize swap but am still getting bogged down because of it. I've read about nginx and it seems like it's probably all I need. My only long term plans are to add an SMF Forum to my site, and possibly a second site. A couple of quesitons:

    1. Will Nginx minimize my memory footprint based on my site requirements stated above?
    2. How easy is it to dump apache and install nginx on my Hardy server?
    3. Is there a 'magic' setting to minimize swap usage with a basic Wordpress blog/php forum?
    4. I can rebuild my slice, but since I've got Hardy like I want it, is changing from Apache to Nginx a good idea, or should I just rebuild slice and start from scratch?

    Thanks in advance,

    John
  • you don't need to remove Apache or anything. Just install nginx on the same slice and don't run apache. It doesn't matter if Apache is still installed on the system if it's not running.
  • Either stop the httpd service (and also disable on boot) OR move apache to another port (ie 81,etc...) and start nginx on 80.
  • Here is what I would do:

    Enable backups. Backup your current slice. Create a new slice from the backup. Do all of you tweaking on this slice so you don't mess anything up. As others have said, you don't need to uninstall Apache, just turn it off. I would follow the guides, install Nginx, PHP, MySQL and set up your confs to get your WP site working. The only issue you should be concerned with is that WP and some WP plugins are set to work with Apache and .htaccess specifically. You need to be aware that for certain things, like permalinks, WP would create rewrite rules on .htaccess and will no longer. You will need to manually handle this stuff in your Nginx conf files. It is easy pretty easy to set up rewrites in Nginx - just look at the docs.
  • so lorax are you saying that I would then have 2 slices? I'd like to not incur the add'l cost of a second slice, not that it's expensive, but would prefer not to.

    So there is no easy way to dump apache, eh? I guess I'm just trying to keep a tidy slice and not have any thing on there that I'm not using. But if there is a compelling reason to have both, I'd be interested in knowing why.
  • You would have two slices during the time you were setting up the new one. Once you were confident that the new one was working properly, you would edit your DNS to point to the new IP. Once everything switched over and your site was being served from your new slice, you could kill the old one and even kill backups if you really wanted to. The ability to do things like this with incredible ease is what makes Slicehost so good.
  • What is being left out here is dealing with PHP, which is actually a huge factor in all of this.

    Nginx doesn't have any kind of module for dealing with PHP natively the way Apache does via mod_php. This gives you essentially two choices: either run PHP via FastCGI or have Nginx proxy PHP requests to Apache (and let Nginx handle the rest). Each has its benefits and drawbacks which you can easily research in detail, however I've found it generally a better choice to proxy PHP scripts to Apache.

    "But wait!" you cry. "Isn't the point here to get away from Apache?"

    Well, the point is to run a more efficient server. When configured to solely serve PHP pages (by disabling most of Apache's modules), Apache becomes quite efficient. And the major benefit of Apache over FastCGI for PHP is that it can scale needed script processes on demand, whereas FastCGI must have a number of processes set beforehand.

    Also, it's really, really easy to set Nginx to proxy scripts to Apache. You start Apache on a separate port (8080 is a common one), add some lines to your Nginx config, maybe setup an Apache virtual host, and you're good to go.

    Serving PHP with FastCGI, in my experience, is quite the opposite. Among other things, you need to write a FastCGI-PHP init script. Or find one. And, really, the overall process is still very poorly documented.

    Either way, you'll need to look into either of these options if you're going to run Wordpress and Nginx.

  • truthiness,

    Have you checked out litespeed? It is a drop in replacement for apache at $250 for an owner license, that is near the cost of a vps for a year but could solve your solution. Have you added xcache/eaccelerator to your mix? Are you using wp-supercache?

    If you really want to do this you can have nginx serve all static content and forward dynamic content to apache as well.
  • you DO NOT want to proxy requests from nginx to apache. what would be the point of going to all this trouble. I personally wouldn't be too worried about 'scaling' of fastcgi.
    Have 4 spawned child processes (one for each cpu core) and call it good. If you feel the need to increase at some point you can always change it. Its one like in a single config file and it restarts almost instantly. You could do it on a live site in about 10 seconds or less.
  • I moved from apache to nginx because of memory issues. Currently running gallery2 and wordpress (no BB) and I have found that it works a treat for me (not that I have a site that generates much traffic - but I am sure it could handle it). Setting up was relatively easy (although I had to search quite a lot) - again not sure I have the optimal solutions - but it works and I have put in a number of tweaks that I have found! If you want some of the conf files etc that I use - shout!
  • proxying php requests to apache is actually very smart. The point being to avoid having apache load every apache module in order to handle each request. Disabling everything except for Mod_php leaves you with a process that takes just about as much memory as fastcgi would, but which requires less manual configuration and can scale automatically without needing to be edited.

    Honestly for normal traffic numbers it won't really matter. Even if you get dugg, fastcgi and apache will both be able to handle it, though fastcgi may need to spawn more child processes than apache for huge peaks, taking up more memory. Whereas apache's mod_php can scale up better.

    Along with mod_php, there's another high-scaling option for proxying - php fpm. The fpm module would need to be compiled into your php. It's basically a native module for php, kind of like mod_php is a native module for apache. It's got a lot of promise as the best of both worlds. It's rather new right now not as mature as apache mod_php.
  • thanks for providing some ideas. I like the idea of splitting processes between the two.

    So let me ask this... I am going to rebuild my slice as an excercise for myself. When i do this are you saying that I should set up Apache on port 8080, then my nginx on 80?

    Once I do that, I guess I am a bit unclear on how to split the functions between them. Any good writeups on such a setup?
  • am I just changing my virtual host in my apache2.conf file from *:80 to *:8080 to change where apache runs? If yes, do I then just run nginx and set up on 80?

    If so, I won't rebuild slice and just make these changes. I uninstalled apache, but then reinstalled and my .conf file is still the same and all seems to be working the same as before.
  • You'll want to edit the following files to have Apache listen in on port 8080:

    in /etc/apache2/ports.conf:

    NameVirtualHost *:8080
    Listen 8080
    

    And then in your virtual host files (/etc/apache2/sites-available/)

    <VirtualHost *:8080>
    

    Then add something like this to your Nginx config's 'server' area:

    location ~ \.php$ {
        proxy_pass http://127.0.0.1:8080;
        proxy_redirect off;
    
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
        client_max_body_size 10m;
        client_body_buffer_size 128k;
    
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
    
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
    }
    
  • What you'll do is change apache to listen on port 8080. Deactivate all unnecessary apache modules that have nothing to do with serving php. Then install nginx and have it listen on port 80. You'll need to customize the nginx config file (or the nginx virtual host file) to add the instruction to proxy to apache if the file's a php file. Here's an example of that instruction from one of my nginx virtual hosts:

    location ~ .*\.php$ {
    proxy_pass http://127.0.0.1:8080;

    proxy_redirect off;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    client_max_body_size 10m;
    client_body_buffer_size 128k;

    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;

    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    }
  • I would just like to suggest that you confirm that Apache is the problem.
    Reduce the number of apache clients and reduce the open session time.
    Also make sure that MySql is optimized for small memory usage.
    I am running Plone and had big swap problems until I started optimizing the space usage for apache and Zope.

    My 2 cents (of which China owns .4 cents)
  • Posted By: afewtipsI would just like to suggest that you confirm that Apache is the problem.
    Reduce the number of apache clients and reduce the open session time.
    Also make sure that MySql is optimized for small memory usage.
    I am running Plone and had big swap problems until I started optimizing the space usage for apache and Zope.

    My 2 cents (of which China owns .4 cents)

    what process on the mysql side should I be taking a look at? Again, this is just going to be a wordpress blog and a future forum set up. About 12k-15k pageviews a day right now on the blog.
  • I'm no MySql expert, but these may help.

    The first link I read and used, the second I just came across.
    Hope they help.
    (This link is having issues. If you search in Google for "Optimizing MySQL and Apache for Low Memory" it will be first and you can read the cached version until
    the site is fixed.)
    Apache part:
    http://emergent.urbanpug.com/?p=60
    My Sql part:
    http://emergent.urbanpug.com/?p=61

    http://day32.com/MySQL/
  • Truthiness, you may want to read this thread.

  • thanks Roshambo, but I did already tweak per that thread... not much change.

    thanks afewtips, i'll check those links out.
  • I have tried the following way in my slice. The blog(WP) is really slow once is has been served by nginx as the front and apache as the end. I have no idea about this. MAY it need more confirm. Finally, I have to run the blog under nginx via fcgi:)

    Posted By: roshambo

    You'll want to edit the following files to have Apache listen in on port 8080:

    in /etc/apache2/ports.conf:

    NameVirtualHost *:8080 Listen 8080

    And then in your virtual host files (/etc/apache2/sites-available/)

    <VirtualHost *:8080>

    Then add something like this to your Nginx config's 'server' area:

    location ~ \.php$ {     proxy_pass http://127.0.0.1:8080;     proxy_redirect off;      proxy_set_header Host $http_host;     proxy_set_header X-Real-IP $remote_addr;     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;      client_max_body_size 10m;     client_body_buffer_size 128k;      proxy_connect_timeout 90;     proxy_send_timeout 90;     proxy_read_timeout 90;      proxy_buffer_size 4k;     proxy_buffers 4 32k;     proxy_busy_buffers_size 64k;     proxy_temp_file_write_size 64k; }