Centos, Varnish, NGINX, Drupal
- 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:
1
2
include
/
etc
/
nginx
/
sites
-
enabled
/
*
.
conf
;
server_names_hash_bucket
_
64
;
It will look something like:
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
_
application
/
octet
-
stream
;
log
_
main
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
;
access
_
/
var
/
log
/
nginx
/
access
.
log
main
;
sendfile
on
;
#tcp_nopush on;
keepalive
_
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
_
64
;
}
Add host yoursitename.com
Create two directories sites-available and 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:
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
_
$
uri
$
uri
/
/
index
.
php
?
q
=
$
uri
&
$
args
;
}
error
_
404
/
404.html
;
error
_
500
502
503
504
/
50x.html
;
location
=
/
50x.html
{
root
/
www
/
html
/
biyixia
.
com
;
}
location
~
\
.
php
$
{
root
/
www
/
html
/
yoursitename
.
com
;
fastcgi
_
127.0.0.1
:
9000
;
fastcgi
_
index
.
php
;
fastcgi
_
SCRIPT
_
$
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:
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
1
chown
-
R
nginx
:
nginx
/
www
/
html
/
yoursitename
.
com
Install Varnish
1
yum
install
varnish
Configure Varnish
Open /etc/sysconfig/varnish, replace the following values:
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:
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
1
yum
install
php
-
fpm
Restart servers:
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:
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
Comentários