欢迎来到Ezhost

Ezhost是一个自动化安装常见服务器服务的Python命令库,例如LAMP、LNMP等等。

开始安装

安装python3-pip

Ezhost是基于Python3开发的,在安装Ezhost之前,需要安装python3-pip。

# 安装python3-pip
$ sudo apt-get install python3-pip

安装Ezhost

在安装Ezhost之前,需要安装一些Ubuntu中的依赖库

# 安装依赖库
$ sudo apt-get install libffi-dev libssl-dev
$ sudo pip3 install cryptography pynacl

通过pip3安装Ezhost

# 安装Ezhost
$ sudo pip3 install ezhost

使用方法

Ezhost主要是用于主机与客户机通信执行自动化安装,在主机上执行一系列的安装命令来控制客户机。

使用客户机的用户名与密码进行连接

$ ezhost -H host -U user -P password [keyword]

使用客户机的用户名与SSH KEY进行连接

$ ezhost -H host -U user -K ~/.ssh/keyfile.pem [keyword]

使用客户机的配置文件进行连接

在/config/文件夹下面创建 ezhost.ini 的配置文件,然后在此配置文件中写入如下配置:

1
2
3
4
5
6
7
8
9
[develop_server]
host=hostd
user=userd
passwd=passwordd

[test_server]
host=hostt
user=usert
keyfile=~/.ssh/keyfile.pem

Note

如果你不想暴露你的客户机密码,则可以使用SSH KEY代替密码。将 passwd=password 改成 keyfile=~/.ssh/keyfile.pem 即可.(此处假设你的SSH KEY文件保存在~/.ssh/keyfile.pem里面)

当你的配置文件创建完成后,可以使用如下命令访问到客户机:

$ ezhost -C /config/ezhost.ini develop_server [keyword]

[keyword]的使用方法

[keyword]是一系列的,已存在于Ezhost里面的服务器替代名,例如这里安装LAMP服务器,我们就把[keyword]替换成了 -s lamp

$ ezhost -C development.ini -s lamp

参数解释

-H 客户机地址
-U 客户机用户名
-P 客户机密码
-K 客户机SSH KEY路径
-C 配置文件路径

命令行列表

默认安装

默认安装就是不在询问用户,直接执行安装程序

-f 或者 --force

git代码更新

通过主机控制远程客户机的代码进行更新

-gp /var/www/html/project 或者 --git-pull /var/www/html/project

Note

其中 /var/www/html/project 是你的客户机git代码的保存目录

登录到远程客户机

通过主机登录到客户机

-login 或者 --login

登录到远程客户机的Mysql服务器

直接通过主机登录到客户机的Mysql服务器,然后就可以操作Mysql服务器的一些指令

-my 或者 --mysql

查看客户机运行中的端口

打印客户机中正在运行的端口

-ap 或者 --active-port

服务器列表

LAMP服务器

介绍

Linux + Apache + Mysql + PHP

-s lamp 或者 --server lamp

默认的配置信息

  • Mysql密码: password
  • Web工作目录: /var/www/html
  • phpinfo路径: /var/www/html/info.php

服务器重启

$ sudo service apache2 restart

LNMP Server

介绍

Linux + Nginx + Mysql + PHP

-s lnmp 或者 --server lnmp

默认的配置信息

  • nginx配置文件路径: /etc/nginx/sites-available/default
  • Mysql密码: password
  • Web工作目录: /var/www/html
  • phpinfo路径: /var/www/html/info.php

重启

# php5
$ sudo service php5-fpm restart

# php7
$ sudo service php7.0-fpm restart

# nginx
$ sudo service nginx restart

基础的Django Web服务器

介绍

Django + Mysql

-s django -p project_name 或者 --server django --project project_name

Note

如果使用 -p project_name 参数,则会创建一个以 project_name 为名字的文件夹,并将Django项目放置在该文件夹下面,否则默认使用 demo 作为文件夹名。

默认的配置信息

  • Mysql密码: password
  • 项目路径: /var/www/html/project_name
  • Python虚拟环境路径: ~/.project_name

Django的Mysql配置

默认安装完成后,Django使用的数据库是Sqlite3,我们需要将其修改成为Mysql

  • 编辑``/var/www/html/project_name/project_name/settings.py``
$ cd /var/www/html/project_name/project_name
$ vim settings.py
  • 修改数据库配置为Mysql
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': 'localhost',
        'NAME' : 'your_database_name',
        'USER' : 'root',
        'PASSWORD' : 'password'
    }
}
  • 激活Django项目的虚拟环境
$ source ~/.project_name/bin/activate
  • 进入 /var/www/html/project_name 目录并执行数据库迁移
$ cd /var/www/html/project_name
$ python manage.py migrate

高级的Django Web服务器

介绍

Django + Uwsgi + Nginx + Supervisor

-s django-uwsgi -p project_name 或者 --server django-uwsgi --project project_name

默认的配置信息

基本配置:

  • 项目目录: /var/www/html/project_name
  • python虚拟环境: ~/.project_name
  • Mysql密码: password

服务器配置:

  • nginx配置文件路径: /etc/nginx/sites-enabled/default
  • uwsgi配置文件路径: /var/www/html/project_name/project_name.ini
  • supervisor配置文件路径: /etc/supervisor/conf.d/project_name_sysd.conf

日志配置:

  • django基本输出日志: /var/log/project_name_out.log
  • django错误信息日志: /var/log/project_name_error.log

重启

# nginx服务器重启
$ service nginx restart

# uwsgi服务器重启
$ sudo supervisorctl restart project_name

# 启动uwsgi服务
$ sudo supervisorctl start project_name

# 重加载supervisor配置文件
$ sudo supervisorctl reread
$ sudo supervisorctl update

设置supervisor默认启动

# 针对ubuntu 16,supervisor重启后自动运行
$ sudo systemctl enable supervisor
$ sudo systemctl start supervisor

# 针对ubuntu 14
$ sudo update-rc.d supervisor enable

项目列表

LNMP中安装Wordpress项目

介绍

在LNMP服务器中安装Wordpress项目

-s lnmp-wordpress -p news 或者 --server lnmp-wordpress --project news

Note

-p 或者 --project 指代的是需要创建的wordpress项目名

数据库的创建

Wordpress默认使用Mysql作为数据库,所以我们需要提前安装好Mysql数据库

  1. 登录到客户机的Mysql服务器

    $ ezhost -H host -U user -P password --mysql
    
  2. 输入数据库密码并创建数据库

    mysql> create database name_you_want;
    

默认的配置信息

  • Mysql密码: password
  • 项目路径: /var/www/html/news

大数据应用列表(运行在Ubuntu 16)

Kafka

Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理所有的动作流数据。 它通过O(1)的磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能。 拥有高吞吐量即使是非常普通的硬件,Kafka也可以支持每秒数百万的消息。 同时Kafka支持通过服务器和消费机集群来分区消息。

安装关键字

-s bigdata -ba kafka 或者 --server bigdata --bigdata-app kafka

话题(topic)

# 创建话题
sudo ./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

# 检查话题
sudo ./bin/kafka-topics.sh --list --zookeeper localhost:2181

# 删除话题
sudo ./bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic test

运行生产者(producer)

sudo ./bin/kafka-console-producer.sh --broker-list <IP Address>:9092 --topic test

运行消费者(consumer)

sudo ./bin/kafka-console-consumer.sh --bootstrap-server <IP Address>:9092 --topic test --from-beginning

Kafka安装配置

  • 安装路径:/opt/kafka
  • bin目录: /opt/kafka/bin
  • zookeeper配置文件:/opt/kafka/config/zookeeper.properties
  • kafka配置文件:/opt/kafka/config/server.properties

服务

# 查看状态
sudo systemctl status kafka
sudo systemctl status zookeeper

# 重启服务
sudo systemctl restart kafka
sudo systemctl restart zookeeper

Kafka分配多个启动端口

假设我想为我安装的kafka服务启动三个运行端口:9092,9093,9094,则我需要创建如下配置 文件:

[kafka_server]
host=192.168.33.31
user=ubuntu
passwd=18fc2f8e53c021a965cd9628
KAFKA_PORTS=9092, 9093, 9094

Note

我们在配置文件中使用 KAFKA_PORTS 来指定需要开启的端口,用“,“分割多个端口。

然后直接通过配置文件安装方法,假设创建的配置文件名为 config.ini ,则可以执行下列命令 生成多端口服务器:

> ezhost -C config.ini kafka_server -s bigdata -ba kafka

登录到服务器检查端口服务是否开启:

> sudo systemctl status zookeeper.service
> sudo systemctl status kafka-9092.service
> sudo systemctl status kafka-9093.service
> sudo systemctl status kafka-9094.service

Spark

Spark 是专为大规模数据处理而设计的快速通用的计算引擎。Spark 是一种与 Hadoop 相似的 开源集群计算环境,但是两者之间还存在一些不同之处,这些有用的不同之处使 Spark 在某些 工作负载方面表现得更加优越,换句话说,Spark 启用了内存分布数据集,除了能够提供交互式查询外, 它还可以优化迭代工作负载。

安装关键字

-s bigdata -ba spark 或者 --server bigdata --bigdata-app spark

安装配置

Spark
  • 安装路径:/opt/spark
  • 配置路径:/opt/spark/conf
Hadoop
  • 安装路径:/opt/hadoop
  • 配置路径:/opt/hadoop/etc

测试

> cd /opt/spark
> ./bin/run-example SparkPi 10
> sudo ./bin/pyspark --master local[2]

Spark Cluster

demo_config.ini

[demo_spark_master]
host=192.168.33.25
user=ubuntu
passwd=18fc2f8e53c021a965cd9628
SPARK_WORKER_MEMORY=512M

[demo_spark_slave1]
host=192.168.33.26
user=ubuntu
passwd=18fc2f8e53c021a965cd9628

[demo_spark_slave2]
host=192.168.33.27
user=ubuntu
passwd=18fc2f8e53c021a965cd9628

Note

可以在Spark主(master)服务器中配置每个从(slave)服务器的执行内存大小, 通过 SPARK_WORKER_MEMORY 进行赋值。

安装实例

我们把 demo_config.ini 中的 demo_spark_master 做为spark主(master)服务器, 把 demo_spark_slave1 作为第一个从(slave)服务器,则安装命令如下:

ezhost -C /vagrant/ezhost/data/demo_config.ini demo_spark_master -s bigdata -ba spark -add-slave demo_spark_slave1

假如你已经完成了上面的命令操作,现在想为 demo_spark_master 主服务器多增加一个从服务器 demo_spark_slave2 ,则可以通过增加 -skip-master 来避免重复安装主服务器,安装命令如下:

ezhost -C /vagrant/ezhost/data/demo_config.ini demo_spark_master -s bigdata -ba spark -add-slave demo_spark_slave2 -skip-master

Web UI

192.168.33.25:8080

Elasticsearch

Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片, 索引副本机制,restful风格接口,多数据源,自动搜索负载等。

安装关键字

-s bigdata -ba elastic 或者 --server bigdata --bigdata-app elastic

安装配置

  • 安装路径:/usr/share/elasticsearch
  • 配置路径:/etc/default/elasticsearch

测试

curl 127.0.0.1:9200

服务

# 查看状态
sudo systemctl status elasticsearch

# 重启服务
sudo systemctl restart elasticsearch

Logstash

Logstash是一个完全开源的工具,他可以对你的日志进行收集、过滤,并将其存储供以后使用。

安装关键字

-s bigdata -ba logstash 或者 --server bigdata --bigdata-app logstash

安装配置

  • 安装路径:/usr/share/logstash
  • 配置路径:/etc/logstash

服务

# 查看状态
sudo systemctl status logstash

# 重启服务
sudo systemctl restart logstash

Kibana

Kibana也是一个开源和免费的工具,它可以为Logstash和ElasticSearch提供的日志分析友好的Web界面, 可以帮助您汇总、分析和搜索重要数据日志。

安装关键字

-s bigdata -ba kibana 或者 --server bigdata --bigdata-app kibana

安装配置

  • 安装路径:/usr/share/kibana
  • 配置路径:/etc/kibana

测试

访问web页面:http://<IP Address>:5601

服务

# 查看状态
sudo systemctl status kibana

# 重启服务
sudo systemctl restart kibana

API

This is the Read The Docs API documentation, autogenerated from the source code.

ezhost.ServerAbstract

class ezhost.ServerAbstract.ServerAbstract[source]

Server abstract class, all server should extend from it

apache_dir_index

Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell our web server to prefer PHP files, so we’ll make Apache look for an index.php file first.

apache_web_dir

default apache project dir

command_path

Currently command path

django_uwsgi_ini

This ini file is django uwsgi configuration

django_uwsgi_with_nginx

This ini file nginx configuration with uwsgi application

host_keyfile

Host keyfile getter

host_passwd

Host password getter

host_string
Returns:
str: hostname string
host_user

Host user getter

install()[source]

All subclass need implement this function

kafka_install_dir

kafka install dir :return:

mysql_password

Mysql password

nginx_php7_web_config

Nginx web config

nginx_php7_web_wordpress_config

Nginx web wordpress config

nginx_ssl_dir

ssl directory

nginx_web_config

Nginx web config

nginx_web_dir

default apache project dir

nginx_web_ssl_config

Nginx web ssl config

nginx_web_wordpress_config

Nginx web wordpress config

phpinfo

In order to test that our system is configured properly for PHP, we can create a very basic PHP script.

project

project getter

python_env_dir

Python virtualenv dir

server_type

Server type getter

supervisor_config_dir

supervisor control config dir

supervisor_etc_dir

supervisor control config dir

supervisor_uwsgi_ini

supervisor control uwsgi configuration

ezhost.ServerBase

Base class for all server class

class ezhost.ServerBase.DynamicImporter(module_name, class_name, **kwargs)[source]

Dynamic import module and call class

class ezhost.ServerBase.ServerBase(args, configure_obj, **kwargs)[source]

Server command bootstrap class

init_host()[source]

Initial host

install()[source]

install the server

ezhost.ServerCommon

This class is used for create the common command such as mysql install, nginx install and so on..

Author: Zhe Xiao Github: https://github.com/zhexiao/ezhost.git

class ezhost.ServerCommon.ServerCommon[source]

All common function will create inside this class.

add_spark_slave(master, slave, configure)[source]

add spark slave :return:

common_config_nginx_ssl()[source]

Convert nginx server from http to https

common_install_apache2()[source]

Install apache2 web server

common_install_mysql()[source]

Install mysql

common_install_nginx()[source]

Install nginx

common_install_python_env()[source]

Install python virtualenv

common_update_sys()[source]

update system package

elastic_config()[source]

config elasticsearch :return:

elastic_install()[source]

elasticsearch install :return:

generate_ssh(server, args, configure)[source]

异步同时执行SSH生成 generate ssh :param server: :param args: :param configure: :return:

hadoop_install()[source]

install hadoop :return:

java_install()[source]

install java :return:

kafka_config()[source]

kafka config :return:

kafka_install()[source]

kafka download and install :return:

kibana_config()[source]

config kibana :return:

kibana_install()[source]

kibana install :return:

logstash_config()[source]

config logstash :return:

logstash_install()[source]

logstash install :return:

reset_server_env(server_name, configure)[source]

reset server env to server-name :param server_name: :param configure: :return:

spark_config()[source]

config spark :return:

spark_install()[source]

download and install spark :return:

systemctl_autostart(service_name, start_cmd, stop_cmd)[source]

ubuntu 16.04 systemctl service config :param service_name: :param start_cmd: :param stop_cmd: :return:

update_source_list()[source]

update ubuntu 16 source list :return:

ezhost.ServerLamp

This class is aim to install lamp server(Linux, Apache, Mysql and PHP) into your local environment.

Usage:
$ sudo pip install ezhost $ ezhost -s lamp -H 127.0.0.1:2201 -U vagrant -P vagrant

Author: Zhe Xiao Github: https://github.com/zhexiao/ezhost.git