How to Install Nginx + Php FPM + APC on CentOS 6.4

By | November 14, 2023

LEMP server

A lemp server runs Nginx web server along with Php and Mysql or MariaDB on a Linux system. Nginx is increasing becoming popular because of its lightweight structure and ability to handle large amounts of traffic in an optimum manner.

Mariadb is the replacement for mysql because mysql is not very free anymore. In this tutorial we shall be setting up Nginx with Php FPM on CentOS. The instructions to install MariaDB shall be covered in another post.

CentOS is a very popular os for linux based web servers. CentOS (Community Enterprise Operating System) is based on RHEL (RedHat Enterprice Linux) and is 100% binary compatible with it. For us it simply means that its similar to rhel in its working and environment and that we have the handy yum command available to install software easily from the repositories. In this example we shall be working on CentOS 6.4 which, at the time of this post is a recent version.

Install Nginx

The first step is to install Nginx web server. Nginx is not available in the default CentOS repositories but nginx provides centos specific repositories for easy use.

Add the nginx repository

We create a repository file in /etc/yum.repos.d directory

$ nano /etc/yum.repos.d/nginx.repo

Now open the file and add the following lines. These instructions are provided by Nginx directly.

name=nginx repo

Save and close. Now nginx can be installed.

$ yum install nginx

The above will download and install the nginx web server and make it ready to use.
After the installation completes, its time to do some inspection. First use the service command to check the status of nginx.

[root@dhcppc2 ~]# service nginx status
nginx is stopped

The above shows that nginx service is there but is stopped. Next check the configuration using the t option with nginx command.

[root@dhcppc2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

The above command tells that the configuration is OK and all set to run. And most importantly, it also tells the location of the nginx configuration file. For creating virtual hosts/multiple domains, its important to create separate configuration files for each host. The virtual hosts configurations are located at /etc/nginx/conf.d/

OK, now lets start nginx server.

[root@dhcppc2 conf.d]# service nginx start
Starting nginx:                                            [  OK  ]

Now nginx is up and running. Find the ip address of the nginx server using ifconfig and connect to that ip from a browser to test it out.

You might have to open port 80 on the centos server, if it is not already open. Check this tutorial on how to open http port on centos. Once its open the ip address of the nginx server should load the page with content like this

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to
Commercial support is available at

Thank you for using nginx.

Creating virtual host

Virtual hosts or "Server Blocks" as nginx calls them, allow nginx to serve multiple hosts based on a name. For example it can host and together. All it needs is a configuration file for each host containing the host specific settings like root directory etc.

The /etc/nginx/conf.d/ directory contains a default.conf configuration file which has the settings for a virtual host that will work through http://localhost/

Check the file and it will show the location of web files which is called the root.

location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

So our web file (.html, .php and everything) should be put inside the /usr/share/nginx/html directory.

To create more virtual hosts, simply copy the default.conf file and edit the root path and server_name settings to match that of your domain/host.

$ cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/mysite.conf

Now edit mysite.conf and configure settings like root directory and server_name.
Then test your nginx configuration and restart server. That should create the new virtual host.

File Permissions

When setting up the root directory for the web files, it must be ensured that the files and all parent directories are readable by "group" and "others" on the system. For example a permissions of 0755. Nginx would not be able to read the files, if the parent directories are not readable.

For example if you choose to place you web files in the following directory


Then ensure that both home and joe directories are readable by group and others. Otherwise even if the web files are readable by others, but not the parent directory joe, then nginx would not be able to read the files and error log would contain messages like this

open() "/home/joe/public_html/index.html" failed (13: Permission denied)

Install PHP and FPM

Now that nginx server is up and running, next task is to install the php interpreter so that .php files can be processed by nginx.

Since php-fpm is already part of the CentOS repository, we don’t need to create another repo entry like the one we did for nginx package. So to install php-fpm, we can just use the yum command.

$ yum install php-fpm

You may want to use the "-C" option if you don't want yum to update repository data before installing the packages.

Now check the php-fpm service status

# service php-fpm status
php-fpm is stopped

Then start it

# service php-fpm start
Starting php-fpm:                                          [  OK  ]
[root@dhcppc2 ~]#

Now its time to create a php script and test it. The web files directory is /usr/share/nginx/html
So create a file in that directory.

# nano /usr/share/nginx/html/phpinfo.php

After that we add this content to it.


Now its time to open the file via our browser.

NO, it won't work yet, since we have not configured nginx to execute php scripts.

Configure Nginx to run php

Nginx already comes with a default configuration to use php fpm. Open the /etc/nginx/conf.d/default.conf file and uncomment the following lines.

# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
	root           /usr/share/nginx/html;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
	include        fastcgi_params;

The SCRIPT_FILENAME is very important and must point to the correct location, otherwise you would keep getting "File not found." error.

Save the file and close it. Now test your new nginx configuration.

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@dhcppc2 ~]#

Configuration is OK. Now restart nginx for new configuration to take effect.

# service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@dhcppc2 ~]#

Done. Now open the php script in your browser and it should work right away.

Install APC

APC is the alternative php cache that enables php to cache both user data and opcode. This makes php execution faster than usual. The package for APC is available in the base repository itself so we do not need to configure any additional repositories.

Install it using yum right away.

# yum install php-pecl-apc

Now restart php-fpm for apc to get enabled

# service php-fpm restart
Stopping php-fpm:                                          [  OK  ]
Starting php-fpm:                                          [  OK  ]
[root@analytics php.d]#

The configuration file for apc is located at the following path.


The same path is also mentioned in the output of phpinfo() function if you want to verify.
The Configuration section of phpinfo.php page should have a section for apc which shows the values of various apc directives. All those directives can be configured in the apc.ini file.


1. While configuring virtual hosts, the nginx configuration test might fail with an error like this

# nginx -t
nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
nginx: configuration file /etc/nginx/nginx.conf test failed

To fix this error, open the main nginx configuration file located at /etc/nginx/nginx.conf and add the following line in the http block.

server_names_hash_bucket_size 64;

That will increase the hash bucket size and fix the error.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].


How to Install Nginx + Php FPM + APC on CentOS 6.4
  1. Guest

    I installed PHP 5.3.29 (fpm-fcgi) (built: Aug 20 2014 16:42:49). It needed to change /etc/php-fpm.d/www.conf. Edit it and change lines
    user = apache
    group = apache


    user = nginx
    group = nginx

  2. Pascal Dutilleul

    If you have the error ‘No Input file specified’ from php, then move the setting ‘root’ one level higher. Don’t define it in Server – Location, but rather directly below Server

Leave a Reply

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