Linux服务器常用软件部署搭建

Nginx在Linux下的安装

  • ubuntu环境下的安装配置

  • Nginx的基本安装

#1.Nginx的下载安装
  sudo apt-get update
  apt-get install nginx
#2.Nginx的配置文件路径
  sudo vi /etc/nginx/nginx.conf
#3.Nginx的启动和停止
  sudo service nginx start
  sudo service nginx stop

ubuntu中安装Nginx相对来说比较简单,默认端口是80,在浏览器中输入ip:80即可访问

安装Nginx
   ./configure 

   make && make install

默认安装目录:
   /usr/local/nginx

配置Nginx为系统服务,以方便管理
  1、在/etc/rc.d/init.d/目录中建立文本文件nginx
  2、在文件中粘贴下面的内容:

#!/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/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:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   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
}
 
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

 

3、修改nginx文件的执行权限
	chmod +x nginx
4、添加该文件到系统服务中去
	chkconfig --add nginx
	查看是否添加成功
	chkconfig --list nginx

启动,停止,重新装载
service nginx start|stop
  • Nginx配置vhost

所谓 vhost 就是把多个应用映射到一个ip地址上,达到多对一的效果

#1.创建nginx虚拟主机配置文件 
  sudo vi /etc/nginx/sites-available/mydefault.vhost 
#mydefault.vhost文件内容如下:
#-----------------------------mydefault.vhost--------------------------------------#
#用户端api接口转发
server {
  listen       80;
  server_name  192.168.0.65;
  index index.html;
  
  location /uc {
    index index.html;
    client_max_body_size    5m;
    proxy_pass http://127.0.0.1:6001;
      proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
  location /otc {
    index index.html;
    client_max_body_size    5m;
    proxy_pass http://127.0.0.1:6002;
      proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
  location /exchange {
    index index.html;
    client_max_body_size    5m;
    proxy_pass http://127.0.0.1:6003;
      proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
  location /market {
    index index.html;
    client_max_body_size    5m;
    proxy_pass http://127.0.0.1:6004;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
  }
  location /chat {
    index index.html;
    client_max_body_size    5m;
    proxy_pass http://127.0.0.1:6007;
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
  }
}
#后台api接口转发
server {
  listen       80;
  server_name  api.admin.domain.com;
  index index.html;
  location / {
    index index.html;
    client_max_body_size    5m;
    proxy_pass http://127.0.0.1:6010;
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
  }
}
#---------------------------mydefault.vhost----------------------------------#
#2.建立一个mydefault.vhost文件的链接
#(因为nginx的主配置文件nginx.conf会自动应用sites-enabled中的配置文件,所以我们需要在sites-enabled中建立一个mydefault.vhost文件的链接)
  cd /etc/nginx/sites-enabled
  sudo ln -s /etc/nginx/sites-available/mydefault.vhost mydefault.vhost
#3.重新加载nginx使配置生效 
  sudo /etc/init.d/nginx reload
  • centos环境下的安装配置

centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库

#1.使用yum安装nginx需要包括Nginx的库,安装Nginx的库
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#2.rpm包的安装:
#2.1.安装一个包
  rpm -ivh
#2.2.升级一个包,没安装过的不能使用升级命令
  rpm -Uvh
#2.3.移走一个包
  rpm -e
#3.安装准备依赖lib库
  yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
#4.使用下面命令安装nginx
  yum install nginx
#5.启动/重启/停止Nginx
  service nginx start 或 systemctl start nginx.service
  service nginx restart
  /etc/init.d/nginx stop
  /etc/init.d/nginx start

kafka在Linux下的安装

  • ubuntu 环境下的安装配置
  • Kafka 的基本环境的安装
##Install and Start Apache Kafka Server : 0.0.0.0:9092
#0: update and upgrade
  sudo apt-get update -y
#1: home dir
  cd /home/imex
#2: find new package @ www.apache.org
  http://mirror.bit.edu.cn/apache/kafka/1.0.1/kafka_2.11-1.0.1.tgz
#3: download
  wget http://mirror.bit.edu.cn/apache/kafka/1.0.1/kafka_2.11-1.0.1.tgz
#4: mkdir
  sudo mkdir /opt/Kafka
  cd /opt/Kafka
#5: install
    sudo tar -xvf /home/imex/kafka_2.11-1.0.1.tgz -C /opt/Kafka/
#6: start
  sudo  /opt/Kafka/kafka_2.11-1.0.1/bin/kafka-server-start.sh         
  /opt/Kafka/kafka_2.11-1.0.1/config/server.properties
#7: to start the Kafka server as a background process:
  sudo nohup /opt/Kafka/kafka_2.11-1.0.1/bin/kafka-server-start.sh /opt/Kafka/kafka_2.11-1.0.1/config/server.properties /tmp/kafka.log 2>&1 &
#check 
   netstat -ant | grep :9092
#8: Testing Kafka Server
#-- To test Kafka, create a sample topic with name "testing" in Apache Kafka using the following command:
  sudo /opt/Kafka/kafka_2.11-1.0.1/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1  --partitions 1 --topic testing       
#-- Now, ask Zookeeper to list available topics on Apache Kafka by running the following command:
  sudo /opt/Kafka/kafka_2.11-1.0.1/bin/kafka-topics.sh --list --zookeeper localhost:2181    
#--Now, publish a sample messages to Apache Kafka topic called testing by using the following producer command:
  sudo /opt/Kafka/kafka_2.11-1.0.1/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic testing
#--Now, use consumer command to check for messages on Apache Kafka Topic called testing by running the following command:
  sudo /opt/Kafka/kafka_2.11-1.0.1/bin/kafka-console-consumer.sh --    zookeeper localhost:2181 --topic testing --from-beginning
  • Kafka的服务配置
#1.创建服务文件
  cd /etc/systemd/system/
  sudo vi kafka.service
#2.填充内容
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Type=forking
ExecStart= /opt/Kafka/kafka_2.12-1.1.0/bin/kafka-server-start.sh /opt/Kafka/kafka_2.12-1.1.0/config/server.properties
ExecStop=/opt/Kafka/kafka_2.12-1.1.0/bin/kafka-server-stop.sh
Restart=alwaiys
PrivateTmp=true

[Install]
WantedBy=multi-user.target
#3.启动、停止命令
  sudo systemctl start kafka.service
  sudo systemctl stop kafka.service
  • Kafka自动关闭问题

Kafka在启动一段时间后,如果出现服务自动关闭情况,可在启动kafka的时使用守护进程模式启动,即在原启动命令中加 -daemon

kafka-server-start.sh  -daemon  config/server.properties  &

mysql在Linux下的安装

  • ubuntu环境下的安装配置

mysql的基本环境的安装

#1.首先执行下面三条命令:
  sudo apt-get install mysql-server
  sudo apt isntall mysql-client
  sudo apt install libmysqlclient-dev
#2.安装成功后可以通过下面的命令测试是否安装成功:
  sudo netstat -tap | grep mysql
#3.现在设置mysql允许远程访问,首先编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf:
  sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
  #注释掉bind-address = 127.0.0.1:
#4.保存退出,然后进入mysql服务,执行授权命令:
  grant all on *.* to root@'%' identified by '你的密码' with grant option;
  flush privileges;
  #然后执行quit命令退出mysql服务,执行如下命令重启mysql:
  service mysql restart

mysql的基本环境的卸载

#1. 删除mysql
  a. sudo apt-get autoremove --purge mysql-server-5.0
  b. sudo apt-get remove mysql-server
  c. sudo apt-get autoremove mysql-server
  d. sudo apt-get remove mysql-common (非常重要)
#2. 清理残留数据
  dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P

zookeeper在Linux下的安装

  • ubuntu环境下的安装配置

zookeeper的基本环境的安装

# 1.下载zookeeper
    wget http://apache.osuosl.org/zookeeper/stable/zookeeper-3.4.12.tar.gz
# 2.解压到相关目录
   tar -xf zookeeper-3.4.12.tar.gz
# 3.将zookeeper-3.4.3/conf目录下的zoo_sample.cfg文件拷贝一份,命名为为“zoo.cfg”
# 4.修改zoo.cfg配置文件
    dataDir=/home/gradven/zookeeper-3.4.6/data # 数据存放位置,可根据需要修改
    clientPort=2181 # 服务监听端口,可根据需要修改。
# 5.启动服务
   bin/zkServer.sh start
# 6.启动客户端测试
  bin/zkCli.sh -server 127.0.0.1:2181
#  显示根目录下、文件:
  ls /
# 创建文件,并设置初始内容:
  [zk: 127.0.0.1:2181(CONNECTED) 1] create /treeroot hello
  Created /treeroot
  [zk: 127.0.0.1:2181(CONNECTED) 2] get /treeroot
  hello
  cZxid = 0x2
  ctime = Tue Oct 20 00:30:34 CST 2015
  mZxid = 0x2
  mtime = Tue Oct 20 00:30:34 CST 2015
  pZxid = 0x2
  cversion = 0
  dataVersion = 0
  aclVersion = 0
  ephemeralOwner = 0x0
  dataLength = 5
  numChildren = 0
  [zk: 127.0.0.1:2181(CONNECTED) 3] delete /treeroot
  [zk: 127.0.0.1:2181(CONNECTED) 4]
# 退出客户端:
  quit
# 7.查看zookeeper的状态
   ./zkServer.sh status ./zkServer.sh status
# 8.重启zookeeper 命令
   ./zkServer.sh status ./zkServer.sh restart

zookeeper的服务配置

# 1.设置开机自启动
  在/etc/rc.local的末尾添加一行以绝对路径启动脚本的行;
  /usr/local/sbin/zookeeper/zookeeper-3.4.12/bin/zkServer.sh start
# 2.zookeeper服务配置

redis在Linux下的安装

通过rz命令将本地的tar.gz包上传到linux服务器上

Linux服务器常用软件部署搭建_第1张图片

tar -zxvf 命令解压文件

进入到redis-3.0.2文件夹下执行make编译命令

Linux服务器常用软件部署搭建_第2张图片

看到下面这样的结果说明一切正常

Linux服务器常用软件部署搭建_第3张图片

执行 make install命令进行安装,我个人习惯将软件都安装到一个指定的目录下,这次安装的目录是/home/server/redis 安装目录可以提前建好

Linux服务器常用软件部署搭建_第4张图片

安装完成结果跟上图一样说明安装一切正常

可以到redis目录下看会有个bin的目录

将redis-3.0.2目录下的redis.conf文件复制到安装完成的目录redis目录下

到这一步就可以启动redis试试了  ./bin/redis-server redis.conf

Linux服务器常用软件部署搭建_第5张图片

  • redis配置

这里修改配置文件redis.conf 主要修改俩个地方.>> vim redis.conf

1.将daemonize后面的no改为yes,意为后台运行的意思

2.将requirepass前面的#号去掉,开启密码验证,然后后面是我设置的密码

配置文件修好了之后 开始 redis 配置到系统服务了

   回到解压完后的文件夹下redis-3.0.2/utils/下,将redis_init_script复制到/etc/init.d下并改名为redis

然后修改这个redis文件:将代码修改为:

#!/bin/sh
# chkconfig: 2345 80 90
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
REDISPASSWORD=wwwaaa

EXEC=/home/server/redis/bin/redis-server
CLIEXEC=/home/server/redis/bin/redis-cli

PIDFILE=/var/run/redis.pid
CONF="/home/server/redis/redis.conf"

#echo $1

case "$1" in
	cli)
		
                echo "Cli Redis server..."
               $CLIEXEC -p 6379 -a wwwaaa
       
        ;;
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF &
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
				
				echo $PID 
				
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT -a $REDISPASSWORD shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

  安装路径不一样的需要修改下,密码也一样,然后保存.

将redis添加到系统服务:chkconfig redis

然后我们就可以通过service来启动/停止redis了

启动redis服务:(启动及停止命令)

Linux服务器常用软件部署搭建_第6张图片

 

你可能感兴趣的:(Linux)