WordPress Nginx

Home WordPress Nginx Tutorials Installing PHP+APC, MySQL, Postfix, Nginx and WordPress on Ubuntu

Installing PHP+APC, MySQL, Postfix, Nginx and WordPress on Ubuntu

A complete WordPress-Nginx setup will involve PHP, MySQL, Nginx stack and on the top of it WordPress itself. This is the bare minimum requirement for WordPress. Postfix is highly recommended but technically you can use other packages for SMTP or even use remote SMTP servers like gmail.com to send mails using WordPress.

If you already have PHP, MySQL & Nginx up & running then you may jump to next part i.e Nginx configuration for different WordPress

Assumption:

I am assuming few things before you start.

  1. You have a Ubuntu server up and running. This guide may work on other Debian based distros but this is tested on Ubuntu 12.04 LTS only.
  2. You have shell access to your Ubuntu server with sudo privilege or you have direct root-user access.

Installing PHP 5.4

We are going to use PHP 5.4 as it is much faster & better than PHP 5.3.

As official repo for Ubuntu 12.04 LTS contains PHP version 5.3 only, we will use a launchpad repo maintained by Ondřej Surý

Run following codes to add launchpad repo to apt-sources:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php5

Press “enter” key whenever asked!

Update apt-cache…

This is must otherwise next command may end up installing PHP 5.3!

sudo apt-get update

Now, run following to install PHP itself

sudo apt-get install php5-common php5-mysql php5-xmlrpc php5-cgi php5-curl php5-gd php5-cli php5-fpm php-apc php-pear php5-dev php5-imap php5-mcrypt

Alternatively, you can compile PHP yourself. [TODO: I will post separate article for that]

Check PHP Version

Run following command…

php -v

You will see following output…

PHP 5.4.6-2~precise+1 (cli) (built: Aug 30 2012 14:37:22) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

Installing MySQL

With MySQL, we will also install an extra package mysqltuner here. It is one of the best way to optimize your mysql configuration.

sudo apt-get install mysql-server mysqltuner

Above wizard should ask you to enter a mysql password for user root. If it doesn’t, then run the following command to change MySQL password.

sudo mysqladmin -u root password NEWPASSWORD

Please note that above command will not help you in case you forget your mysql root password in future. [TODO: Post help]

Installing Postfix

If you want to send mails from your WordPress, you will need a SMTP server. You can use a remote SMTP server like gmail.com for this. But I prefer using postfix package to run a SMTP server locally.

In simple words, if you skip this step and do not configure remote SMTP server with WordPress either, your WordPress will not be send email notifications for comments, etc.

sudo apt-get install postfix

It will take you through few steps. The important one are:

  1. Select option – Internet Site
  2. Hostname – generally your main domain i.e example.com. Recommended – a FQDN like mail.example.com 

You no need to touch other options. Just restart mail service to be safe.

/etc/init.d/postfix restart

Testing your mail config:

Run following PHP code with admin@example.com replaced by your mail id:

php -a
mail ('admin@example.com', "Hello from nginx!", "My email setup works!");
exit ();

Alternatively you can use check-mail WordPress Plugin after your WordPress setup completes to be sure that your WordPress can send emails.

Installing Nginx

Ubuntu official repos come with a Nginx package but I prefer using launchpad repo maintained by Nginx team.

sudo add-apt-repository ppa:nginx/stable
sudo apt-get update 
sudo apt-get install nginx

If you prefer to use Nginx package in Ubuntu repo, you can simply run following command:

sudo apt-get install nginx-full

What’s Next?

At this point, we are done with installing prerequisite for WordPress.

In the next article, we will jump to basic WordPress installation & configuration.

After that, I will share some commonly used optimization tips and configuration tweaks and then we will touch complex WordPress configuration. You can also find the complete list of WordPress-Nginx tutorials here.

Keep reading! :-)

22 Comments…

 Share your views
  1. HI,

    Got into bit of trouble here..
    I have tried to install APC for my current server ( aleady nginx, running on Centos)
    Now it seems APC is conflicting with Zend Optimizer
    If i enable APC extension on php.in then nginx will get me a 502 Error.

    Any idea to solve it.

  2. I don’t like this line:

    sudo mysqladmin -u root password NEWPASSWORD

    because you’ll have the password in you bash history.

    For me the better way is to use the

    mysql_secure_installation

    script.

    • Avatar of Rahul Bansal

      Thanks for your feedback. :-)

      I am aware of mysql_secure_installation and bash history concern but if I can read your bash history then I will be able to read other files as well. ;-)

      Goal of this chapter is to get all needed software up and running quickly. This is part of a large on-going wordpress-nginx series tutorials. I will be covering security in-depth towards end where I will revisit this and few more things.

  3. I think you’ll should explain the configuration of php-fpm too. The default options are quite good, but not for best performance. I’m using this one for my pool nginx

    [nginx]    
    listen = /var/lock/php5-fpm.sock    
    user = nginx    
    group = www-data    
    
    request_slowlog_timeout = 5s
    slowlog = /var/log/php5-fpm/www-slowlog.log
    
    listen.owner = nginx
    listen.group = www-data
    listen.mode = 0666
    listen.backlog = -1
    
    pm = dynamic
    pm.max_children = 9
    pm.start_servers =3
    pm.min_spare_servers = 2
    pm.max_spare_servers = 4
    pm.max_requests = 200
    pm.status_path = /status
    
    request_terminate_timeout = 120s
    
    rlimit_files = 131072
    rlimit_core = unlimited
    
    catch_workers_output = yes
    
    env[HOSTNAME] = $HOSTNAME
    env[TMP] = /tmp
    env[TMPDIR] = /tmp
    env[TEMP] = /tmp
    
    • Avatar of Rahul Bansal

      What you are using is quite customised. In real-life people don’t need to make these many changes to their config. As explained earlier this article is part of a large series. If I cover everything here itself, then this article will become too big to be useful!

      I covered php slowlog in details here. It’s not good to keep slowlog always on as it itself slows-down PHP scripts. I will recommend turning it off when you are done with debugging.

      request_terminate_timeout is commented by default and it takes value of max_execution_time from php.ini file. Only when your php scripts fails to terminate after max_execution_time elapses (most likely because of some bug) you need to set value for request_terminate_timeout explicitly.

      I explained this in more details here.

      You can find all articles in this series here. More will be added soon. :-)

  4. Hi,

    Please add that to get out of interactive mode , need to hit – d

  5. Avatar of ovidiu

    if I give APC say 1GB for caching, does it share that space between all vhosts even though they are running with different users?

  6. Avatar of Rahul Bansal

    I am not sure but I think APC will “allocate” 1 GB for every PHP’s linux-user. Still, OS may not complain as long as that much memory is not “used” by all processes together.

    You can simply do following:

    1. Start with 256MB memory for APC. Copy apc.php file under different vhosts.
    2. Check what results apc.php shows from browser.
    3. If all vhosts’ apc.php shows same amount of memory consumption then memory may be shared among all.
    4. Also, if you see low free memory in apc.php output in any-case, you can increase memory allocated to APC.
  7. Avatar of ovidiu

    Doesn’t really work. I tried that before. The reason is that each vhost runs the webserver under its own user so apc.php within one vhost sees only stuff it has access to.

    i.e. apc.php within site a) show i.e. 60MB out of 256M in use while apc.php within site b) shows 58MB out of 256MB in use.

    I think that is because they run as different users so they only see their usage?
    The reason for my question was: i.e. if I know each of my ten wordpress sites needs 60MB, do I assign 60MB to apc.ini and since each vhost runs its own settings each uses 60MB or should I assign 600MB to apc.ini?

    • Avatar of Rahul Bansal

      Sorry for delayed reply.

      You can try 60MB and if you see apc.php is using 100% of memory, you can start increasing it gradually.

      As every vhost is having separate memory segment, small segments can be used.

  8. php -a puts me in interactive mode rather than interactive shell, what do I need to do to get interactive shell. I am following your tutorial step by step.

    Thanks

  9. My wordpress can not send email, i has install postfix. Why it happen? how to fix this problem?

  10. Great series.. one detail I couldn’t find though off the bat:
    Do I use Ubuntu 12.04 x32 server or 12.04 x64 server?

    Many thanks.

  11. Thanks for the great blog series. It has helped me out tremendously.

    Unfortunately, I’ve hit a snag here with my nginx configuration. I am trying to deploy my blog at sudofoo.net, but I am getting a 403 error.

    Here is my /etc/nginx/sites-enables/sudofoo config file:

    server {
        listen 80;
        server_name sudofoo.net http://www.sudofoo.net;
    
        access_log   /var/log/nginx/sudofoo.access.log;
        error_log    /var/log/nginx/sudofoo.error.log;
    
        root /usr/share/nginx/www/sudofoo;
        index index.php;
    
        location / {
                try_files $uri $uri/ /index.php?$args; 
        }
        location ~ \.php$ {
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    }
    

    (I’m not sure why the text is the same color as the background, but highlighting the code should reveal it)

    I gave ownership of the following directories and sub-directories to the www-data user: /var/log/nginx/ and /usr/share/nginx/www/sudofoo/.

    Also, if I change the root and index to /usr/share/nginx/www and index.html respectively, I do get the default nginx welcome page.

    Does anyone have any tips on how to troubleshoot this issue?

  12. Awesome Tutorial Rahul,
    I also changed ‘cgi.fix_pathinfo=0′ in ‘/etc/php5/fpm/php.ini’ because i had it written down in my own notes as security issue.
    “If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. ”

    Is this necessary Rahul, what do you think?

Leave a Comment

  • Facebook
  • GitHub
  • Twitter
  • LinkedIn
  • Google

Your email address will not be published.

*

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

{ 3 Trackbacks }

  1. 一篇关于Nginx+Wordpress-multisite+静态文件处理的重要文章 | WP奇才 (Pingback)
  2. WordPress Multisite with Nginx - How to set it all up and enjoy the blazing fast consequences - WPMU.org (Pingback)
  3. WordPress Multisite with Nginx – How to set it all up and enjoy the blazing fast consequences | Pilvikoulutus 2013 (Pingback)