Install nginx and php in docker ubuntu container

Docker can be used to install nginx and php5 (php-mpm) in a ubuntu container. We’ll cover docker running on Mac and Ubuntu Linux for the purpose of this tutorial. The steps are mostly same for both environments.

  1. Download ubuntu image and run it. We’ll use -p 8081:80 when running docker container to make its port 80 available on host port 8081.

    $ docker pull ubuntu
    $ docker run -it -p 8081:80 --name nginxphp1 ubuntu /bin/bash
  2. Install nginx and php5-fpm (in docker)

    $ apt-get update
    $ apt-get upgrade
    $ apt-get install nginx
    $ apt-get install php5-fpm
    $ service nginx start
    $ service php5-fpm start 
  3. Extra step for Mac. Find docker host IP for mac as it is not localhost for Mac.

    $ docker-machine ip default

    Note that for Mac, instead of localhost, we’ll use this ip.

  4. Check if ngnix is working by visiting localhost:8081 (or docker_host_ip:8081 for Mac) in browser and you should be able to see nginx default page.
  5. Edit default site settings in /etc/nginx/sites-available/default and uncomment php settings. It should look like this.
    # pass the PHP scripts to FastCGI server listening on
            location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                    # With php5-cgi alone:
                    # fastcgi_pass;
                    # With php5-fpm:
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;

    Note that we have kept fastcgi_pass commented out.

    Now restart nginx

    $ service nginx restart
  6. Now edit /etc/php5/fpm/php.ini and
    add cgi.fix_pathinfo = 0 in relevant section. Then restart php5-fpm

    $ service php5-fpm restart
     * Restarting PHP5 FastCGI Process Manager php5-fpm
  7. Start service nginx and php5-fpm

    service nginx start
    service php5-fpm start
  8. Create file info.php in directory /usr/share/nginx/html/ with the following content:

    Now visit localhost:8081/info.php (or docker_host_ip:8081/info.php for Mac) and you should be able to see php info page.
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments