Linode VPS的申请和设置及基于Nginx的WordPress详细配置
老纪博客的开篇之作是什么?想了想还是就写怎么申请Linode VPS,怎么配置简单的防火墙和怎么设置Nginx服务器及PHP Fast-CGI吧,因为没有这些,就没有老纪博客的存在,呵呵,开始啦。
本教程包括3个部分:
- Linode VPS 申请及设置
- Nginx服务器的编译及配置和PHP-FastCGI设置
- 配置wordpress,你自己的博客
- Linode VPS 申请及设置
- Nginx服务器的编译及配置和PHP-FastCGI设置
- 配置wordpress,你自己的博客
Linode VPS 是现在几种国外vps中口评比较好的一个,价格适中,初级版Linode 360每个月19.9美金,性能比较稳定,他的服务器采用的是4核Xen CPU,每月200G流量对于一个流量适中的网站来说应该绰绰有余。因为money的问题,现在我选择的就是Linode 360的,他的升级也很方便,如果有一天你的网站逐渐牛B的话可直接升级到更高版,不需你做什么,只需一个Support Ticket和点击一个大按钮再加上迁移时等待的时间就全自动OK了(Linode上FAQ中有说明)。选择一个离中国比较近的机房,我选择的是Fremont的机房,访问速度不说嗖嗖的,也不至于让你等半天郁闷的去吧嗒烟。反正省去了不少在国内遇到的麻烦(备案呀,被拔和谐呀),速度测试可以参考Linode论坛中提供的测试地址
Linode VPS的申请比较简单,但是需要你一张能够支付美元的信用卡,比方建行龙卡就可以。在你需要的方案(plans)中选择一个你需要的(量力而行呦,再怎么说VPS比虚拟主机的钱还是贵的)点击Sign Up Now进入到申请界面,输入正确的信息,特别是信用卡部分,Payment Term部分可以选择按月支付,Referral Code部分可以输入:90a525cb94c5ee1b3b54f1f0e2d03ef5946019ff, Promotion Code(优惠码)很难找到,找到的话可以省点钱。注册完他有个Linode Distribution Wizard,很简单,选择你比较熟悉的linux发行版(空间小的可以选择Debian或者Ubuntu,另外应该选32位版的,随让咱没钱呢),此时磁盘映像大小和swap大小系统已经自动设好,一般按缺省配置即可,再给你的这台VPS加个root密码,这可是你的进门钥匙呦,否则入不了洞房的,一定要记住!!!这时你就有了一个属于自己的、占有给定资源的主机了,你就是她的主人,想干什么就干什么,她不听话就休了她,咱们再”Deploy a Linux Distribution“一个,呵呵
现在用linux的iptables来设计个防火墙,这样你的她就不容易被人拐跑了。在/etc/network/if-up.d/下建一个firewall文件,并设置为可执行属性
1 2 3 | cd /etc/network/if-up.d touch firewall chmod +x firewall |
用nano或者你所熟悉的任何一款编辑器打开firewall文件,加入以下代码,其作用就是只打开80,433,22端口,并且对连接进行了一些限制,如果想阻止DDos,那么需要跟进一步的配置,慢慢老纪会逐步的拨开这些面纱,为读者提供最直接的指导文章
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #--------------------------------------------------------------- # Initialize all the chains by removing all the rules # tied to them #--------------------------------------------------------------- iptables --flush iptables -t nat --flush iptables -t mangle --flush #--------------------------------------------------------------- # The loopback interface should accept all traffic # Necessary for X-Windows and other socket based services #--------------------------------------------------------------- iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT #--------------------------------------------------------------- # Allow outbound DNS queries from the FW and the replies too # # - Interface eth0 is the internet interface # # Zone transfers use TCP and not UDP. Most home networks # websites using a single DNS server won't require TCP statements # #--------------------------------------------------------------- iptables -A OUTPUT -p udp -o eth0 --dport 53 --sport 1024:65535 -j ACCEPT iptables -A INPUT -p udp -i eth0 --sport 53 --dport 1024:65535 -j ACCEPT #--------------------------------------------------------------- # Allow previously established connections # - Interface eth0 is the internet interface #--------------------------------------------------------------- iptables -A OUTPUT -o eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT #--------------------------------------------------------------- # Allow port 80 (www) and 22 (SSH) connections to the firewall #--------------------------------------------------------------- iptables -A INPUT -p tcp -i eth0 --dport 22 --sport 1024:65535 -m state --state NEW -j ACCEPT iptables -A INPUT -p tcp -i eth0 --dport 80 --sport 1024:65535 -m state --state NEW -j ACCEPT #--------------------------------------------------------------- # Allow port 80 (www) and 443 (https) connections from the firewall #--------------------------------------------------------------- iptables -A OUTPUT -j ACCEPT -m state --state NEW,ESTABLISHED,RELATED -o eth0 -p tcp -m multiport --dport 80,443 -m multiport --sport 1024:65535 #--------------------------------------------------------------- # Allow previously established connections # - Interface eth0 is the internet interface #--------------------------------------------------------------- iptables -A INPUT -j ACCEPT -m state --state ESTABLISHED,RELATED -i eth0 -p tcp #--------------------------------------------------------------- # If a packet doesn't match one of the built in chains, then # The policy should be to drop it #--------------------------------------------------------------- iptables -A INPUT -j DROP iptables -A OUTPUT -j DROP iptables -A FORWARD -j DROP |
至此,简单的防火墙已经设置成功,要让其立即生效,可以执行./firewall
Nginx网站服务器是俄国人Igor Sysoev以BSD授权方式开源的高性能HTTP服务器,稳定性能极佳,因为采用异步io方式对http请求进行处理,其负载能力非常强,占用内存也非常少,现在越来越多的大网站像都豆瓣、新浪、网易(特别还有老纪博客,呵呵)等,都转用它了。加上PHP的FastCGI模式,足以用有限的资源做出一个负载能力很强的网站,现就Nginx+PHP FastCGI编译配置整理如下
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 26 27 28 29 | # 下载nginx稳定版 wget http://sysoev.ru/nginx/nginx-0.7.61.tar.gz tar -zxvf nginx-0.7.61.tar.gz cd nginx # 配置、编译、安装 # 先安装需要库 aptitude install libssl-dev libpcre3-dev zlib1g-dev ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --with-md5-asm --with-md5=/usr/include --with-sha1-asm --with-sha1=/usr/include --with-http_stub_status_module make && make install #把编译完的nginx减减肥 strip /sbin/nginx #还要在临时目录里建立nginx目录,否则运行nginx是会报错 mkdir /var/tmp/nginx && chmod nginx:nginx /var/tmp/nginx # 建立 nginx用户 其home目录专为网站服务 useradd nginx mkdir /home/nginx chown nginx:nginx /home/nginx # 至此nginx的设置完成 # 安装php mysql, 要记住你的mysql root密码呦! aptitude install php5 php5-cgi mysql-server mysql-client # 安装spawn-fcgi,这是lighttpd脱离出来专门调度fast-cgi的东西 wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.gz tar -zxvf spawn-fcgi-1.6.2.tar.gz ./configure && make && make install stript /usr/local/bin/spawn-fcgi # 创建nginx fast-cgi服务脚本 touch /etc/init.d/nginx touch /etc/init.d/fast-cgi chmod +x /etc/init.d/nginx /etc/init.d/fast-cgi |
用nano或者你熟悉的文本编辑器编辑nginx和fast-cgi文件
nginx配置
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #! /bin/sh ### BEGIN INIT INFO # Provides: nginx # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the nginx web server # Description: starts nginx using start-stop-daemon ### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/sbin/nginx NAME=nginx DESC=nginx test -x $DAEMON || exit 0 set -e case "$1" in start) echo -n "Starting $DESC: " start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." ;; stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON || true echo "$NAME." ;; restart|force-reload) echo -n "Restarting $DESC: " start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON || true sleep 1 start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." ;; reload) echo -n "Reloading $DESC configuration: " start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON || true echo "$NAME." ;; *) N=/etc/init.d/$NAME echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 exit 1 ;; esac exit 0 |
fast-cgi配置
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #! /bin/sh ### BEGIN INIT INFO # Provides: fast-cgi # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the fast-cgi web server # Description: starts fastcgi using start-stop-daemon ### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/bin/spawn-fcgi NAME=spawn-fcgi DESC=spawn-fcgi DAEMON_OPTS="-u nginx -g nginx -C 8 -s /var/run/$NAME.sock -P /var/run/$NAME.pid -- /usr/bin/php-cgi" test -x $DAEMON || exit 0 set -e case "$1" in start) echo -n "Starting $DESC: " start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." ;; stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON || true echo "$NAME." ;; restart|force-reload) echo -n "Restarting $DESC: " start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON || true sleep 1 start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." ;; reload) echo -n "Reloading $DESC configuration: " start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON || true echo "$NAME." ;; *) N=/etc/init.d/$NAME echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 exit 1 ;; esac exit 0 |
现在要做的就是把他们注册成服务
1 2 3 | aptitude install rcconf #运行rcconf,选中这两项即可 rcconf |
废话少说,直接写代码
1 2 3 4 5 6 7 8 9 10 11 12 | # 创建mysql数据库 wordpress 和用户wordpress mysql -u root -p yourrootpasswd mysql>create database wordpress; mysql>grant all privileges on wordpress.* to wordpress@localhost identified by "yourwdpasswd"; mysql>exit cd /home/nginx wget http://cn.wordpress.org/wordpress-2.8.2-zh_CN.tar.gz tar -zxvf wordpress-2.8.2-zh_CN.tar.gz chown nginx:nginx wordpress cd wordpress mv wp-config-sample.php wp-config.php #把里面的define('DB_NAME', 'putyourdbnamehere');define('DB_USER', 'usernamehere');define('DB_PASSWORD', 'yourpasswordhere');改成你自己的数据库、用户名、密码 |
最后修改/etc/nginx/nginx.conf文件
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 26 27 28 29 30 31 32 33 34 35 36 | worker_processes 4; events { worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; server { listen 80; server_name www.yourdomain.com; location / { root /home/nginx/wordpress; index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/html; } location ~ \.php$ { root /home/nginx/wordpress; fastcgi_pass unix:/var/run/spawn-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } |
基本就OK了,也把我累坏了,最后执行一下/etc/init.d/nginx start && /etc/init.d/fast-cgi,用浏览器访问一下你那牛哄哄的博客吧。
作者:老纪
原载:老纪博客版权所有。转载时必须以链接形式注明转载自老纪博客 [http://www.jifuyi.com/]。
本文链接地址:http://www.jifuyi.com/linode-vps-register-nginx-wordpress-setting/
老纪,一个三十好几的老程序员,不知不觉蹉跎了不少岁月,心中曾有的萌动没有一丝的记忆,所以现在能记的就记,能写的就写。软件的开发、技巧,互联网的时讯、要闻,新科技的发展、评论,个人的感悟、随想就统统的让它跃然于老纪博客上。
inputx
2009年08月19日
这个博客是建立在Linode VPS之上的吗
老纪
2009年08月20日
是的,建立在Linoe上,360的,感觉不错,速度可操控上都很好,最主要的自己能掌控一切
zixun
2009年09月24日
http://hi.baidu.com/linode_com/ 这里说的Linode VPS和你说的是一个服务商么?
我没用过,一直在找这方面的资料,读了你的很多文章,真想试试啊。哈哈
老纪
2009年09月25日
是,建议使用,老纪的这个vps已经连续运行了2个多月了,一直很稳定,cpu负载,网络速度自我感觉还比较好,比一般的虚拟主机强多了。今天刚查了下cpu负载,不过1%,如果想先试试的话可以申请一个最基本的360, 将来网站流量大后可以自动升级,这个linode做的很不错。如果申请的话可以用老纪的推荐码http://www.linode.com/?r=90a525cb94c5ee1b3b54f1f0e2d03ef5946019ff,这样老纪在三个月后会有点优惠,这都是自愿的,呵呵
kent
2009年10月11日
想请教如何实现1个域名绑定一个目录?因为我想在这个VPS上放几个不同域名,不同内容的站,可以实现吗?
老纪
2009年10月11日
nginx有虚拟主机功能,且实现十分简单,只需绑定域名即可,也就是加一个server上下文
即可,把www.yourdomain.com换成你的域名, /your/dir换成你的目录
betaer
2010年03月19日
nginx中加载一个目录对应一个文件夹 在哪里添加啊,不太清楚喔,刚买linode,刚用text的ssh安装完lnmp和vsftp,反正是搞能登录ftp了。。就是不知道咋绑定多域名了
老纪
2010年03月19日
你的问题没太搞明白,不过应该是在
location *** {
root /your/dir;
index index.html index.htm index.php;
}
你把***换成你要的就行
kent
2009年10月11日
再请教 如 make && make install
&& 2个符号要不要输入??
不好意思,我很菜..希望你能回答我的问题。感谢!
老纪
2009年10月11日
&& 表示的是两个命令顺序执行,也就是先执行make再执行make install,这是bash里的语法
rollom
2009年11月10日
你的这篇文章很有用,Linode vps很有名。我没有使用过VPS,感觉价格比虚拟机贵了点。以后配置就看你的这篇文章了。考虑买个外国的虚拟机先用着,等流量大了,再搬到VPS。
能和你交换链接吗,我的地址rollom.com?谢谢。
老纪
2009年11月11日
总体来说感觉还不错,正酝酿在上面做个大点的应用,既是试验也是实践,链接已做上!
断峰狼
2009年11月29日
最初是朋友帮我弄的nginx,看的郁闷,后来换了Apache,就更郁闷了~
性能狂差,老是CPU超负荷,现在打算再换回nginx了~
老纪
2009年12月01日
要想驾驭一件事,必须先了解他的品性, 技术也一样
AntiGameZ
2010年01月03日
刚在Linode安家,可惜在Dallas,没有Fermont快,不过速度还凑合啦。非常感谢你的这篇文章。
抓个bug,编译nginx之前,安装的包libpre3-dev应该为libpcre3-dev,应该是笔误。
老纪
2010年01月04日
呵呵,对,手和眼睛有时配合不好,谢了
Zeddicus
2010年02月10日
请问装完wordpress以后,在插件和主题的删除与升级的过程中,是不是会提示要输入FTP信息?怎样才可以让他不提示呢?谢谢!
老纪
2010年02月11日
没有,我的插件,wordpress都是自动升级,没什么问题
javaliker
2010年03月31日
aptitude install libssl-dev libpcre3-dev zlib1g-dev
这句我执行时 提示找不到 是怎么回事 ? 另外 我执行make 提示需要安装GCC ? 难到 系统不带GCC吗 谢谢了!
老纪
2010年03月31日
干净的debian base 系统没有编译环境,执行下面的aptitude install build-essential命令安装编译环境,如果不是root用户,价格sudo
javaliker
2010年04月01日
为何安装phpMyAdmin后出现cannot load MySQL extension错误 谢谢!如何设置呢 ?
javaliker
2010年04月01日
就是提示 Cannot load mysql extension. Please check your PHP configuration. 谢谢!
老纪
2010年04月01日
到/etc/php5/conf.d/ 目录下, 看有没有mysql.ini文件,没有的话用你熟知的编辑器(nano)建一个,内容extension=mysql.so
javaliker
2010年04月02日
修改了还不行 ,重新安装了 php-mysql 就好了 多谢了
dturbo
2010年05月10日
cd /etc/network/if-up.d 显示 没有 这个文件夹 怎么办