How to install a web server with Nginx + MySQL + PHP5 + APC + Spawn_FastCGI [1st Part: Presentation]

Not long ago we mentioned that now DesdeLinux (all its services) are working in GNUTransfer.com servers. The blog has improved a lot in terms of speed, fluidity, even when we go from having (after UsemosLinux merge) more than 30.000 visits every day (almost 200 users connected simultaneously). How to achieve good server performance even with this volume of traffic?

Currently Justice (VPS where the blog and some other service are) has 3GB of RAM, however less than 500MB is consumed, this is possible with the correct choice of software to use and an adequate configuration of them. For example, Apache is undoubtedly a great in the world, the No.1 when it comes to hosting, but precisely for that reason Apache is not always the best option. When the traffic is high and the server hardware is not really large (Ex: 8 or 16GB of RAM) Apache may well consume too much RAM making the server at certain times take too long to respond, or worse, that our site is offline for insufficient resources. This is why many of us choose Nginx over Apache.

NGINX:

We already told you about Nginx earlier in the article Nginx: An interesting alternative to Apache, there we told you that it is a web server like Apache, LightHttpd or Cherokee, but compared to Apache it stands out for its performance and low hardware consumption, precisely why many large sites such as Facebook, MyOpera.com, DropBox or even WordPress .com use Nginx instead of Apache. In the world of Linux DesdeLinux It is not the only one that uses Nginx, as far as I know, emsLinux and MuyLinux also use it :)

My personal experience with Nginx dates back several years, when out of necessity I started looking for lightweight alternatives to Apache. At that time Nginx was on version 0.6 and its compatibility with high demand sites made in PHP was not the most optimal, however nowadays from version 0.9 onwards (v1.2.1 available on Debian Stable, v1.4.2 available on ArchLinux) has improved a lot, to the point that with a proper configuration and union of Nginx + PHP everything will work like a charm.

In this tutorial series I will use Nginx version 1.2.1-2.2, available in the Debian Stable repos (Wheezy).

PHP5:

PHP, that programming language that many of the sites (and CMS) work with today, is in my view, the black sheep of the family. That is, in my personal experience, large sites, with a large volume of visits, with many options, functions, etc., if such a site is made in PHP it will consume more resources than a similar site made, for example, in RoR. My experience has been that folks, PHP is a huge resource dragon, PHP + Apache is enough to swallow hundreds and hundreds of MB of RAM without a real need.

The reason not to use RoR, Django or something else is simply that DesdeLinux (the blog, our flagship) works with WordPress, a CMS developed with PHP that offers us so many comforts, that we simply do not plan to change it in the short or medium term, honestly, WordPress, even when it is not perfect, serves us for what we need and maybe more.

Regarding PHP, in these tutorials I will use the PHP version 5.4.4-14 available on Debian Wheezy (Stable)

Spawn_FastCGI:

This can be said to be what unites Nginx with PHP, that is, even if they have the PHP5 package installed if they do not have Spawn_FastCGI installed and executed when they open a site in PHP the browser will download the file, it will not show them anything that the .php has been programmed because the server does not know how to process .php files, which is why it is essential to install and configure Spawn_FastCGI.

If we used Apache it would be something as simple as installing the libapache2-mod-php5 package but since we use Nginx we will have to install the spawn-fcgi package instead. Also, in the tutorial I will explain how to create an initial script for it in /etc/init.d/ so that you can control it more comfortably.

MySQL:

This could be the big question mark or perhaps, for some, the discordant note. Many I know will ask me the question: why use MySQL and not MariaDB?

The issue is simply that I don't have enough time to dedicate to doing a migration at this moment from MySQL to MariaDB, a migration that in theory should be transparent for everyone, 100% compatible with everything, but that is... as I said, in theory. At the time I started moving services DesdeLinux from one VPS to another I had to leave Apache behind and use Nginx, this involved different configuration files, different ways of declaring VHosts, installation and configuration from scratch of the server and its services, at that time I could not add another task to the list, also and to be honest, I changed Apache for Nginx because Apache did not satisfy my needs, however, MySQL so far meets my needs 100%, I see no reason for me to increase my workload by changing something that It was already working technically well for me.

Once explained why I did not install MariaDB, also explain that as the vast majority of websites need a database for their operation, because it is where a lot of information (or almost all) of it will be stored. There are some who like Postgre or someone else, in this series of tutorials I will explain how install MySQL and configure separate users for each site.

La MySQL version that I will use is v5.5.31

APCs:

APC is an optimizer for PHP (explained very simply). It allows us once properly configured that the PHP processing works better, that the responses from the server are faster.

There are alternatives like memcache however, I have always used APC and have had very favorable results. I recommend reading this article in English: Comparing APC and Memcache as local content cache

I will use in the tutorial the version of php-apc v3.1.13-1 also available in the Debian Stable repos.

Resumiendo:

This way of installing a web server configuration is not the most optimal, far from it, for example many will recommend Varnish, which from what I have read works true miracles because everything or almost everything is cached, but, in our case we do not need that 100% of the site is always cached as we do not want or need to go to that extreme. However, I clarify, as I said above: "as far as I have read", I personally have not used Varnish until today, so I cannot give you a 100% objective opinion.

This will be a series of tutorials in which I will show you how to install a web server like the one hosted by this minute. DesdeLinux (blog, forum, paste, etc). The blog has 30.000 visits every day, almost 200 users accessing it simultaneously, and yet the RAM does not exceed 500MB consumed, for some this may be excessive consumption but... hey, we have 3GB of RAM, less than 500MB (which include FTP service, SSH, etc.) is really good right? 🙂

All the 'magic' is not only done by Nginx + Spawn_FastCGI + APC, our blog cache system is really well configured and the rules for Nginx are precise, this makes the blog even when it receives a lot of traffic process much less PHP than what usual, as it has a lot already cached. If you have a site in high demand and you have resource problems, I recommend without a doubt that you study to see which cache system will work best for you, which one will best suit your needs.

I hope you find these tutorials interesting, in each of them I will try to explain everything in a comprehensive, detailed and as simple way as possible.

regards


27 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Bruno cascio said

    Very good and clear! I congratulate you!

    1.    KZKG ^ Gaara said

      Thank you

  2.   Christopher castro said

    Very good tutorial.

    What fills me with doubt is how they configured the email server.

    1.    KZKG ^ Gaara said

      The MailServer is something apart, that is, it has nothing to do with the web server as you know 🙂

      However, a long time ago I decided not to complicate myself with the MailServer, I chose to use iRedMail (support for MySQL, LDAP and Postgre) and with the proper settings and details that I add in the configuration files, everything works fine.

  3.   rots87 said

    I like the article, I am waiting for the series of articles

    1.    KZKG ^ Gaara said

      Thanks, I hope to bring the next one on Monday or Tuesday, it will deal with Nginx installation and configuration.

  4.   to that said

    Very good, the right configuration, it is difficult to find it, the compromise between the factors is sometimes almost unsolvable, I too went to nginx a while ago and later to mariadb (recently, I think a year ago).

    // As I mentioned, it would be good if you raised the possibility of chroot, and use the proxy_cache_path which is also useful. Also the comparison of the socket (in the cases that it is possible) against the port. and define well the number of children / ram.

    regards

    1.    KZKG ^ Gaara said

      Thank you for your comment 🙂
      Yes of course, it would be very nice to cage Nginx to keep it separate as such from the rest of the system, I had not considered that possibility in these tutorials, I'll see what I can do. About the proxy_cache_path, I've never used it, I'll read a bit about it to see how it goes.

      Regarding the number of threads (min & max), in the Nginx configuration it is obviously defined, in the Nginx post I will talk a lot about the .conf file 😉

      Again, thank you for your comment.

  5.   msx said

    This type of HowTos is what makes the web truly powerful for computer scientists as it saves us tons of hours of research and testing until we finally decide on the appropriate option, thanks a lot!

    One question, does this run on Debian? What version of OS and packages?

    Regards!

    1.    KZKG ^ Gaara said

      Thank you.
      Indeed, sites that report, that repeat and repeat the news there are already too many ... what is needed are sites that put tutorials, that is what the web needs!

      Yes, Debian Wheezy (current Stable), the versions of the packages are right there in the post 😉

  6.   eliotime3000 said

    Excellent comment. Let's see if I do a kind of Errata with ZPanel X, and incidentally, do the installation manually in Debian Wheezy.

  7.   Federico Antonio Valdes Toujague said

    Go ahead KZKG ^ Gaara !!!, that The Best Criterion of Truth is Practice, and you have experience about what you write. A professional and working website. Major League Baseball, Dude.

    1.    eliotime3000 said

      That's true. Also, when I started to play with the web servers that I installed in Windows, the truth is that Apache shoots up in terms of resource consumption if you use WordPress (in Drupal it consumed half the resources).

  8.   carriage said

    I think that for the Nginx part this tutorial is going to come in handy. I now want to install a server with Nginx, php, Varnish and MariaDB. But of course, you have to start, and laziness can do a lot when it comes to fighting with servers and at the moment I am happy with the typical lamp and memcache that I have xDD.

    A greeting.

  9.   auroszx said

    Great, just one of these would come in handy 🙂 Another looking forward to it.

  10.   Ivan Gabriel Sosa said

    We follow you. We are currently starting out in the world of web servers. We bought two from Hostinger, and a friend helped us configure it from scratch (PHP, MySQL, Apache). It is the only combination that is used in Linux, a platform that I have been on since January.
    But I was very interested in this topic. Cheers!

  11.   Jose Manuel said

    I have never installed a web server but if I want to do it, a question, will the level necessary to understand the tutorials and do the installation be high or with basic knowledge could I try it? Thanks in advance.

    1.    eliotime3000 said

      The truth is that it does not require much knowledge to be able to operate a database server. One who has already tried that experience tells you.

  12.   Mauricio said

    Hello, it is very good what you are going to do with this series of posts.

    I recently installed Nginx + Php Fastcgi + Mariadb. Nginx.

    All this, I did in Archlinux, because that distribution is the only one from my point of view, that does not bring as much goodies as the others. I put it in a caged environment and it gave me a lot of inconvenience to have it working perfectly.

    Now it is working perfectly. Although I am interested in knowing your opinions, about that of the children and father processes, the more tips they give me, the better.

    All this is just for practice.
    The equipment has 4GB of DDR2 ram and a 2Ghz Core 2.4duo processor.

    Greetings and I look forward to the upcoming posts of this series.

  13.   who said

    200 users connected simultaneously?
    Only at certain times of the day, right? Because otherwise it would exceed those 30.000 daily visits.

    1.    KZKG ^ Gaara said

      Yes of course, there are not always 200 people online, at this time there are almost 40 because it is still early, in a few hours they will exceed 100.

  14.   dhunter said

    Just for fun I just switched from lighty to nginx on my workstation (Symfony2 right now), I took the conf from here [1], very simple.

    [1] http://ihaveabackup.net/2012/11/17/nginx-configuration-for-symfony2

  15.   Apr4xas said

    Waiting for the continuation of this 😀

    1.    KZKG ^ Gaara said

      This week I must publish it, thanks for reading nos

      1.    health said

        and? a lot is missing?

  16.   Dean said

    Good post…

  17.   NOEL IVAN said

    GOOD EVENING.
    DUE TO SPECIFICATIONS OF A SCHOOL PROJECT, THEY LET ME INSTALL NGINX IN OPENBSD 5.4 IN ORACLE MV VIRTUALBOX TO BE ABLE TO USE PHP, MYSQL, AMONG OTHERS, NADAMAS THAT I HAVE NOT BEEN ABLE TO FIND INFORMATION REGARDING THE ABOVE SAID, BELIEVING THAT IT IS PROBLEM TO ME ABOVE.