Nginx Ubuntu Varnish WordPress – Amazon EC2

这里以Ubuntu 12.04 LTS为例,和Nginx,Varnish来搭建WordPress运行环境。

EC2的SSH登录名称是ubuntu,需要转到root状态下。

sudo -i

安装MySQL
apt-get install mysql-server mysql-client
过程中按提示设置root SQL password

安装Nginx
apt-get install nginx
/etc/init.d/nginx start
访问你实例的公共DNS URL,就会见到Welcome to nginx!’

配置防火墙
ufw allow ssh
ufw allow http
ufw enable
ufw status

安装PHP,通过PHP-FPM安装PHP和Mysql扩展
apt-get install php5-fpm php5-mysql

配置Nginx
vi /etc/nginx/sites-available/default //(Esc键并输入q!可以不保存退出vi)

找到index index.html index.htm这行,后面加入index.php;像这样index index.html index.htm index.php;

然后注释掉这部分并改成以下

location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php; include fastcgi_params;
}

修改PHP的配置
vi /etc/php5/fpm/pool.d/www.conf
找到listen = 127.0.0.1:9000 这行,并将它改成listen = /var/run/php5-fpm.sock

重启php5-fpm 和 Nginx
/etc/init.d/php5-fpm restart
/etc/init.d/nginx restart

测试PHP
vi /usr/share/nginx/www/info.php
加入
访问你的实例公共NDS地址,例如ec2-XX-XXX-XXX-XX.eu-west-1.compute.amazonaws.com/info.php,可以看到PHP信息页面,证明成功安装PHP。

安装WordPress
创建主机
vi /etc/nginx/sites-available/www.youmane.com
加入
server {
listen 80;
server_name www.youmane.com youmane.com;
root /var/www/www.youmane.com/public_html;
if ($http_host != "www.youmane.com") {
rewrite ^ http://www.youmane.com$request_uri permanent;
}
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

链接网站,启用
cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/www.youmane.com www.youmane.com
rm default

重启Nginx
/etc/init.d/nginx reload

注意:添加新的主机,要重启nginx

设置web目录(主机配置文件中指定的web目录)
mkdir -p /var/www/www.youmane.com/public_html

设置MySQL(创建一个WordPress的用户名/密码)
创建数据库:
mysqladmin -u root -p create wordpress
添加用户

mysql -u root -p

GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost' IDENTIFIED BY 'wp_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost.localdomain' IDENTIFIED BY 'wp_password'; FLUSH PRIVILEGES; quit;

这里database=wordpress,user=wp_user,password=wp_password

安装WordPress
cd /tmp
wget http://wordpress.org/latest.tar.gz
tar xvfz latest.tar.gz
cd wordpress/
mv * /var/www/www.youmane.com/public_html/

允许nginx的目录能写,以便,WordPress可以修改它的配置文件
chown -R www-data:www-data /var/www/www.youmane.com/public_html/

mv /var/www/boromeke/wp-config-sample.php /var/www/boromeke/wp-config.php

vi /var/www/www.youmane.com/public_html/wp-config.php

/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wp_user');
/** MySQL database password */
define('DB_PASSWORD', 'wp_password');

访问http://www.youmane.com/wp-admin/install.php 完成安装。

安装Varnish
vi /etc/nginx/sites-available/www.youmane.com
将80端口改为 listen 127.0.0.1:8080;

开始安装
curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add -
echo "deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0" >> /etc/apt/sources.list
apt-get update
apt-get install varnish

设置侦听Varnish 80端口
vi /etc/default/varnish
将对应的行改成DAEMON_OPTS="-a :80
正确是如下:
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"

编辑Varnish的默认配置文件
vi /etc/varnish/default.vcl

加入
backend default {
.host = "127.0.0.1";
.port = "8080";
}
# Drop any cookies sent to WordPress.
sub vcl_recv {
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
}

# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
if (!(req.url ~ "wp-(login|admin)")) {
unset beresp.http.set-cookie;
}
}

重启nginx&restart
/etc/init.d/nginx restart
/etc/init.d/varnish restart

发表评论或回复

*选项为必填项,您的电子邮件地址不会被公开。

*
*