最近入了一台独立服务器,如果直接拿来跑项目的话就太浪费资源了。于是打算使用Proxmox VE这款虚拟化管理软件进行VPS管理。

Proxmox VE是一款套开源的虚拟化管理软件,用户可通过网页的方式来管理服务器上使用 kvm 以及 lxc 技术运行的虚拟机。同时提供了一些先进功能的支持,如集群、HA等。

0x00 安装

Proxmox VE是基于Debian进行开发的,主要有两种安装方式。
其一是通过官方提供的iso作为一个全新的系统安装
另一种方式是在已有的Debian系统上安装
手动安装时请务必保证网卡配置正确,若出错的话在不带IPMI的机子上很难处理。

安装完成后即可通过https://ip:8006/访问管理页面

另外,这里记录一下版本升级的方法。由于Proxmox VE是一家商业公司在运营,所以一些功能是需要购买订阅才能使用的,例如说版本更新功能。但是可以通过一些方法绕过限制。注意这些更新方法请勿用于生产环境中。

将软件源更改为测试源

修改/etc/apt/sources.list.d/pve-install-repo.list, 将 pve-no-subscription 修改为pvetest
然后apt三连即可更新为新版本。

apt-get update
apt-get upgrade
apt-get dist-upgrade

0x01 相关设定

对于kvm虚拟化的虚拟机,若想上传需要用到的iso文件,可以直接通过网页端上传,也可以直接将文件放入/var/lib/vz/template/iso/
如果想对kvm虚拟机的启动参数进行调整,官方提供了api:qm set,具体可参照官方文档
对于lxc虚拟化的虚拟机,可以直接从系统中下载对应发行版的模板,无需自行下载。
可以直接使用LXC自带的api对lxc虚拟机进行管理,注意-n为虚拟机的id。

0x02 网络配置

对于多ip的服务器,本身官方就是按照桥接的方式做好网络配置的,直接在虚拟机中填写分配的ip即可。
对于单ip服务器,可以采用NAT的方法让虚拟机连上外部网络。这里介绍俩种方式。

采用QEMU自带的NAT

对于KVM虚拟机,可以直接在创建虚拟机的时候勾上NAT,这时候就会自动为虚拟机分配一个虚拟的子网并且虚拟机可以通过nat连接到外部网络,基本上是开箱即用。同时也支持端口映射,具体可参考官方wiki下的QEMU port redirection。但之前在使用的过程中,发现这个端口映射并不是很稳定。同时虽然这种方法很简单,但是虚拟机之间是隔离的,无法互通数据,这样就非常不灵活。
同时,LXC虚拟机是没有这种开箱即用的NAT的。

iptablesnat">配置iptables创建子网以实现nat

主要思路是创建一个虚拟桥接设备并创建一个子网,然后将所有虚拟机包括宿主机都连接到这个子网内,再开启iptables的NAT功能。
编辑配置文件/etc/interfaces,以下是参考配置

auto vmbr2
iface vmbr2 inet static
    address 10.0.0.254
    netmask 255.255.255.0
    bridge_ports none
    bridge_stp off
    bridge_fd 0
    post-up echo 1 > /proc/sys/net/ipv4/ip_forward
    post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j MASQUERADE
    post-down iptables -t nat -D POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j MASQUERADE

以上配置创建了vmbr2并且分配了一个子网10.0.0.0/24,同时宿主机(同时亦为网关)在这个子网内的ip为10.0.0.254。然后开启了内核的转发功能与iptables的NAT功能(其中vmbr0为通向外部网络的设备)。
若想添加端口转发直接在iptables中增加相关条目即可。
例如想要将宿主机vmbr0的80端口的tcp连接转发到10.0.0.102的80端口上:
iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80
如果想保存转发规则,使之重启后依然有效,则需要在/etc/interfaces相应位置加入

post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80
post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80

通过以上方法就能组建一个灵活的子网了,kvm虚拟机和lxc虚拟机都可接入,并且都可以有端口转发。由于没有DHCP服务器所以要自行分配ip。注意创建虚拟机的时候将其挂载到vmbr2端口下。
我的服务器只有一个ip,所以内部组网就只能采取这种这种的方法了hhhh。为了充分利用资源,我将80,443端口转发到内部一台虚拟机上,这台虚拟机再使用nginx反代到内网的其它虚拟机,以充分利用单个ip。

启用BBR优化网络

目前的Proxmox VE版本的linux内核版本比较新,已经包含了bbr模块了。

修改sysctl.conf

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf

保存生效

sysctl -p

检测是否已启用bbr模块

lsmod | grep bbr

如果含有bbr即说明内核内已启用bbr模块

转:http://www.senra.me/add-tun-tap-support-for-proxmox-ve-4-lxc-containers/

一.为什么

Proxmox VE 4将3代的OpenVZ支持去除换成了LXC,算是跟随趋势吧,但是LXC和OpenVZ还是有挺多不同的,在权限上更是如此,而网上最多的还是OVZ相关的教程,LXC的是真的少,不过一直以来没需求,所以没怎么折腾,最近在搞内网,所以为了用Tunnel还是得把TUN/TAP支持打开。

二.怎么办

首先当然是谷歌咯,关键词选得好马上出结果,proxmox官方论坛讨论贴 https://forum.proxmox.com/threads/tun-devices-in-ve-4-lxc.23473

总结一下

①.如果需要对所有(之后创建的)LXC容器全部开启tun/tap

那么需要修改/etc/lxc/default.conf,添加lxc.cgroup.devices.allow = c 10:200 rwm

当然,也可以创建/usr/share/lxc/config/common.conf.d/02-tuntap.conf 这样的配置文件来取得同样的效果

②.而如果只要对指定容器有效,则需要修改/etc/pve/lxc/[ID].conf , [ID]为你的容器编号

还没完,这样只是给了权限,实际上还没好,需要在容器的/etc/rc.local 自启动文件中添加如下

这样你执行需要tun/tap的程序时就不会出问题了

三.附录(记如何在v3版本的ovz下开启tun/tap)

a. 编辑vz.conf文件,添加iptable_nat ipt_state这2个东西

b. 手动关闭所有小鸡,重启openvz模块
/etc/init.d/vz restart

c. 对小鸡进行必要的设置(这里以小鸡100为例,100是指PID,WEB控制面板里可以看到)

d. 母鸡加载一次这些

e. 启动小鸡输入以下命令

在KVM中安装CentOS,ppp拨号时报错,根据错误日志查看,发现pptp没有穿透,所以按照如下方法配置防火墙后,拨号成功。

---
Starting negotiation on /dev/pts/1
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <auth chap MS-v2> <magic 0xc93c30c6> <pcomp> <accomp> <mrru 1500> <endpoint [MAC:00:16:3e:d8:d7:39]>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <auth chap MS-v2> <magic 0xc93c30c6> <pcomp> <accomp> <mrru 1500>
...
LCP: timeout sending Config-Requests
Connection terminated.
---
经搜索,发现有人提到PPTP穿透需要在iptables中配置,就像一般的路由器一样。通过lsmod查询,发现确实没有pptp模块,于是就通过以下命令加载了一下该模块:
# modprobe ip_nat_pptp
加载之后,果然能够正常拨上VPN了。

为了能够让iptables在每次启动的时候都自动加载该模块,最好在/etc/sysconfig/iptables-config中加入该模块:
IPTABLES_MODULES="ip_conntrack_netbios_ns ip_conntrack_ftp ip_nat_pptp"

----------------

pptpsetup --create pptp_hk --server xxx.xxx.xxx.xxx --username xxxxxxx --password xxxxxxx --encrypt

pppd call pptp_hk

2.0.0818固件降级比较麻烦,还好有热心网友分享直刷1.5固件

链接:http://pan.baidu.com/s/11AGbG 密码:zhh6

降级到1.5后,进行获取root权限和ssh权限。

telnet连不上,是因为openwrt只会在root账户没有密码的情况下才允许telnet连接。
所以,构造请求 ;passwd -d root;
http://192.168.11.1/cgi-bin/luci/;stok=6162fd66813c8381229b2fe99993d61e/api/devices/allowConnect?mac=%3Bpasswd%20-d%20root%3B
删除 root密码
telnet 连接 成功

修改opkg.conf,更换openwrt源

更新opkg  opkg update
安装dropbear opkg install dropbear
设置 dropbear 自启动 修改 /etc/rc.local 添加一行 /usr/sbin/dropbear
本来可以执行 /etc/init.d/dropbear rc.common但报错了,不管了,有空再看,先粗暴的直接执行命令。

查看一下 iptables,确认22 端口开放;
重启,发现ssh可以连了。
那就再telnet上去,设置root密码。

参考:http://www.right.com.cn/forum/thread-161455-1-1.html

 

然而刷openWRT并不需要获取ssh权限,直接使用telnet将下载的uboot和潘多拉固件刷入路由器。这个就比较容易了,执行几个命令而已。

参考:http://www.7po.com/thread-456939-1-1.html

http://www.centoscn.com/CentosServer/test/2014/1120/4153.html

OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件。使用OpenVPN可以方便地在家庭、办公场所、住宿酒店等不同网络访问场所之间搭建类似于局域网的专用网络通道。

使用OpenVPN配合特定的代理服务器,可用于访问Youtube、FaceBook、Twitter等受限网站,也可用于突破公司的网络限制。

一、服务器端安装及配置

服务器环境:干净的CentOS6.3 64位系统

内网IP:10.143.80.116

外网IP:203.195.xxx.xxx

OpenVPN版本:OpenVPN 2.3.2 x86_64-redhat-linux-gnu

    1、安装前准备

# 关闭selinux
setenforce 0
sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
# 安装openssl和lzo,lzo用于压缩通讯数据加快传输速度
yum -y install openssl openssl-devel
yum -y install lzo
# 安装epel源
rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
sed -i 's/^mirrorlist=https/mirrorlist=http/' /etc/yum.repos.d/epel.repo

    2、安装及配置OpenVPN和easy-rsa

# 安装openvpn和easy-rsa
yum -y install openvpn easy-rsa
# 修改vars文件
cd /usr/share/easy-rsa/2.0/
vim vars
# 修改注册信息,比如公司地址、公司名称、部门名称等。
export KEY_COUNTRY="CN"
export KEY_PROVINCE="Shandong"
export KEY_CITY="Qingdao"
export KEY_ORG="MyOrganization"
export KEY_EMAIL="me@myhost.mydomain"
export KEY_OU="MyOrganizationalUnit"
# 初始化环境变量
source vars
# 清除keys目录下所有与证书相关的文件
# 下面步骤生成的证书和密钥都在/usr/share/easy-rsa/2.0/keys目录里
./clean-all
# 生成根证书ca.crt和根密钥ca.key(一路按回车即可)
./build-ca
# 为服务端生成证书和密钥(一路按回车,直到提示需要输入y/n时,输入y再按回车,一共两次)
./build-key-server server
# 每一个登陆的VPN客户端需要有一个证书,每个证书在同一时刻只能供一个客户端连接,下面建立2份
# 为客户端生成证书和密钥(一路按回车,直到提示需要输入y/n时,输入y再按回车,一共两次)
./build-key client1
./build-key client2
# 创建迪菲·赫尔曼密钥,会生成dh2048.pem文件(生成过程比较慢,在此期间不要去中断它)
./build-dh
# 生成ta.key文件(防DDos攻击、UDP淹没等恶意攻击)
openvpn --genkey --secret keys/ta.key

查看keys目录下生成的文件:

wKioL1RhllmyOZW9AAKmWJ5x4tg383.jpg

    3、创建服务器端配置文件

# 在openvpn的配置目录下新建一个keys目录
mkdir /etc/openvpn/keys
# 将需要用到的openvpn证书和密钥复制一份到刚创建好的keys目录中
cp /usr/share/easy-rsa/2.0/keys/{ca.crt,server.{crt,key},dh2048.pem,ta.key} /etc/openvpn/keys/
# 复制一份服务器端配置文件模板server.conf到/etc/openvpn/
cp /usr/share/doc/openvpn-2.3.2/sample/sample-config-files/server.conf /etc/openvpn/
# 查看server.conf里的配置参数
grep '^[^#;]' /etc/openvpn/server.conf
# 编辑server.conf
vim /etc/openvpn/server.conf
port 1194
# 改成tcp,默认使用udp,如果使用HTTP Proxy,必须使用tcp协议
proto tcp
dev tun
# 路径前面加keys,全路径为/etc/openvpn/keys/ca.crt
ca keys/ca.crt
cert keys/server.crt
key keys/server.key  # This file should be kept secret
dh keys/dh2048.pem
# 默认虚拟局域网网段,不要和实际的局域网冲突即可
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
# 10.0.0.0/8是我这台VPN服务器所在的内网的网段,读者应该根据自身实际情况进行修改
push "route 10.0.0.0 255.0.0.0"
# 可以让客户端之间相互访问直接通过openvpn程序转发,根据需要设置
client-to-client
# 如果客户端都使用相同的证书和密钥连接VPN,一定要打开这个选项,否则每个证书只允许一个人连接VPN
duplicate-cn
keepalive 10 120
tls-auth keys/ta.key 0 # This file is secret
comp-lzo
persist-key
persist-tun
# OpenVPN的状态日志,默认为/etc/openvpn/openvpn-status.log
status openvpn-status.log
# OpenVPN的运行日志,默认为/etc/openvpn/openvpn.log 
log-append openvpn.log
# 改成verb 5可以多查看一些调试信息
verb 5

4、配置内核和防火墙,启动服务

# 开启路由转发功能
sed -i '/net.ipv4.ip_forward/s/0/1/' /etc/sysctl.conf
sysctl -p
# 配置防火墙,别忘记保存
iptables -I INPUT -p tcp --dport 1194 -m comment --comment "openvpn" -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
service iptables save
# 启动openvpn并设置为开机启动
service openvpn start
chkconfig openvpn on

    5、创建客户端配置文件

# 复制一份client.conf模板命名为client.ovpn
cp /usr/share/doc/openvpn-2.3.2/sample/sample-config-files/client.conf client.ovpn
# 编辑client.ovpn
vim client.ovpn
client
dev tun
# 改为tcp
proto tcp
# OpenVPN服务器的外网IP和端口
remote 203.195.xxx.xxx 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
# client1的证书
cert client1.crt
# client1的密钥
key client1.key
ns-cert-type server
# 去掉前面的注释
tls-auth ta.key 1
comp-lzo
verb 3

二、Windows客户端安装及配置

客户端系统:Windows7 64位

内网IP:172.16.4.4

OpenVPN版本:OpenVPN 2.3.3 Windows 64位

    1、下载安装OpenVPN

OpenVPN 2.3.3 Windows 32位 安装文件:

http://swupdate.openvpn.org/community/releases/openvpn-install-2.3.3-I002-i686.exe

OpenVPN 2.3.3 Windows 64位 安装文件:

http://swupdate.openvpn.org/community/releases/openvpn-install-2.3.3-I002-x86_64.exe

    2、配置client

将OpenVPN服务器上的client.ovpn、ca.crt、client1.crt、client1.key、ta.key上传到Windows客户端安装目录下的config文件夹(C:\Program Files\OpenVPN\config)

    3、启动OpenVPN GUI

在电脑右下角的openvpn图标上右击,选择“Connect”。正常情况下应该能够连接成功,分配正常的IP。

wKiom1RhjGbijB7xAABMoQo3Tnc522.jpg

wKioL1RhjgyxiZ17AAIRIRCqqws094.jpg    

    4、测试

wKiom1RhjfPD8_AYAAGxUvt_PCA503.jpg

ping通服务器的内网IP,说明已经接入到服务器的内部网络。

到OpenVPN服务器上查看客户端的连接情况,查看状态文件/etc/openvpn/openvpn-status.log:

参考文章:http://www.miui.com/thread-442048-1-1.html

 

环境:笔记本电脑,win7系统
设备:小米手机(强君的)
手机操作:打开usb绑定和wifi热点,设置wifi热点,配置ssid名和密码
PC操作:系统会自动安装usb驱动,生成网卡,本地连接2 设置IP为192.168.42.1 掩码255.255.255.0,然后在本地连接(也就是能上网的网卡)右键属性设置共享,允许本地连接2通过它上网。
如果无法识别驱动,装个豌豆荚就行了。
手机操作:安装quicksshd,豌豆荚有下载
PC操作:远程通过putty连接到手机,手机的IP在quciksshd上可以查到
PUTTY操作:
busybox sysctl -a | grep ip_forward
#查看是否存在net.ipv4.ip_forward = 1
#如果不存在
echo 1 >/proc/sys/net/ipv4/ip_forward
#将如下写成shell,比如share_aphot.sh
busybox route add default gw 192.168.42.1
iptables -F
iptables -t nat -A POSTROUTING -s 192.168.43.0/24 -o usb0 -j MASQUERADE
然后赋予执行权限
chmod 777 share_aphot.sh
运行,就能上网了。

将来如果需要手机上网的时候,在putty里执行share_aphot.sh就行了,如果为了方便可以在手机上安装超级终端,登录进去执行su获得root权限再执行share_aphot.sh。

 

第2层隧道协议 (L2TP),是 VPN 隧道协议的一种,是 PPTP 的后续版本。L2TP 支持两端点间多隧道,但通常要由 IPSec 来提供加密和验证功能,可建立变动的客户端到固定服务器的连接。

一、安装 IPSec

IPSec 用于对 IP 数据包进行加密和验证,通常使用 Openswan 来实现 IPSec。

1.1、安装编译工具

1
apt-get -y install build-essential

1.2、安装 OpenSwan 依赖包

1
apt-get -y install libgmp3-dev flex bison

1.3、编译安装 OpenSwan

1
2
3
4
wget -c http://www.openswan.org/download/openswan-2.6.33.tar.gz
tar -zxf openswan-2.6.33.tar.gz
cd openswan-2.6.33
make programs install

1.4、编辑 IPSec 配置文件

注意将"192.168.1.102"换成服务器公网IP。

1
cp /etc/ipsec.conf /etc/ipsec.conf.old
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
cat >/etc/ipsec.conf<<EOF
version 2.0

config setup
    nat_traversal=yes
    virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12
    oe=off
    protostack=netkey

conn L2TP-PSK-NAT
    rightsubnet=vhost:%priv
    also=L2TP-PSK-noNAT

conn L2TP-PSK-noNAT
    authby=secret
    pfs=no
    auto=add
    keyingtries=3
    rekey=no
    ikelifetime=8h
    keylife=1h
    type=transport
    left=192.168.1.102
    leftprotoport=17/1701
    right=%any
    rightprotoport=17/%any
EOF

1.5、设置 PSK 预共享密钥

注意将"192.168.1.102"换成服务器公网IP。将"123456"换成你自己的PSK。

1
2
3
cat >/etc/ipsec.secrets<<EOF
192.168.1.102 %any: PSK "123456"
EOF

1.6、调整网络策略

for 循环语句,请一行一行地输入,输完后按回车。

1
2
3
4
5
for each in /proc/sys/net/ipv4/conf/*
do
echo 0 > $each/accept_redirects
echo 0 > $each/send_redirects
done

1.7、重启IPSec 服务

1
2
/etc/init.d/ipsec restart
/usr/local/sbin/ipsec verify

可尝试使用 L2TP/IPSec 客户端连接一次,以测试 IPSec 部分配置是否成功。

1
cat /var/log/auth.log | grep pluto

如出现"IPsec SA established transport mode"则成功了。

二、安装 L2TP

使用 xl2tpd 来实现 L2TP,另外要注意的是 xl2tpd 需要从 rp-l2tp 中提取 l2tp-control。

2.1、提取 l2tp-control

1
2
3
4
5
6
7
8
wget http://nchc.dl.sourceforge.net/project/rp-l2tp/rp-l2tp/0.4/rp-l2tp-0.4.tar.gz
tar zxvf rp-l2tp-0.4.tar.gz
cd rp-l2tp-0.4
./configure
make
cp handlers/l2tp-control /usr/local/sbin/
mkdir /var/run/xl2tpd/
ln -s /usr/local/sbin/l2tp-control /var/run/xl2tpd/l2tp-control

2.2、编译安装 xl2tpd

1
apt-get -y install libpcap-dev #安装依赖包
1
2
3
4
5
wget -c http://www.xelerance.com/wp-content/uploads/software/xl2tpd/xl2tpd-1.2.8.tar.gz
tar -zxf xl2tpd-1.2.8.tar.gz
cd xl2tpd-1.2.8
make install
mkdir /etc/xl2tpd

2.3、编辑 xl2tpd 配置文件

ip range 是连接上来的客户端所获得的服务器端内网的 IPv4 地址段。
local ip 是 pppX 所占用的那个 IP 地址。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cat >/etc/xl2tpd/xl2tpd.conf<<EOF
[global]
ipsec saref = yes

[lns default]
local ip = 10.10.11.1
ip range = 10.10.11.2-10.10.11.245
refuse chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
EOF

三、PPP 的安装配置

3.1、安装 ppp 包

1
apt-get -y install ppp

3.2、配置 options.xl2tpd

主要是改MS-DNS,其他默认。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat >/etc/ppp/options.xl2tpd<<EOF
require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
asyncmap 0
auth
crtscts
lock
hide-password
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
EOF

四、添加 VPN 用户

chap-secrets 文件为4段,分别是:用户名、服务器名称、密码、分配给客户端的IP。
服务器名可以是l2tpd 或 pptpd,*号代表全部。
密码以明文填写,不需进行加密。
最后的*号代表从remoteip指定的IP段随机分配

1
2
3
cat >>/etc/ppp/chap-secrets<<EOF
user * 123456 *
EOF

五、配置数据包转发

否则连接VPN后,只能访问服务器资源,而不能访问这台服务器以外的资源。

1
2
sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.conf
sysctl -p

开启iptables转发

1
iptables -t nat -A POSTROUTING -j MASQUERADE

设置MTU

1
iptables -I FORWARD -p tcp --syn -i ppp+ -j TCPMSS --set-mss 1356

六、启动 xl2tpd 服务

1
2
/usr/local/sbin/xl2tpd
/usr/local/sbin/xl2tpd -D #以调式模式启动

参考资料:

1. http://www.linuxhomenetworking.com/wiki/index.php
2. http://www.linode.com/wiki/index.php/AndroidL2TPPSKServer
3. https://humou.net/blog/201102061326.html
4. http://b.gkp.cc/2010/06/19/setup-ipsec-l2tp-on-centos-55/
5. http://apple4.us/2010/05/setting-up-l2tp-vpn-on-debian-ubuntu.html

原文地址 : http://wangyan.org/blog/debian-l2tp-ipsec-vpn.html
本站遵循 : 知识共享署名-非商业性使用-相同方式共享 3.0 版权协议
版权声明 : 原创文章转载时,请务必以超链接形式标明 文章原始出处

买了一台米国的KVM,一年才200块,可以用VPN,自己架设了一套,以下是参考出处:

http://www.onelone.com/tutorial/vpn2.html

感谢作者

Debian架设VPN其实跟CentOS相差不大,大家要设置的内容一样,只是具体执行命令有差别而已,所以本文只是简单讲解一下,如有不明请参看我的上一篇CentOS Linux VPS架设VPN教程。闲话少说,下面是教程:

 

一、确认PPP和TUN启用后,先升级系统然后重启一次:

 

apt-get update
reboot

 

1.png

二、安装pptpd:apt-get install pptpd 然后配置pptpd,编辑 /etc/pptpd.conf  文件,去掉下面两行前面#

 

localip 192.168.0.1
remoteip 192.168.0.234-238,192.168.10.245

 

三、配置 DNS,编辑 /etc/ppp/options 文件,加入下面两行:

 

ms-dns 8.8.8.8
ms-dns 8.8.4.4

 

四、开启IP转发,编辑 /etc/sysctl.conf 文件,去掉下面代码前面的#

 

net.ipv4.ip_forward=1

 

五、添加VPN用户名密码,编辑 /etc/ppp/chap-secrets 文件,按下面格式输入:

 

用户名    pptpd       密码      *

 

六、把转发规则写成文件,执行命令:vi /etc/pptpdfirewall.sh 然后内容输入:

 

sudo /sbin/iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT 
--to-source 你的VPS公网IP

sudo /sbin/iptables -A FORWARD -s 192.168.0.0/24 -p tcp -m tcp --tcp-flags 
FIN,SYN,RST,ACK SYN -j TCPMSS --set-mss 1356

 

七、设置文件执行权限,执行下面命令:

 

chmod 755 /etc/pptpdfirewall.sh

 

八、设置开机启动,执行命令:vi /etc/init.d/rc.local 然后在最后一行加入下面代码:

 

sh /etc/pptpdfirewall.sh

 

九、重启VPS后就可以正常使用你的VPN了:

 

reboot

 

2.png 

最后,如果连接出现错误619或者错误734,请参看这里

 

yum -y install wget --noplugins
wget freevps.us/downloads/nginx-centos-6.sh -O - | bash

注意安装完成后 php-fpm 默认跑在 apache 用户上。

安装完成后,如果你需要的 PHP 扩展未安装,可输入如下命令查询:

yum list | grep ^php*

找到了扩展的名字,就可以直接安装了:

# 以 php memcache 扩展为例
yum install -y php-pecl-memcached

此外还遇到的问题是 DOMDocument 找不到,直接 yum 安装 php-xml:

yum install -y php-xml

所有扩展 yum 安装完成后都需要重新启动 / 载入 php-fpm。
贴下内容来看看(非最新版本,请直接下载 .sh 文件):

#!/bin/bash

##################
# disable apache #
##################
service httpd stop 
chkconfig httpd off
service xinetd stop
chkconfig xinetd off
service saslauthd stop
chkconfig saslauthd off
service sendmail stop
chkconfig sendmail off
service postfix stop
chkconfig postfix off

#Optimize yum on OpenVZ
if [ -e "/proc/user_beancounters" ]
then
  sed -i 's/plugins=1/plugins=0/' /etc/yum.conf
fi

#remove all current PHP and MySQL, will reinstall later. Also, remove apache 
yum -y remove httpd php mysql rsyslog sendmail postfix

###################
# Add a few repos #
###################
# install the Atomic repo for php and nginx (may use epel for nginx depending on version)
wget -q -O - http://www.atomicorp.com/installers/atomic | sh

# RPMForge for nginx dependencies
rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm

#EPEL for syslog-ng and nginx (may use atomic for nginx depending on version)
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm

################################
# Install PHP, NGINX and MySQL #
################################
#yum install GeoIP libGeoIP.so.1 --enablerepo=repoforge
yum -y install mysql-server php-fpm php-mysql php-gd nginx nano exim syslog-ng

#################
# install MySQL #
#################
#yum -y install mysql-server
cat > /etc/my.cnf <<END
[mysqld]
default-storage-engine = myisam
key_buffer = 8M
query_cache_size = 8M
query_cache_limit = 4M
max_connections=25
thread_cache=1
skip-innodb 
query_cache_min_res_unit=0
tmp_table_size = 4M
max_heap_table_size = 4M
table_cache=256
concurrent_insert=2 
END
echo  Do not worry if you see a error stopping MySQL or NGINX
/etc/init.d/mysqld stop
/etc/init.d/mysqld start

####################
# Set up NGINX PHP #
####################
cat > /etc/nginx/php <<END
index index.php index.html index.htm;

location ~ \.php$ {

   include fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
  try_files \$uri =404;
    fastcgi_pass 127.0.0.1:9000;
    error_page 404 /404page.html; #makes nginx return it's default 404 
#	page instead of a blank page

} 
END

cat > /etc/nginx/nginx.conf <<END
user              nginx nginx;
worker_processes  2;

error_log         logs/error.log;

pid               logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 64M;
    sendfile        on;
    tcp_nopush      on;

    keepalive_timeout  3;

    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types      text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    server_tokens off;

    include /etc/nginx/conf.d/*;
} 
END
rm /etc/nginx/conf.d/*
cat > /etc/nginx/conf.d/default.conf <<END
server {
    listen 80 default;
    server_name _;
    root /var/www/html;
    include php;

  } 
END
mkdir /usr/share/nginx/logs/
service nginx restart
chkconfig nginx on
chkconfig mysqld on

cat > /etc/php-fpm.d/www.conf <<END
; Start a new pool named 'www'.
[www]

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000

; Set listen(2) backlog. A value of '-1' means unlimited.
; Default Value: -1
;listen.backlog = -1

; List of ipv4 addresses of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any
listen.allowed_clients = 127.0.0.1

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions. 
; Default Values: user and group are set as the running user
;                 mode is set to 0666
;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0666

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache

; Choose how the process manager will control the number of child processes.
; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives:
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;             pm.start_servers     - the number of children created on startup.
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
; Note: This value is mandatory.
pm = dynamic

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes to be created when pm is set to 'dynamic'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI.
; Note: Used when pm is set to either 'static' or 'dynamic'
; Note: This value is mandatory.
pm.max_children = 5

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 1

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 1

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 3

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
pm.max_requests = 500

; The URI to view the FPM status page. If this value is not set, no URI will be
; recognized as a status page. By default, the status page shows the following
; information:
;   accepted conn    - the number of request accepted by the pool;
;   pool             - the name of the pool;
;   process manager  - static or dynamic;
;   idle processes   - the number of idle processes;
;   active processes - the number of active processes;
;   total processes  - the number of idle + active processes.
; The values of 'idle processes', 'active processes' and 'total processes' are
; updated each second. The value of 'accepted conn' is updated in real time.
; Example output:
;   accepted conn:   12073
;   pool:             www
;   process manager:  static
;   idle processes:   35
;   active processes: 65
;   total processes:  100
; By default the status page output is formatted as text/plain. Passing either
; 'html' or 'json' as a query string will return the corresponding output
; syntax. Example:
;   http://www.foo.bar/status
;   http://www.foo.bar/status?json
;   http://www.foo.bar/status?html
; Note: The value must start with a leading slash (/). The value can be
;       anything, but it may not be a good idea to use the .php extension or it
;       may conflict with a real PHP file.
; Default Value: not set 
;pm.status_path = /status

; The ping URI to call the monitoring page of FPM. If this value is not set, no
; URI will be recognized as a ping page. This could be used to test from outside
; that FPM is alive and responding, or to
; - create a graph of FPM availability (rrd or such);
; - remove a server from a group if it is not responding (load balancing);
; - trigger alerts for the operating team (24/7).
; Note: The value must start with a leading slash (/). The value can be
;       anything, but it may not be a good idea to use the .php extension or it
;       may conflict with a real PHP file.
; Default Value: not set
;ping.path = /ping

; This directive may be used to customize the response of a ping request. The
; response is formatted as text/plain with a 200 response code.
; Default Value: pong
;ping.response = pong

; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_terminate_timeout = 0

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_slowlog_timeout = 0

; The log file for slow requests
; Default Value: /var/log/php-fpm.log.slow
;slowlog = /var/log/php-fpm.log.slow

; Set open file descriptor rlimit.
; Default Value: system defined value
;rlimit_files = 1024

; Set max core size rlimit.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
;rlimit_core = 0

; Chroot to this directory at the start. This value must be defined as an
; absolute path. When this value is not set, chroot is not used.
; Note: chrooting is a great security feature and should be used whenever 
;       possible. However, all PHP paths will be relative to the chroot
;       (error_log, sessions.save_path, ...).
; Default Value: not set
;chroot = 

; Chdir to this directory at the start. This value must be an absolute path.
; Default Value: current directory or / when chroot
;chdir = /var/www

; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Default Value: no
;catch_workers_output = yes

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

; Additional php.ini defines, specific to this pool of workers. These settings
; overwrite the values previously defined in the php.ini. The directives are the
; same as the PHP SAPI:
;   php_value/php_flag             - you can set classic ini defines which can
;                                    be overwritten from PHP call 'ini_set'. 
;   php_admin_value/php_admin_flag - these directives won't be overwritten by
;                                     PHP call 'ini_set'
; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.

; Defining 'extension' will load the corresponding shared extension from
; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
; overwrite previously defined php.ini values, but will append the new value
; instead.

; Default Value: nothing is defined by default except the values in php.ini and
;                specified at startup with the -d argument
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
;php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M
php_admin_value[upload_max_filesize] = 32M
END

mkdir /var/www
mkdir /var/www/html/
useradd apache
service php-fpm start
chkconfig php-fpm on
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save

wget freevps.us/downloads/setup-vhost.sh -O /bin/setup-vhost
chmod 755 /bin/setup-vhost
echo "alias nano='nano -w'" >> ~/.bashrc
clear
echo Installation done.
echo to create a vhost, run
echo setup-vhost example.com
echo do not include the www. subdomain.

转:http://www.cnblogs.com/vicowong/archive/2011/12/01/2116212.html

一、CentOS 6安装

1.1 使用VMware 虚拟机进行安装,进行安装界面

(分配内存必须大于1G,否则不会显示图型安装界面,网络设置使用“桥接模式” 即"Bridged"模式)

1.2 选择 Install or upgrade an existing system

1.3 在"Disc found" 框 选择 "skip"

1.4 next 选择 "chinese(simplified)(中文(简介 )) next

1.5 选择 "美国英语式" 下一步

1.6 选择 "基本 存储设备" 下一步

1.7 弹出"警告"框时,选择"重新初始化所有"

1.8 主机名可以保留默认,点击”配置网络“ 弹出“网络连接” 双击“System eth0"

1.9 弹出“正在编辑 System eth0" 选择"自动连接" 点击"应用“ ,点击”关闭“ 关闭”网络连接“框 下一步

1.10 不要选择“系统时钟使用UTC时间” 下一步

1.11 输入并确认 ”根密码“ 下一步

1.12 选择"替换现有Linux系统 " 下一步 “将修改写入磁盘"

1.13 选择"Basic Server" 下一步

1.14 大概一共590个软件包,复制安装完成后,点击“重新引导”,重新启动计算机

1.15 (安装完成后,可以将虚拟机内存由1G,改为512M)

 

二、更新centos6

2.1 更新

yum update -y

2.2 查看版本,确认为"CentOS Linux release 6.0 (Final)"

lsb_release -a

2.2 安装编译器

yum install -y gcc gcc-c++

 

三、安装Google-perftools (使用tcmalloc 加速 mysql 和 nginx)

3.1下载需要的文件

下载 libunwind-1.0.1.tar.gz 到 /usr/local/data-original

3.2 安装libunwind

cd /usr/local/data-original/

tar zvxf libunwind-1.0.1.tar.gz

cd libunwind-1.0.1

./configure --enable-shared

make && make install

3.3 安装google-perftools

cd /usr/local/data-original/

tar zvxf google-perftools-1.8.3.tar.gz

cd google-perftools-1.8.3

./configure --enable-shared --enable-frame-pointers

make && make install

3.4 更新,使动态链接库能够被系统共享

echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf

ldconfig

 

四、安装mysql

4.1.下载文件

下载 ncurses-5.9.tar.gz到/usr/local/data-original
下载 bison-2.5.tar.gz到/usr/local/data-original
下载 cmake-2.8.6.tar.gz到/usr/local/data-original
下载 mysql-5.5.18.tar.gz到/usr/local/data-original

4.2 安装ncurses

yum install ncurses-devel -y

cd /usr/local/data-original/

tar zvxf ncurses-5.9

./configure

make && make install

4.3 安装cmake

cd /usr/local/data-original/

tar zvxf cmake-2.8.6.tar.gz

cd cmake-2.8.6

./bootstrap

make && make install

4.4 安装bison

cd /usr/local/data-original/

tar zvxf bison-2.5.tar.gz

cd bison-2.5

./configure

make && make install

4.5 创建mysql需要的目录、配置用户和用户组

groupadd mysql

useradd -g mysql mysql

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

4.6.安装mysql (需要 cmake ncurses-devel bison 库)

4.6.1 安装

cd /usr/local/data-original/

tar zvxf mysql-5.5.18.tar.gz

cd mysql-5.5.18

cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DSYSCONFDIR=/etc/ -DWITH_SSL=yes -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=on

make && make install

4.6.2 创建软连接

ln -s /opt/mysql/lib/lib* /usr/lib/

4.6.3 配置mysql数据库

cd /opt/mysql
./scripts/mysql_install_db --basedir=/opt/mysql/ --datadir=/data/mysql/ --user=mysql

4.6.4 复制配置文件

cp ./support-files/my-large.cnf /etc/my.cnf

4.6.5 修改配置文件,设置默认使用utf8编码

vim /etc/my.cnf
在[client]下添加一行
default-character-set = utf8
在[mysqld]下添加一行
character-set-server = utf8

4.6.6 设置mysql开机自动启动服务
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on

4.6.7 修改服务配置文件
vim /etc/rc.d/init.d/mysqld

根据设定需要,修改mysqld文件中的下面两项
basedir=/opt/mysql
datadir=/data/mysql

4.6.8 启动mysqld服务
service mysqld start

4.6.9 数据库初始化及修改root密码(root初始密码为空)
./bin/mysql_secure_installation
根据提示操作

4.6.10 软连接mysql

ln -s /opt/mysql/bin/mysql /bin

4.6.11 重启centos后,尝试用root连接mysql
mysql -u root -p

成功登录后查看状态

status;

4.6.12 使用tcmalloc优化mysql ( 需要安装google-perftools)

修改MySQL启动脚本(根据你的MySQL安装位置而定)
vim /opt/mysql/bin/mysqld_safe

在# executing mysqld_safe的下一行,加上:
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

4.6.13 重启服务,查看tcmalloc是否生效 (第二条命令显示即生效)

service mysqld restart

lsof -n | grep tcmalloc

如果显示以下类似的信息,即表示tcmalloc生效

[root@localhost mysql]# lsof -n|grep tcmalloc
mysqld 30347 mysql mem REG 253,0 2177570 544322 /usr/local/lib/libtcmalloc.so.0.2.2

 

五、安装Nginx

5.1.准备安装

下载 pcre-8.20.tar.gz到/usr/local/data-original

下载 nginx-1.0.10.tar.gz到/usr/local/data-original

5.2 更新包

yum install zlib* openssl* -y

 

5.3 安装Pcre

cd /usr/local/data-original/

tar zvxf pcre-8.20.tar.gz

cd pcre-8.20

./configure

make && make install

5.4 创建www用户和组,创建www虚拟主机使用的目录,以及Nginx使用的日志目录,并且赋予他们适当的权限

groupadd www
useradd -g www www

mkdir -p /data/www
chmod +w /data/www
chown -R www:www /data/www

为tcmalloc添加目录,并且赋予适当权限

mkdir -p /tmp/tcmalloc/

chown -R www:www /tmp/tcmalloc/

 

5.1 安装Nginx (需要 pcre google-perftools 库)

5.5.1 安装

cd /usr/local/data-original/

tar zvxf nginx-1.0.10.tar.gz

伪装服务器信息(可以不修改)

cd nginx-1.0.10/data-original/core

vim ./data-original/core/nginx.h

修改NGINX_VERSION为你希望显示的版号

修改NGINX_VER为你希望显示的名称

修改NGINX_VAR 为你希望显示的名称

保存

开始安装

./configure --user=www --group=www --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-google_perftools_module

make && make install

5.4.2 修改 nginx.conf ,令nginx可以 google-perftools实现加速

vim /opt/nginx/conf/nginx.conf

修改前面几行为:

user www www;
worker_processes 8;
error_log logs/error.log crit;
pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc/;
events{
use epoll;
worker_connections 65535;
}

 

5.4.3 测试和运行

cd /opt/nginx

./sbin/nginx -t

如果显示下面信息,即表示配置没问题

nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful

输入代码运行nginx服务

./sbin/nginx

ps au|grep nginx

如果显以类似下面的信息,即表示nginx已经启动

root 2013 0.0 0.0 103156 856 pts/0 S+ 03:22 0:00 grep nginx

输入代码检测是否支持加速

lsof -n | grep tcmalloc

如果显示类似下面的信息,即表示支持tcmalloc加速 (mysqld和nginx两个线程都支持)

mysqld 20818 mysql mem REG 253,0 2177570 281050 /usr/local/lib/libtcmalloc.so.0.2.2

nginx 29454 www 25w REG 253,0 0 288399 /tmp/tcmalloc/.29454

nginx 29455 www 27w REG 253,0 0 288403 /tmp/tcmalloc/.29455

 

5.5.4 打开防火墙80端口

写入规则,保存并重启服务

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

/etc/rc.d/init.d/iptables save

/etc/init.d/iptables restart

查看防火墙信息
/etc/init.d/iptables status

如果显示以下类似信息,即表示已经打开了80端口

1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80

5.5.5 编写nginx 启动服务

cd /etc/init.d

vim nginx

输入以下代码并保存

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

 

保存后,设置权限,并添加到启动服务列表中

chmod 755 /etc/init.d/nginx

chkconfig --add nginx

chkconfig --level 345 nginx on

service nginx start



六、安装PHP

6.1 准备安装

下载php-5.3.6.tar.gz到/usr/local/data-original
下载 libiconv-1.14.tar.gz到/usr/local/data-original
下载 libmcrypt-2.5.8.tar.gz到/usr/local/data-original
下载mcrypt-2.6.8.tar.gz到/usr/local/data-original
下载mhash-0.9.9.9.tar.gz到/usr/local/data-original
下载libmcrypt-2.5.8.tar.gz到/usr/local/data-original

yum -y install autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libXpm* gcc gcc-c++

 

5.2 安装libiconv (加强系统对支持字符编码转换的功能)

cd /usr/local/data-original/

tar zvxf libiconv-1.14.tar.gz

cd libiconv-1.14

./configure --prefix=/usr/local

make && make install

5.3 安装libmcrypt(加密算法库,PHP扩展mcrypt功能对此库有依耐关系,要使用mcrypt必须先安装此库)

5.3.1 安装libmcrypt

cd /usr/local/data-original/

tar zvxf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure

make && make install

5.3.2安装libltdl

cd libltdl/

./configure --enable-ltdl-install

make && make install

5.3.3 更新共享

ln -sf /usr/local/lib/libmcrypt.* /usr/lib64/
ln -sf /usr/local/bin/libmcrypt-config /usr/lib64/
#ln -sf /usr/local/lib/libiconv.so.2 /usr/lib64/

ldconfig

5.4 安装mhash(hash加密算法库)

5.4.1 安装mhash

cd /usr/local/data-original/

tar zvxf mhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9

./configure

make && make install

5.4.2更新共享

ln -sf /usr/local/lib/libmhash.* /usr/lib64/

ldconfig

5.5 安装mcrypt

cd /usr/local/data-original/

tar zvxf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8

ldconfig

./configure

make && make install

5.6 安装php

5.6.1 创建mysql软连接、ldap软连接

mkdir -p /opt/mysql/include/mysql
ln -s /opt/mysql/include/* /opt/mysql/include/mysql/

ln -s /usr/lib64/libldap* /usr/lib

5.6.2 安装

cd /usr/local/data-original/

tar zvxf php-5.3.8.tar.gz

cd php-5.3.8

./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --with-mysql=/opt/mysql --with-mysqli=/opt/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --disable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap

make ZEND_EXTRA_LIBS='-liconv'

make install

5.6.3 复制配置文件

cp php.ini-production /opt/php/etc/php.ini

5.6.4 安装memcache扩展(已经安装PHP)

cd /usr/local/data-original/

tar zvxf memcache-2.2.6.tar.gz

cd memcache-2.2.6

/opt/php/bin/phpize

ldconfig

./configure --with-php-config=/opt/php/bin/php-config

make && make install

修改php配置文件,支持memcache

vim /opt/php/etc/php.ini

在文件中搜索extension_dir、extension ,在相应位置添加下面两行
extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension = "memcache.so"

5.6.5 安装eaccelerator扩展(已经安装PHP)

cd /usr/local/data-original/

tar jxvf eaccelerator-0.9.6.1.tar.bz2

cd eaccelerator-0.9.6.1

/opt/php/bin/phpize

./configure --enable-eaccelerator=shared --with-php-config=/opt/php/bin/php-config

make && make install

修改php配置文件,支持eaccelerator

vim /opt/php/etc/php.ini

在文件尾加入以下代码

[eaccelerator]
zend_extension="/opt/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.log_file = "/opt/php/var/log/eaccelerator_log"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

增加eaccelerator目录
mkdir -p /tmp/eaccelerator
5.6.5 安装php-fpm

cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
vim /opt/php/etc/php-fpm.conf

修改以下地方
[global]
pid = run/php-fpm.pid-p
error_log = log/php-fpm.log
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 5s
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

[www]
user = www
group = www

5.6.6 修改nginx,支持php

vim /opt/nginx/conf/nginx.conf

找到并修改以下代码

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
include fastcgi_params;
}

 

5.6.7将php-fpm 作为服务运行

cd /usr/local/data-original/php-5.3.8
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 700 /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --level 345 php-fpm on
服务方式启动php-fpm
service php-fpm restart

 

5.6.8 编写测试页面

vim /data/www/index.php

输入代码

<html>
<head><title>hello php</title></head>
<body>
<?php phpinfo();?>
</body>
</html>

5.6.8 打开浏览器进行测试