top of page

Centos, Varnish, NGINX, Drupal

  • Writer: Qasim Zee
    Qasim Zee
  • Nov 28, 2015
  • 5 min read

As an experiment this week I decided to change the web server of a site to NGINX, the existing web server was Apache. I would like to share the steps required to setup NGINX with Varnish to make your Drupal site run at a blazing speed.

Environment:

Centos 6

16G RAM

Quadcore processor.

Install NGINX

yum install nginx

Configure NGINX

Open /etc/nginx/nginx.conf

Add the following lines at the bottom of “http” block:

include /etc/nginx/sites-enabled/*.conf; server_names_hash_bucket_size 64;

1

2

include

/

etc

/

nginx

/

sites

-

enabled

/

*

.

conf

;

server_names_hash_bucket

_

size

64

;

It will look something like:

http {     include       /etc/nginx/mime.types;     default_type  application/octet-stream;     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                       '$status $body_bytes_sent "$http_referer" '                       '"$http_user_agent" "$http_x_forwarded_for"';     access_log  /var/log/nginx/access.log  main;     sendfile        on;     #tcp_nopush     on;     #keepalive_timeout  0;     keepalive_timeout  65;     #gzip  on;     # Load config files from the /etc/nginx/conf.d directory     # The default server is in conf.d/default.conf     include /etc/nginx/conf.d/*.conf;     include /etc/nginx/sites-enabled/*.conf;     server_names_hash_bucket_size 64; }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

http

{

include

/

etc

/

nginx

/

mime

.

types

;

default

_

type 

application

/

octet

-

stream

;

log

_

format 

main

'$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"'

;

access

_

log 

/

var

/

log

/

nginx

/

access

.

log

main

;

sendfile

on

;

#tcp_nopush     on;

keepalive

_

timeout 

65

;

#gzip  on;

# Load config files from the /etc/nginx/conf.d directory

# The default server is in conf.d/default.conf

include

/

etc

/

nginx

/

conf

.

d

/

*

.

conf

;

include

/

etc

/

nginx

/

sites

-

enabled

/

*

.

conf

;

server_names_hash_bucket

_

size

64

;

}

Add host yoursitename.com

Create two directories sites-available and sites-enabled

mkdir /etc/nginx/sites-available mkdir /etc/nginx/sites-enabled

1

2

mkdir

/

etc

/

nginx

/

sites

-

available

mkdir

/

etc

/

nginx

/

sites

-

enabled

Create a file named “yoursitename.com.conf” under sites-available and paste the following contents:

server {         listen   81; # as varnish is running at port 80         root /www/html/yoursitename.com;  #Update this path according to your default directory         index index.php index.html index.htm;         server_name yoursitename.com www.yoursitename.com;         location / {                 try_files $uri $uri/ /index.php?q=$uri&$args;         }         error_page 404 /404.html;         error_page 500 502 503 504 /50x.html;         location = /50x.html {                 root /www/html/biyixia.com;         }         location ~ \.php$ {                 root           /www/html/yoursitename.com;                 fastcgi_pass   127.0.0.1:9000;                 fastcgi_index  index.php;                 fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;                 include        fastcgi_params;         } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

server

{

listen

81

;

# as varnish is running at port 80

root

/

www

/

html

/

yoursitename

.

com

;

#Update this path according to your default directory

index

index

.

php

index

.

html

index

.

htm

;

server_name

yoursitename

.

com

www

.

yoursitename

.

com

;

location

/

{

try

_

files

$

uri

$

uri

/

/

index

.

php

?

q

=

$

uri

&

$

args

;

}

error

_

page

404

/

404.html

;

error

_

page

500

502

503

504

/

50x.html

;

location

=

/

50x.html

{

root

/

www

/

html

/

biyixia

.

com

;

}

location

~

\

.

php

$

{

root

/

www

/

html

/

yoursitename

.

com

;

fastcgi

_

pass 

127.0.0.1

:

9000

;

fastcgi

_

index 

index

.

php

;

fastcgi

_

param 

SCRIPT

_

FILENAME 

$

document_root

$

fastcgi_script_name

;

include

fastcgi_params

;

}

}

Please note that the port number is “81” as varnish will run at port 80 and will behave like content server.

Create a symlink and remove the default configuration:

sudo ln -s /etc/nginx/sites-available/yoursitename.com /etc/nginx/sites-enabled/yoursitename.com rm /etc/nginx/conf.d/default.conf

1

2

sudo

ln

-

s

/

etc

/

nginx

/

sites

-

available

/

yoursitename

.

com

/

etc

/

nginx

/

sites

-

enabled

/

yoursitename

.

com

rm

/

etc

/

nginx

/

conf

.

d

/

default

.

conf

Please note that default.conf’s location can be different based on different platforms.

Change owner of the web directory

chown -R nginx:nginx /www/html/yoursitename.com

1

chown

-

R

nginx

:

nginx

/

www

/

html

/

yoursitename

.

com

Install Varnish

yum install varnish

1

yum

install

varnish

Configure Varnish

Open /etc/sysconfig/varnish, replace the following values:

VARNISH_STORAGE_SIZE = 3G VARNISH_LISTEN_PORT=80

1

2

VARNISH_STORAGE_SIZE

=

3G

VARNISH_LISTEN_PORT

=

80

Keep all other values as it is.

Open /etc/varnish/default.vcl and update “backend default” block to the following:

backend default {   .host = "127.0.0.1";   .port = "81”; #get content from nginx from port 81 }

1

2

3

4

backend

default

{

.

host

=

"127.0.0.1"

;

.

port

=

"

81

;

#get content from nginx from port 81

}

Install PHP FPM

I am also using php fpm for faster php processing. Here is the command for that, simple simple

yum install php-fpm

1

yum

install

php

-

fpm

Restart servers:

service varnish restart service nginx restart service php-fpm restart

1

2

3

service

varnish

restart

service

nginx

restart

service

php

-

fpm

restart

Configure Varnish with Drupal

Install Drupal’s varnish module

Add the following lines to your settings.php:

$conf['cache_backends'][] = 'sites/all/modules/third-part/varnish/varnish.cache.inc'; $conf['cache_class_cache_page'] = 'VarnishCache'; $conf['page_cache_invoke_hooks'] = FALSE;

1

2

3

$

conf

[

'cache_backends'

]

[

]

=

'sites/all/modules/third-part/varnish/varnish.cache.inc'

;

$

conf

[

'cache_class_cache_page'

]

=

'VarnishCache'

;

$

conf

[

'page_cache_invoke_hooks'

]

=

FALSE

;

Go to varnish configuration: admin /config /development /varnish

Set Varnish control terminal: 127.0.0.1:6082

Set Varnish Control key from/etc/varnish/secret

Please make sure the “Varnish running” is ticked.

Awesome, you are done. Enjoy the faster response times of your site.

Please comment if you have any feedback

Recent Posts

See All

Comentários


bottom of page