CentOS安装OpenVZ并配置OpenVZ Web Panel

大家好!

随心博客已经彻底沦陷了。

现在变成一年一更新了……

上一次更新了一堆文章,结果VPS挂了,都没了,于是心灰意冷,闲置了好久……

这次想写一篇关于CentOS安装OpenVZ并配置OpenVZ Web Panel的文章,话说天下文章一大抄,抄来抄去都变了样。如何你第一次使用CentOS安装OpenVZ的话,你一定会跟我一样,被各种奇葩粘贴复制的文章弄的抓狂。好吧,今天随心就发挥特长,把所有网上的奇葩文章整合成一篇完成的教程,看了我的教程你绝对不会走弯路的,哇咔咔。

注意:本教程的方法建立在基于KVM构架的VPS或独立服务器上,其他的环境就不要试了,免得你伤心……

前提准备:VPS或服务器安装CentOS 6及以上系统。update系统,并安装vim编辑器。

1、关闭selinux并配置iptables(重要)

vim /etc/sysconfig/selinux
添加内容:
SELINUX=disabled
保存

开放iptables的3000端口或vim /etc/sysconfig/iptables

/sbin/iptables -I INPUT -p tcp --dport 3000 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart

2、安装OpenVZ

配置YUM源(这个办法你在网上搜到的所有相关教程全部失效,还是我总结的方法可以用,骚年)

cd /etc/yum.repos.d
wget http://download.openvz.org/openvz.repo
rpm --import  http://download.openvz.org/RPM-GPG-Key-OpenVZ
yum update -y

安装OpenVZ内核和vzctl、vzquota等工具

yum install  vzkernel
yum install vzctl vzquota

配置OS内核参数,进入/etc/sysctl.conf文件,修改下面两个参数

为了让VE访问外部网络,需要对IP进行转发
net.ipv4.ip_forward = 1
主要是控制对kernel系统信息的debug功能
kernel.sysrq = 1

使上面的配置文件生效

modprobe bridge
lsmod|grep bridge

现在reboot,重启后检查VZ服务是否运行

chkconfig --list vz
返回如下信息则代表正在运行
vz                0:off 1:off 2:on 3:on 4:on 5:on 6:off
启动之前可以先检查一下OpenVZ服务时候已经启动了
service vz status 
service vz start 

3、安装OpenVZ Web Panel

wget -O - https://raw.githubusercontent.com/sibprogrammer/owp/master/installer/ai.sh | sh

安装结束后,使用如下信息登录控制面板

http://<your-host>:3000

最后这个问题是所有教程都没有写的,一个非常奇葩的问题,当你安装好OWP后,你无法下载系统模版,也就无法创建VZ。哈哈,这个问题困扰了我几天,终于在github里发现了解决办法。引起这个BUG的原因就是一个重要的进程文件hw-daemon.rb被覆盖为空文档了,解决的办法就是重新写入这个文件的内容。

vim /opt/ovz-web-panel/utils/hw-daemon/hw-daemon.rb
复制一下内容
#!/usr/bin/env ruby

require 'webrick'
require 'xmlrpc/server.rb'

# workaround for clients with incorrect DNS records
Socket.do_not_reverse_lookup = true

ENV['PATH'] += ':/usr/sbin'

DAEMON_VERSION = '1.3'
CURRENT_DIR = File.expand_path(File.dirname(__FILE__)) + '/'
CONFIG_FILE = CURRENT_DIR + 'hw-daemon.ini'
PID_FILE = CURRENT_DIR + 'hw-daemon.pid'
LOG_FILE = CURRENT_DIR + 'hw-daemon.log'
SSL_CERT_FILE = CURRENT_DIR + "/certs/server.crt"
SSL_PKEY_FILE = CURRENT_DIR + "/certs/server.key"

$SERVER_ADDRESS = "0.0.0.0"
$SERVER_PORT = 7767
$AUTH_KEY = ""
$DEBUG = false
$LOG = WEBrick::Log.new(LOG_FILE)

$SSL_ENABLE = false
$SSL_CERT = ''
$SSL_PKEY = ''

$THREADS = {}

class HwDaemonApiHandler < XMLRPC::WEBrickServlet  
  
  def version
     DAEMON_VERSION
  end
  
  def exec(command, args = '')
    output = `#{command} #{args} 2>&1`
    exit_code = $?
    $LOG.debug("Exec command: #{command} #{args}; code: #{exit_code}; output:\n#{output}")
    { 'exit_code' => exit_code >> 8, 'output' => output }
  end
  
  def job(command, args = '')
    job_id = generate_id
    
    t = Thread.new do
      result = self.exec(command, args)
      $THREADS[job_id]['result'] = result
    end    
    
    $THREADS[job_id] = { 'thread' => t }
    
    { 'job_id' => job_id }
  end
  
  def job_status(job_id)
    found = $THREADS.has_key?(job_id)
    result = ''
    
    if found
      alive = $THREADS[job_id]['thread'].alive?
      result = $THREADS[job_id]['result'] unless alive
    end
    
    { 'found' => found, 'alive' => alive, 'result' => result }
  end
  
  def write_file(filename, content)
    File.open(filename, 'w') { |file| file.write(content) }
    $LOG.debug("Writing file: #{filename}")
  end
  
  def service(request, response)
    WEBrick::HTTPAuth.basic_auth(request, response, '') do |user, password|
      user == 'admin' && password == $AUTH_KEY
    end
     
    super
  end
  
  def handle(method, *params)
    $LOG.debug("Execute method: #{method}")    
    super
  end
  
  private
  
  def generate_id
    symbols = [('0'..'9'),('a'..'f')].map{ |i| i.to_a }.flatten
    (1..32).map{ symbols[rand(symbols.length)] }.join
  end
  
end

class HwDaemonUtil
  
  def initialize
    check_environment
    
    if (0 == ARGV.size)
      do_help
    end

    load_config
    $LOG.level = WEBrick::Log::DEBUG if $DEBUG

    if $SSL_ENABLE
      require 'webrick/https'
      $SSL_CERT = OpenSSL::X509::Certificate.new(File.open(SSL_CERT_FILE).read) if File.readable?(SSL_CERT_FILE)
      $SSL_PKEY = OpenSSL::PKey::RSA.new(File.open(SSL_PKEY_FILE).read) if File.readable?(SSL_PKEY_FILE)
    end
    
    command = ARGV[0]

    case command
      when 'start'
        do_start
      when 'stop'
        do_stop
      when 'restart'
        do_restart
      when 'status'
        do_status
      else
        do_help
    end
  end
  
  def check_environment
    if RUBY_VERSION !~ /1\.8\..+/
      puts "Ruby #{RUBY_VERSION} is not supported."
      exit(1)
    end
    
    if !File.exists?('/proc/vz/version')
      puts "Daemon should be run on the server with OpenVZ."
      exit(1)
    end
  end
  
  def do_start
    puts "Starting the daemon..."
    
    servlet = HwDaemonApiHandler.new
    servlet.add_handler("hwDaemon", servlet)   
    servlet.set_default_handler do |name, *args|
      raise XMLRPC::FaultException.new(-99, "Method #{name} missing or wrong number of parameters!")
    end
    
    server = WEBrick::HTTPServer.new(
      :Port => $SERVER_PORT,
      :BindAddress => $SERVER_ADDRESS,
      :Logger => $LOG,
      :SSLEnable => $SSL_ENABLE,
      :SSLVerifyClient => ($SSL_ENABLE ? OpenSSL::SSL::VERIFY_NONE : nil),
      :SSLCertificate => $SSL_CERT,
      :SSLPrivateKey => $SSL_PKEY,
      :SSLCertName => [ [ "CN", WEBrick::Utils::getservername ] ]
    )
    
    server.mount('/xmlrpc', servlet)
    
    ['INT', 'TERM'].each { |signal| trap(signal) { server.shutdown } }

    WEBrick::Daemon.start do
      write_pid_file
      server.start
      delete_pid_file
    end
  end
  
  def do_stop
    if (File.exists?(PID_FILE))
      pid = File.read(PID_FILE)
      $LOG.debug("Killing process with PID #{pid.to_i}")
      Process.kill('TERM', pid.to_i)
    end
    
    puts "Daemon was stopped."
  end
  
  def do_restart
    do_stop
    do_start
  end
  
  def do_status
    if (File.exists?(PID_FILE))
      puts "Daemon is running."
    else
      puts "Daemon is stopped."
      exit(1)
    end
  end
  
  def do_help
    puts "Usage: ruby hw-daemon.rb (start|stop|restart|status|help)"
    exit(1)
  end
  
  def load_config    
    file = File.new(CONFIG_FILE, 'r')
    
    while (line = file.gets)
      key, value = line.split('=', 2).each { |v| v.strip! }
      
      case key
        when 'address'
          $SERVER_ADDRESS = value
        when 'port'
          $SERVER_PORT = value
        when 'key'
          $AUTH_KEY = value
        when 'ssl'
          $SSL_ENABLE = true if value == 'on'
        when 'debug'
          $DEBUG = true if value == 'on'          
      end
    end
    
    file.close
  end
  
  def write_pid_file
    open(PID_FILE, "w") { |file| file.write(Process.pid) } 
  end
  
  def delete_pid_file
    if File.exists?(PID_FILE)
      File.unlink PID_FILE
    end
  end
  
end

HwDaemonUtil.new

现在就可以开始创建OVZ小鸡了,可以愉快的玩耍了!

作者: 随心

杂记,美文,生活,哲学,记录一个平常人生活的轨迹。作者码字不易,转载请留言告知或注明出处,谢谢,

《CentOS安装OpenVZ并配置OpenVZ Web Panel》有9个想法

    1. 是打不开网页的面板吗?这个可能需要检查一下端口设置,或者换个浏览器??还有,有些IDC提供的Linux模板有些是不一样的,所以可能出现,在有些服务器上可以用,有些服务器上不能用的情况……

  1. Pingback: 2019

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注