How to install a web server with Nginx + MySQL + PHP5 + APC + Spawn_FastCGI [4th Part: Nginx + PHP with SpawnFastCGI]

Some time ago I told you about this series of tutorials, on how to install and configure a server for high demand hosting. This article will be about installing and configuring Nginx + PHP with SpawnFastCGI:

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.

1. Installation:

We will start with the first thing, install Spawn-FastCGI and PHP from our repositories.

All the commands to be executed are executed with root permissions, either by putting sudo at the beginning of each line or by being logged in as root

If on your server you use a distribution such as Debian, Ubuntu or some derivative in the terminal you must put the following and press Enter :

aptitude install spawn-fcgi php5-cgi php5-curl

aptitude is not installed by default in Ubuntu, however I recommend that you install it and use it instead of apt-get, because aptitude does better management of dependencies on certain occasions

Personally, I do not recommend any derivative of Debian, not even Ubuntu for servers, over the years my experiences have not been entirely satisfactory. My first choice for server operating system is Debian, then I would think of CentOS, finally some BSD

2. Configuration:

In the previous step (when we installed Nginx) we downloaded a file called nginx-spawn-fastcgi.tar.gz that when unzipped it created the folder nginx-spawn-fastcgi in our home, we will copy the file from it spawn-fastcgi to /etc/init.d/:

cp ~/nginx-spawn-fastcgi/spawn-fastcgi /etc/init.d/

Also, we need the php-fastcgi executable in / usr / bin /

cp ~/nginx-spawn-fastcgi/php-fastcgi /usr/bin/

Perfect, we have ready the file that will allow us to control spawn-fastcgi and also the php-fastcgi executable, now we are going to start spawn-fastcgi:

/etc/init.d/spawn-fastcgi start

It will show us something like: spawn-fcgi: child spawned successfully: PID: 3739

Now we are going to replace our /etc/nginx/sites-available/mywebsite.net file with ~ / nginx-spawn-fastcgi / mywebsite_plus_php.net

cp ~/nginx-spawn-fastcgi/mywebsite_plus_php.net /etc/nginx/sites-available/mywebsite.net

Why? Simple, because our previous mywebsite.net file does not have support for PHP, that is, it is Nginx only, while the file mywebsite_plus_php.net does have support for PHP, that is, Nginx + PHP using SpawnFastCGI.

The differences between these files are several, for example:

  • In line 3 the file that supports PHP is added index.php
  • A new line under No.3 that contains: fastcgi_index index.php;
  • Several other new lines that tell Nginx how to process PHP.
  • … ..In short, here is a photo that will help you to know the differences between the two files:

nginx_mysql_spawn-fastcgi_comparing_mywebsite_confs

The file mywebsite_plus_php.net is a vhost that serves as an example, that is, and in other words, we must modify it and establish our configurations.

We must change the following:

  • access_log (line 3): This will be the path of the access log file to this site
  • error_log (line 4): This will be the path of the error log file to this site
  • server_name (line 5): The URL, domain that is hosted in that folder, for example, if it were the forum DesdeLinux it would be: forum server_name.desdelinux.net
  • root (line 6): The path to the folder where the html files are, let's leave this in / var / www / as it will only be a test
Obviously they must have pointing in their DNS records of their hosting provider (using CPanel or another tool) that the domain or subdomain declared in server_name is located on the IP of this server that they are configuring. That is, in the DNS where they create subdomains for their domain, they must declare that the domain or subdomain that they have put in line 5 is located on this server (this server = the IP address of the server in question)

Ready, now we will restart Nginx:

/etc/init.d/nginx restart

To check that our Nginx processes PHP properly, let's copy the file phptest.php to the hosted folder, that is, the one indicated in line No.6 of the mywebsite_plus_php.net file (for example, root / var / www /), assuming that have the site hosted directly in / var / www / would be:

cp ~/nginx-spawn-fastcgi/phptest.php /var/www/

Supposing that in line 5 of our mywebsite_plus_php.net (that is, the server_name line) we have said that our site is www.mysite.net then we must access www.mysite.net/phptest.php. In other words, the idea is to access the phptest.php file from our browser and if the following appears then our Nginx linked perfectly with PHP:

nginx_mysql_spawn-fastcgi_tersting_nginx_php

In case this does not appear, that is, that the browser tries to download the .php file ... this means that they did something wrong, that they did not replace /etc/nginx/sites-available/mywebsite.net with ~ / nginx-spawn-fastcgi / mywebsite_plus_php.net… that they forgot to restart Nginx with /etc/init.d/nginx restart or that they forgot to start Spawn-FastCGI with /etc/init.d/spawn-fastcgi start

So far the tutorial to link Nginx with PHP using SpawnFastCGI, we only need MySQL and APC 🙂

I hope you are finding this interesting.


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.   f3niX said

    If it is a quarter, why does the image say 3? started at 0 I guess? excellent post.

    regards

    1.    KZKG ^ Gaara said

      1st: Presentation
      2nd: Nginx
      3rd: Nginx + PHP (Spawn_FastCGI)

      : )

      Thanks for reading ^ _ ^

      1.    Rodrigo said

        what happened to part 4 ???
        and with the subsequent ?????

  2.   rpyanm said

    Hello:

    It would be a better choice if instead of mysql, you used MariaDB, as you should know the latter is a fork of the first, and there is already talk that it will be the Mysql of the future (http://www.genbetadev.com/bases-de-datos/mariadb-sera-el-mysql-del-futuro) as Mysql is free, up to a point.

    SkySQL, a company that provides services based on free databases, financially supported the MariaDB project (http://www.genbetadev.com/bases-de-datos/mariadb-y-skysql-unen-fuerzas-para-llevar-a-mariadb-a-lo-mas-alto) and Google that wants to stop depending on Mysql, as Wikipedia did, and will make the transition from MySQL 5.1 to MariaDB 10.0 with the help, precisely, of SkySQL, who are already experts in this field.

    Salu2.

    1.    KZKG ^ Gaara said

      Hello,

      Yes of course, I know MariaDB and in fact, we have already talked about it: https://blog.desdelinux.net/tag/mariadb/

      However, right now I am using MySQL because I am doing these tutorials from the particular experience I had when migrating. DesdeLinux (with all its services) to other servers, at that time we changed the technology completely and the tasks or changes that I had to face were not few.
      Read this comment of mine from that moment: https://blog.desdelinux.net/el-blog-desdelinux-abandona-hostgator-y-pasa-a-gnutransfer/comment-page-1/#comment-81291

      The final idea is yes indeed, migrate to MariaDB, but I have not had time to do the relevant tests 🙂

      Thanks for reading

  3.   eliotime3000 said

    This tutorial will help me to install zPanel X with NGINX so as not to saturate my site when migrating to the GNUPanel VPS.

  4.   dragnell said

    The Christmas gift? I looked forward to congratulations to all.

    1.    KZKG ^ Gaara said

      Thanks partner 😀

  5.   st0rmt4il said

    Added to favorites!

    By the way, I have this doubt and question, does Nginx really have more performance than Apache?

    Regards!

    1.    KZKG ^ Gaara said

      Well, Apache can be optimized a lot but… until now, Google it and almost all of us agree that Nginx consumes much less RAM, has a not inconsiderable performance, although it is not entirely that simple when configuring.

  6.   Luis Morales said

    Good KZKG ^ Gaara excellent information for those of us who are interested in this world, a question, for when the 4th post 😀