installing nginx, php-fpm, apc + memcache on Gentoo

The following How To will show you through setting up possibly the best way to serve PHP with NginX (Engine X), PHP-FPM (FCGI Modul since PHP 5.3.0 compiled in with needed Option. ) APC Support and the Caching Manager Memcache on Gentoo.


Actually we use a clean Install with Distri Gentoo 2008.
nginx/0.8.53
APC ( Opcode Cache )
PHP 5.3.5 with enabled PHP-FPM
Memcache ( memcached-1.4.5 )
Optional MySQL ( for MySQL i will write a new Tutorial. )

First we will get Gentoo up to date.

Important: Make sure your Portage is updated before performing any profile changes.

# emerge eselect

after doing this we can select our preferred profile

Viewing supported profiles

# eselect profile list

Available profile symlink targets:
[1] default/linux/amd64/10.0
[2] default/linux/amd64/10.0/desktop
[3] default/linux/amd64/10.0/desktop/gnome
[4] default/linux/amd64/10.0/desktop/kde
[5] default/linux/amd64/10.0/developer
[6] default/linux/amd64/10.0/no-multilib
[7] default/linux/amd64/10.0/server
[8] hardened/linux/amd64 *
[9] hardened/linux/amd64/no-multilib
[10] selinux/2007.0/amd64
[11] selinux/2007.0/amd64/hardened
[12] selinux/v2refpolicy/amd64
[13] selinux/v2refpolicy/amd64/desktop
[14] selinux/v2refpolicy/amd64/developer
[15] selinux/v2refpolicy/amd64/hardened
[16] selinux/v2refpolicy/amd64/server

i have choosen number 8 for my System doing by

# eselect profile set 8

Note : If you’d still prefer to change profiles manually, then simply do the following:
Changing profiles manually

# rm /etc/make.profile
# ln -s ../usr/portage/profiles/ /etc/make.profile

now let us begin for the update and enter some commands in our bash console :

# emerge --update --deep world

when everything was completed successfully, we can configures and compile the Nginx, php, and all other necessary applications.
first edit the file /etc/package.use

# echo "dev-lang/php ~amd64" >> /etc/package.keywords
/* you can change ~amd64 to reflect your system */
# echo "dev-lang/php" >> /etc/package.unmask
# echo "dev-lang/php fpm" >> /etc/portage/package.use
# echo "www-servers/nginx fastcgi" >>  /etc/portage/package.use

Hint : if you need more modules by nginx you can manually edit the file
/etc/portage/package.use
here i show you mine

# cat /etc/portage/package.use
www-servers/nginx fastcgi gzip gzip-static pcre ssl status webdav zlib
dev-db/mysql  -berkdb
dev-lang/php -* unicode json crypt calendar curl curlwrappers bcmath exif jpeg pear zip xmlrpc tiff mysqli fpm cli apache2 ctype expat fastbuild ftp gd hash iconv memlimit mysql nls pcre pic pdo reflection session simplexml sockets spl ssl tokenizer truetype xml xsl zlib

Consider while emphasizing the whole dependencies.
edit the file
/etc/make.conf for using Use-Flags before compile.
insert following data to /etc/make.conf :

USE="unicode vim-syntax libwww
ACCEPT_KEYWORDS=~amd64
NGINX_MODULES_HTTP="gzip memcached sub map refererredis secure_link flv addition realip rewrite ssi charset perl auth_basic fastcgi stub_status access uwsgi autoindex"
HTTPD_USER="nginx"
HTTPD_GROUP="nginx"

now emerge everything :-P

# emerge -v pecl-apc
# emerge -v memcached
# emerge -av nginx php

here some USE Variables :

USE="http http-cache pcre ssl vim-syntax -aio -debug -ipv6 -libatomic" NGINX_MODULES_HTTP="access addition auth_basic autoindex charset dav fastcgi flv geo geoip gzip gzip_static limit_req limit_zone map memcached perl realip referer rewrite secure_link ssi stub_status sub upstream_ip_hash uwsgi -browser -cache_purge -degradation -empty_gif -headers_more -image_filter -passenger -proxy -push -random_index -scgi -split_clients -userid -xslt" NGINX_MODULES_MAIL="-imap -pop3 -smtp"
RUBY_TARGETS="ruby18"

Note: If you change some Variables or add new Modules to PHP or NginX, you must reemerge the Package.

# emerge nginx or -'PackageName'

we can edit our fresh installed PHP-FPM and NginX
Location PHP-FPM Configfile :
/etc/php/fpm-php5.3/php-fpm.conf
and the ini file for php
/etc/php/fpm-php5.3/php.ini
PHP Configuration should not be a problem since it defaults already brings with it all functions.
for NginX open
/etc/nginx/nginx.conf
and edit to the following

1

        server {
                listen 80;
                server_name localhost;

                access_log /var/log/nginx/localhost.access_log main;
                error_log /var/log/nginx/localhost.error_log info;

                root /var/www/localhost/htdocs;

                location ~ .php$ {
                   set $memcached_key "XXX:$request_uri"; #XXX is the fileowner from the file and who is using memcache on this server , set it to whatever you want.
  memcached_pass 127.0.0.1:11211;
  default_type       text/html;
  error_page 404 405 502 = @no_cache;
                }
  location @no_cache {
                 fastcgi_pass 127.0.0.1:9000;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME /var/www/locaslhost/htdocs$fastcgi_script_name;
                 include fastcgi.conf;
    }
        }

starting :

# /etc/init.d/nginx start
# /etc/init.d/php-fpm start

Start memcached:
Run “memcached -h” to see a list a options avaible to the memcached. Most likely when you are just starting to test out the deamon, you will do this:

#  memcached -d -u nobody -m 512 127.0.0.1 -p 11211

This will start memcached, using 512MB on the localhost, and listening on the default port 11211.
Autostart memcached on Gentoo
First, configure the memcached by editing the file /etc/conf.d/memcached. Then do the following:

# rc-update add memcached default

its time for a beer cheers :-)

here some nice Info .
[1] Nginx: [www.nginx.org]
[2] Nginx-Module: [wiki.nginx.org/NginxModules]
[3] Nginx-Rewrite-Modul: [wiki.nginx.org/NginxHttpRewriteModule]
[4] PHP-FPM: [php-fpm.org/]
[7] Homer Simpson : [Homer_Simpson]

16 Responses to installing nginx, php-fpm, apc + memcache on Gentoo
  1. GUNER
    March 9, 2011 | 11:37 pm

    bu neeee

  2. Crisu
    March 11, 2011 | 8:43 pm

    great how to , thumbs up :-)

  3. marlis
    March 16, 2011 | 4:12 pm

    Hi Dennis.
    Sag mal, werden die anderen Dienste automatisch eingetragen ? also als default ?
    habe nämlich das Problem, dass der Dienst enginx nicht mehr nach Neustart da ist. lieben Gruss, die Marlis aus Mainz.

    • rip
      April 2, 2011 | 11:45 am

      Eigendlich macht des Gentoo. Kannst aber auch selber machen.

      Einfach “rc-update add nginx default”
      dürfte reichen.
      Natürlich auch für andere Dienste.
      Und es heisst nginx (ausgesprochen wie englisches ‘engine-ex’ oder ‘engine x’… gruss dennis

  4. Anna Meier
    April 9, 2011 | 1:07 pm

    netter blog, solltest aber mal ein wenig mehr Inhalt zeigen :-)

  5. telefonsüchtig
    April 13, 2011 | 5:49 pm

    I can see you might be an expert at your field! I am launching a website soon, and your details will be quite intriguing for me.. Thanks for all your help and wishing you all of the success.

  6. prywatny
    April 16, 2011 | 1:39 pm

    I think (or rather sure) that your point of view is similar to mine, but I’ve need you to clear one thing up, so i @ You. Thank you in adcance! Oh, I bbokmarked your installing nginx, php-fpm, apc + memcache on Gentoo | blog.hot-root.com in Digg also :)

    • rip
      April 16, 2011 | 9:20 pm

      thank you for digging :-P more updates on blog and tutorials soon.

  7. Singapore Web Hosting Services
    April 17, 2011 | 2:28 pm

    It really is rather great precisely how often the most basic thought spins into a fabulous entire short article, is it not? You create articles efficiently, and I really only wish I have a small bit of your ability to write by myself! – Diane

  8. Cherryl Sye
    April 17, 2011 | 10:47 pm

    I were sent here since this post was tweeted by a chap I was following and feel pleased I made it here.

  9. James
    February 25, 2012 | 8:36 am

    That config with memcached makes no sense.

    What exactly is memcache going to cache. You have nothing in the Nginx config telling memcached to cache anything.

    APC is the only cache that will cache opcode automatically.

    So tell what am I missing? What is the purpose of memcached here? What is it caching?

    • rip
      March 5, 2012 | 12:15 pm

      hi, i have updated the tut. i have only forgoten to add the lines with memcache . im sorry :-)

  10. some guy
    March 28, 2012 | 12:37 pm

    I’m truly enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a developer to create your theme? Superb work! one thing i would do. write your tutorials in both language. maybe this is good for more publicity. anyway i wish you a nice day. Golli

  11. blabla
    March 29, 2012 | 6:21 am

    I have bookmarked your blog.hot-root.com !

  12. heiner
    April 1, 2012 | 12:15 pm

    great i found your page. pls make a tutorial with nginx and debian if you can. thank you. btw. i love to read your pitgin english style xD. heiner

  13. rip
    May 8, 2012 | 1:25 am

    maybe when i have time….

Leave a Reply

Connect with Facebook

Wanting to leave an <em>phasis on your comment?

Trackback URL http://blog.hot-root.com/installing-nginx-php-fpm-apc-memcache-on-gentoo/trackback/