CentOS 相关

2017/01/01 OS

查看 CentOS 版本

第一种方法

yum install lsb –y

lsb_release -a

第二种方法

cat /etc/redhat-release

第三种方法

rpm -qcentos-release

修改主机名

#在CentOS或RHEL中,有三种定义的主机名:
#a、静态的(static)系统在启动时从/etc/hostname自动初始化的主机名
#b、瞬态的(transient) 系统运行时临时分配的主机名
#c、灵活的(pretty)允许使用自由形式(包括特殊/空白字符)的主机名,以展示给终端用户
#查看主机设置  centos7 有命令 hostnamectl
 hostnamectl status [--static|--transient|--pretty]
 #修改主机名  会自动修改 /etc/hostname 但不会自动修改 /etc/hosts 需要手动修改
  sudo hostnamectl set-hostname <host-name>
  # centos6
  vim /etc/sysconfig/network
  vim /etc/hosts

修改源

#备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
#下载新的
#CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
#CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
#生成缓存
yum clean all
yum makecache

安装 nginx

yum install gcc-c++  
yum install pcre pcre-devel  
yum install zlib zlib-devel  
yum install openssl openssl--devel  
#查找 nginx
find -name nginx
#卸载 nginx
yum remove nginx
#下载
wget http://nginx.org/download/nginx-1.8.0.tar.gz
#解压
 tar -zxvf nginx-1.8.0.tar.gz 
 #进入目录
 cd  nginx-1.8.0
 #安装  默认安装在/usr/local/nginx   
 ./configure
make  
make install
#查看安装目录
whereis nginx 

安装 pip

安装 pip 出现错误

[root@wfm ~]# yum -y install pip
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
Setting up Install Process
No package pip available.
Error: Nothing to do

解决方法

需要先安装扩展源EPEL(EPEL 是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目)。

sudo yum -y install epel-release
sudo yum -y install python-pip

安装 python3

  1. 先安装 pyenv

参考这里

#先安装 git
yum install git
#安装
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
#设置环境变量 ~/.bash_profile
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  1. 再安装 python3
#查看版本
pyenv install --list
#安装依赖包
#CentOS/RHEL/Fedora
yum install readline readline-devel readline-static
yum install openssl openssl-devel openssl-static
yum install sqlite-devel
yum install bzip2-devel bzip2-libs
yum install patch
yum install gcc-c++
# ubunto
#sudo apt-get update
#sudo apt-get install make build-essential libssl-dev zlib1g-dev
#sudo apt-get install libbz2-dev libreadline-dev libsqlite3-dev wget curl
#sudo apt-get install llvm libncurses5-dev libncursesw5-dev
#安装
pyenv install 3.5.2

安装 elasticsearch

下载合适的版本, 当前为5.3.0,需要 jdk8,所有下载了1.7.6. 使用 jdk7

https://www.elastic.co/downloads/past-releases/elasticsearch-1-7-6

解压

 unzip elasticsearch-1.7.6.zip

启动:

cd  elasticsearch-1.7.6
./bin/elasticsearch

验证是否启动成功:

curl 'http://localhost:9200/?pretty'

如果只有本地可以访问,尝试修改配置文件 elasticsearch.yml

network.host: 0.0.0.0

关闭

curl -XPOST 'http://localhost:9200/_shutdown'

安装 head 插件:

./bin/plugin -install mobz/elasticsearch-head

使用:

http://172.16.4.37:9200/_plugin/head/

创建索引和数据

curl -XPUT 'http://localhost:9200/megacorp/employee/1' -d  '
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}'

查询

curl -XGET 'http://localhost:9200/megacorp/employee/1'

返回

{
  "_index" :   "megacorp",
  "_type" :    "employee",
  "_id" :      "1",
  "_version" : 1,
  "found" :    true,
  "_source" :  {
      "first_name" :  "John",
      "last_name" :   "Smith",
      "age" :         25,
      "about" :       "I love to go rock climbing",
      "interests":  [ "sports", "music" ]
  }
}

搜索

curl -XGET 'http://localhost:9200/megacorp/employee/_search'

返回

{
   "took":      6,
   "timed_out": false,
   "_shards": { ... },
   "hits": {
      "total":      3,
      "max_score":  1,
      "hits": [
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "3",
            "_score":         1,
            "_source": {
               "first_name":  "Douglas",
               "last_name":   "Fir",
               "age":         35,
               "about":       "I like to build cabinets",
               "interests": [ "forestry" ]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "1",
            "_score":         1,
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "2",
            "_score":         1,
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}

简单查询

curl -XGET 'http://localhost:9200/megacorp/employee/_search?q=last_name:Smith'

DSL查询

curl -XGET 'http://localhost:9200//megacorp/employee/_search' -d '
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}'

Search

    Table of Contents