CentOS 6 环境下,安装Java,MySQL,Tomcat,Memcached,Nginx常用服务器应用。注意 CentOS 7 之后开始使用 systemctl,以下步骤可能会有所不同。

Java

cd /opt
wget http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm?AuthParam=1498015445_1d17be28413fab077914e778e3436adb
rpm -ivh jdk-8u131-linux-x64.rpm
vi /etc/profile

添加环境变量

export JAVA_HOME=/usr/java/jdk1.8.0_131
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

输入命令使生效

source /etc/profile

检查版本输出,看看有没有装好

java -version

MySQL

先检查有没有MySQL遗留

rpm -q mysql-libs
yum remove mysql-libs /** if exists **/

下载安装MySQL 5.6.36

cd /opt
#新版本可以去官网找最新的,不同版本可能安装方式有些不同
wget https://cdn.mysql.com/archives/mysql-5.6/MySQL-server-5.6.36-1.el7.x86_64.rpm
wget https://cdn.mysql.com/archives/mysql-5.6/MySQL-client-5.6.36-1.el7.x86_64.rpm
wget https://cdn.mysql.com/archives/mysql-5.6/MySQL-devel-5.6.36-1.el7.x86_64.rpm
 
rpm -ivh MySQL-server-5.6.36-1.el7.x86_64.rpm
rpm -ivh MySQL-devel-5.6.36-1.el7.x86_64.rpm
rpm -ivh MySQL-client-5.6.36-1.el7.x86_64.rpm

我安装server的时候报错:

error: Failed dependencies:

libnuma.so.1()(64bit) is needed by MySQL-server-5.6.36-1.el7.x86_64

下载依赖(出错就看错误输出,缺哪个依赖就装哪个)

wget http://mirror.centos.org/centos/6/os/x86_64/Packages/numactl-2.0.9-2.el6.x86_64.rpm
rpm -ivh numactl-2.0.9-2.el6.x86_64.rpm

重试安装成功,三个包都装好之后

添加用户组和用户

groupadd mysql
useradd -r -g mysql mysql

修改配置文件位置

cp /usr/share/mysql/my-**default**.cnf /etc/my.cnf

初始化MySQL

/usr/bin/mysql_install_db

我这里报错:FATAL ERROR: please install the following Perl modules before executing ./mysql_install_db:

Data::Dumper

安装Autoconf,问题解决

yum install autoconf

启动服务

service mysql start

我这里报错:Starting MySQL. ERROR! The server quit without updating PID file (/var/lib/mysql/VM_64_157_centos.pid).

原因是没有PID file的权限,给/var/lib的所有者改为mysql用户组mysql用户

chown -R mysql:mysql /**var**/lib/mysql

再次启动服务,成功。

MySQL安装成功后,初始密码会生成在root文件夹里,一个叫mysql_secret的文件,但是我没找到,所以直接修改MySQL的密码

service mysql stop
mysqld_safe --skip-grant-tables &
mysql -uroot -p
/** 直接回车成功进入mysql */
mysql>use mysql;
mysql>update user set password=PASSWORD("123456")where user="root";
mysql>flush privileges;
mysql>quit

Tomcat

cd /opt
wget http://apache.fayea.com/tomcat/tomcat-9/v9.0.0.M22/bin/apache-tomcat-9.0.0.M22.tar.gz
tar zxvf apache-tomcat-9.0.0.M22.tar.gz
mv apache-tomcat-9.0.0.M22 /usr/local/tomcat9

配置service启动

/etc/init.d/下新建文件tomcat,编辑内容为如下脚本,JAVA_HOME/CATALINA_HOME改成自己的

#!/bin/sh
#
# tomcat Startup script for the tomcat Server  
#  
# chkconfig: 2345 64 36  
# processname: tomcat  
  
export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin  
BASEDIR=/usr/local/tomcat9
export JAVA_HOME=/usr/java/jdk1.8.0_131  
PROG=`basename $BASEDIR`  
FILE=/usr/local/tomcat9/bin/startup.sh  
  
SLEEPTIME=1.5  
  
start() {  
        PID=`ps -ef | grep "base=$BASEDIR" | grep -v "grep" | awk '{print $2}'`  
        if [ -n "$PID" ];then  
                echo -ne "\033[31;49;1m [FAILED] \033[39;49;0m"  
                echo "$PROG already running..."  
        else  
                $FILE  
                echo -n "Starting $PROG:"  
                echo -e "\033[32;49;1m [OK] \033[39;49;0m"    
	fi
}  
stop() {  
        PID=`ps -ef | grep "base=$BASEDIR" | grep -v "grep" | awk '{print $2}'`  
        if [ -n "$PID" ];then  
                echo -n "Stopping $PROG:"  
                kill -9 $PID;  
                echo -e "\033[32;49;1m [OK] \033[39;49;0m"                 
        else  
                echo -ne "\033[31;49;1m [FAILED] \033[39;49;0m"  
                echo "$PROG is not running."  
        fi  
}  
  
case "$1" in  
 start)  
        start  
        ;;  
 stop)  
        stop  
        ;;  
 restart)  
        stop  
        sleep $SLEEPTIME  
        start  
        ;;  
 *)  
        echo $"Usage: $PROG {start|stop|restart}"  
        exit 1  
esac  

修改文件权限,并加入服务

chmod a+x tomcat
chkconfig --add tomcat

启动

service tomcat start

Memcached

Memcached依赖libevent

cd /opt
wget https://github-production-release-asset-2e65be.s3.amazonaws.com/1856976/f9ea6922-e66b-11e6-9f5c-722c00daa657?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20170620%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20170620T113303Z&X-Amz-Expires=300&X-Amz-Signature=445dd270632229b61814ddc54900999859eb01a26b2989475d0480cc4ac660fe&X-Amz-SignedHeaders=host&actor_id=20593792&response-content-disposition=attachment%3B%20filename%3Dlibevent-2.1.8-stable.tar.gz&response-content-type=application%2Foctet-stream
tar zxvf libevent-2.1.8-stable.tar.gz
cd libevent-2.1.8-stable
./configure --prefix=/usr/local/libevent
make
make install
cd ..
wget https://www.memcached.org/files/memcached-1.4.37.tar.gz
tar zxvf memcached-1.4.37.tar.gz
cd memcached-1.4.37
./configure --prefix=/usr/local/memcache --with-libevent=/usr/local/libevent
make
make install

安装成功,启动Memcached

/usr/local/memcache/bin/memcached -d -m 100 -u root -l 192.168.1.30 -p 11211 -c 64 -P /tmp/memcached.pid

启动成功

配置service启动

/etc/init.d/下新建文件memcached,编辑内容为如下脚本

#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# Source function library - for other linux
#. /etc/rc.d/init.d/functions
# Source function library - for suse linux
. /lib/lsb/init-functions
PORT=11211
USER=root
MAXCONN=1024
CACHESIZE=128
OPTIONS=""
if [ -f /etc/sysconfig/memcached ];then
    . /etc/sysconfig/memcached
fi
# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
    exit 0
fi
RETVAL=0
prog="memcached"
start () {
    echo -n $"Starting $prog: "
    # insure that /usr/local/memcached has proper permissions
    chown $USER /usr/local/memcached
    /usr/local/memcached/bin/memcached -d -p $PORT -l 192.168.1.38 -u $USER  -m $CACHESIZE -c $MAXCONN -P /tmp/memcached.pid $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
    echo -n $"Stopping $prog: "
    killproc memcached
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
        rm -f /var/lock/subsys/memcached
        rm -f /usr/local/memcached/memcached.pid
    fi
}
restart () {
    stop
    start
}
# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
    stop
    ;;
    status)
    status memcached
    ;;
    restart|reload)
    restart
    ;;
    condrestart)
    [ -f /var/lock/subsys/memcached ] && restart || :
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
    exit 1
esac
exit $?

添加权限

chmod 755 /etc/init.d/memcached
chkconfig --add memcached
chkconfig memcached on
service memcached start

Nginx

cd /opt
wget http://nginx.org/download/nginx-1.13.2.tar.gz
tar zxvf nginx-1.13.2.tar.gz
cd nginx-1.13.2
./congifure --prefix=/usr/local/nginx

报错:./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel依赖

yum install pcre-devel

./configure --prefix=/usr/local/nginx

make

make install

安装成功

配置service启动:/etc/init.d/下新建文件nginx,编辑内容为如下脚本

#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

添加权限

chmod a+x nginx
chkconfig --add nginx
service nginx start