Linux常用命令(二)
服务器大本营——技术文章内容集合站发车啦!
11.4 Linux下使用FastDFS
相关的安装包我打包到云盘上了,链接
提取码:cwff
单节点FastDFS
整个安装过程非常复杂,很容易出错,建议进行多次备份。
我们这里不打算安装多台虚拟机,因此会把tracker和storage都安装在一起。
11.4.1 安装gcc
GCC用来对C语言代码进行编译运行,使用yum命令安装:
yum -y install gcc
后面会用到解压命令(unzip),所以这里可以用yum把unzip 也装一下
yum install -y unzip zip
11.4.2 安装libevent
yum -y install libevent
11.4.3 安装libfastcommon-master
解压刚刚上传的libfastcommon-master.zip
unzip libfastcommon-master.zip
进入解压完成的目录
cd libfastcommon-master
编译并且安装:
./make.sh
./make.sh install
11.4.4 安装fastdfs
tar -zxvf FastDFS_v5.08.tar.gz
cd FastDFS
./make.sh
./make.sh install
如果安装成功,会看到/etc/init.d/下看到提供的脚本文件:
ll /etc/init.d/ | grep fdfs
tarcker.conf.sample 是tracker的配置文件模板
storage.conf.sample 是storage的配置文件模板
client.conf.sample 是客户端的配置文件模板
11.4.5 配置并启动tracker服务
1)首先将模板文件复制
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
2)修改复制后的配置文件:
vim /etc/fdfs/tracker.conf
#修改的内容如下:
base_path=/项目名/tracker # 存储日志和数据的根目录
3)新建目录:
mkdir -p /项目名/tracker
注意:关闭防火墙:
chkconfig iptables off
4)启动和停止
service fdfs_trackerd start # 启动fdfs_trackerd服务,停止用stop
检查FastDFS Tracker Server是否启动成功:
ps -ef | grep fdfs_trackerd
设置tracker服务开机启动:
chkconfig fdfs_trackerd on
11.4.6 配置并启动storage服务
1)首先将模板文件复制
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
2)修改复制后的配置文件:
vim /etc/fdfs/storage.conf
#修改的内容如下:
base_path=/项目名/storage # 数据和日志文件存储根目录
store_path0=/项目名/storage # 第一个存储目录
tracker_server=192.168.56.101:22122 # tracker服务器IP和端口
3)新建目录:
mkdir -p /项目名/storage
注意关闭防火墙: chkconfig iptables off
4)启动和停止
service fdfs_storaged start # 启动fdfs_storaged服务,停止用stop
设置storage服务开机启动:
chkconfig fdfs_storaged on
ps -ef | grep fdfs
11.5 安装fastdfs-nginx-module
11.5.1 解压
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
11.5.2 修改config
1)进入src目录
cd fastdfs-nginx-module/src/
2)编辑config
vim config
使用以下底行命令:
:%s+/usr/local/+/usr/+g
将所有的/usr/local替换为 /usr,这个才是正确的目录:
11.5.3 配置nginx与FastDFS关联配置文件
复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录, 并修改
cp /usr/local/项目名/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vi /etc/fdfs/mod_fastdfs.conf
修改以下配置:
connect_timeout=10 # 客户端访问文件连接超时时长(单位:秒)
tracker_server=192.168.56.101:22122 # tracker服务IP和端口
url_have_group_name=true # 访问链接前缀加上组名
store_path0=/leyou/storage # 文件存储路径
复制 FastDFS 的部分配置文件到/etc/fdfs 目录
cd /usr/local/项目名/FastDFS/conf/
cp http.conf mime.types /etc/fdfs/
11.6 安装Nginx的插件
11.6.1 如果没有安装过nginx
1、安装nginx的依赖库
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2、解压安装包
tar -zxvf nginx-1.10.0.tar.gz
3、配置nginx安装包,并指定fastdfs-nginx-model
cd nginx-1.10.0
./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/leyou/fastdfs-nginx-module/src
注意:在执行./configure配置nginx参数的时候,需要将fastdfs-nginx-moudle源码作为模块编译进去。
4、编译并安装
make && make install
11.6.2 如果已经安装过nginx
1、 进入nginx目录:
cd /usr/local/项目名/nginx-1.10.0/
2、配置FastDFS 模块
./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/项目名/fastdfs-nginx-module/src
注意:这次配置时,要添加fastdfs-nginx-moudle模块
3、编译,注意,这次不要安装(install)
Make
4、替换nginx二进制文件:
备份:
mv /usr/bin/nginx /usr/bin/nginx-bak
用新编译的nginx启动文件替代原来的:
cp objs/nginx /usr/bin/
11.6.3 启动nginx
配置nginx整合fastdfs-module模块
我们需要修改nginx配置文件,在/opt/nginx/config/nginx.conf文件中:
vim /opt/nginx/conf/nginx.conf
将文件中,原来的server 80{ …} 部分代码替换为如下代码:
server {
listen 80;
server_name image.项目名.com;
# 监听域名中带有group的,交给FastDFS模块处理
location ~/group([0-9])/ {
ngx_fastdfs_module;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
启动nginx:
nginx # 启动nginx
nginx -s stop # 停止nginx
nginx -s reload # 重新载入配置文件
#可通过ps -ef | grep nginx查看nginx是否已启动成功
11.6.4 设置nginx开机启动
创建一个开机启动的脚本:
vim /etc/init.d/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="/usr/bin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
}
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 777 /etc/init.d/nginx
添加到服务列表
chkconfig --add /etc/init.d/nginx
设置开机启动
chkconfig nginx on
11.7 安装Elasticsearch
需要虚拟机JDK1.8及以上
11.7.1 新建一个用户leyou
出于安全考虑,elasticsearch默认不允许以root账号运行。
创建用户:
useradd leyou
设置密码:
passwd leyou
切换用户:
su - leyou
11.7.2 上传安装包,并解压
我们将安装包上传到:/home/leyou目录
解压缩:
tar -zxvf elasticsearch-6.2.4.tar.gz
我们把目录重命名:
mv elasticsearch-6.3.0/ elasticsearch
进入,查看目录结构:
11.7.3 修改配置
我们进入config目录:cd config
需要修改的配置文件有两个:
1、jvm.options
Elasticsearch基于Lucene的,而Lucene底层是java实现,因此我们需要配置jvm参数。
编辑jvm.options:
vim jvm.options
默认配置如下:
-Xms1g
-Xmx1g
内存占用太多了,我们调小一些:
-Xms512m
-Xmx512m
2、elasticsearch.yml
vim elasticsearch.yml
修改数据和日志目录:
path.data: /home/leyou/elasticsearch/data # 数据目录位置
path.logs: /home/leyou/elasticsearch/logs # 日志目录位置
我们把data和logs目录修改指向了elasticsearch的安装目录。但是这两个目录并不存在,因此我们需要创建出来。
进入elasticsearch的根目录,然后创建:
mkdir data
mkdir logs
修改绑定的ip:
network.host: 0.0.0.0 # 绑定到0.0.0.0,允许任何ip来访问
默认只允许本机访问,修改为0.0.0.0后则可以远程访问
11.7.4 运行
进入elasticsearch/bin目录,可以看到下面的执行文件:
然后输入命令:
./elasticsearch
或者后台运行:
./elasticsearch -d
11.7.5 错误1:内核过低
修改elasticsearch.yml文件,在最下面添加如下配置: 然后重启
bootstrap.system_call_filter: false
11.7.6 错误2:文件权限不足
我们用的是leyou用户,而不是root,所以文件权限不足。
首先用root用户登录。直接输入exit命令
然后修改配置文件:
vim /etc/security/limits.conf
添加下面的内容:
-
soft nofile 65536
-
hard nofile 131072
-
soft nproc 4096
-
hard nproc 4096
11.7.7 错误3:线程数不够
[1]: max number of threads [1024] for user [leyou] is too low, increase to at least [4096]
继续修改配置:
vim /etc/security/limits.d/90-nproc.conf
修改下面的内容:
- soft nproc 1024
改为 - soft nproc 4096
11.7.8 错误4:进程虚拟内存
[3]: max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
vm.max_map_count:限制一个进程可以拥有的VMA(虚拟内存区域)的数量,继续修改配置文件, :
vim /etc/sysctl.conf
添加下面内容:
vm.max_map_count=655360
然后执行命令:
sysctl -p
11.7.9 重启终端窗口
所有错误修改完毕,一定要重启你的 Xshell终端,否则配置无效。
11.8 安装RabbitMQ
cd /usr/local/myapp
mkdir rabbitmq
cd rabbitmq
11.8.1 安装Erlang
1、在线安装
yum install esl-erlang_17.3-1~centos~6_amd64.rpm
yum install esl-erlang-compat-R14B-1.el6.noarch.rpm
2、离线安装
依次执行命令:
1)rpm -ivh esl-erlang-17.3-1.x86_64.rpm --force --nodeps
2)rpm -ivh esl-erlang_17.3-1~centos~6_amd64.rpm --force --nodeps
3)rpm -ivh esl-erlang-compat-R14B-1.el6.noarch.rpm --force --nodeps
11.8.2 安装RabbitMQ
安装
rpm -ivh rabbitmq-server-3.4.1-1.noarch.rpm
11.8.3 设置配置文件
cp /usr/share/doc/rabbitmq-server-3.4.1/rabbitmq.config.example
/etc/rabbitmq/rabbitmq.config
开启用户远程访问
vi /etc/rabbitmq/rabbitmq.config
注意要去掉后面的逗号。
11.8.4 启动、停止
service rabbitmq-server start
service rabbitmq-server stop
service rabbitmq-server restart
11.8.5 开启web界面管理工具
rabbitmq-plugins enable rabbitmq_management
service rabbitmq-server restart
11.8.6 设置开机启动
chkconfig rabbitmq-server on
11.8.7 防火墙开放15672端口
/sbin/iptables -I INPUT -p tcp --dport 15672 -j ACCEPT
/etc/rc.d/init.d/iptables save
11.9 redis安装和配置
11.9.1 安装
解压
tar -xvf redis-4.0.9.tar.gz
编译安装
mv redis-4.0.9 redis
cd redis
make && make install
11.9.2 配置
修改安装目录下的redis.conf文件
vim redis.conf
修改以下配置:
#bind 127.0.0.1 # 将这行代码注释,监听所有的ip地址,外网可以访问
protected-mode no # 把yes改成no,允许外网访问
daemonize yes # 把no改成yes,后台运行
11.9.3 启动或停止
redis提供了服务端命令和客户端命令:
redis-server 服务端命令,可以包含以下参数: start 启动 stop 停止
redis-cli 客户端控制台,包含参数: -h xxx 指定服务端地址,缺省值是127.0.0.1 -p xxx 指定服务端端口,缺省值是6379
11.9.4 设置开机启动
- 输入命令,新建文件
vim /etc/init.d/redis
输入下面内容:
#!/bin/sh
#chkconfig: 2345 90 10
#description: Redis is a persistent key-value database
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/usr/local/leyou/redis/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
Esac
然后保存退出
注意:以下信息需要根据安装目录进行调整:
EXEC=/usr/local/bin/redis-server # 执行脚本的地址
REDIS_CLI=/usr/local/bin/redis-cli # 客户端执行脚本的地址
PIDFILE=/var/run/redis.pid # 进程id文件地址
CONF="/usr/local/src/redis-3.0.2/redis.conf" #配置文件地址
2)设置权限
chmod 755 /etc/init.d/redis
3)启动测试
/etc/init.d/redis start
启动成功会提示如下信息:
Starting Redis server...
Redis is running...
4)设置开机自启动
chkconfig --add /etc/init.d/redis
chkconfig redis on