LNMP+MEM+redis架构部署
架构部署一般的都是LNMP和LAMP两种格式的,我这里说的是LNMP的架构部署,nginx、mysql、php、memcache的编译配置安装, mysql的主从配置、权限的一些问题,后台监控nagio和cacti、zabbix的一些介绍,
#####################################################################################################
在部署架构之前,先安装一下后面所需要的一些必备库,
安装必备库
yum -y groupinstall "Development tools"
yum install -y gcc gcc-c++ make cmake zlib zlib-devel openssh openssl openssl-devel net-snmp pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel xinetd lrzsz dos2unix telnet python-devel mysql-devel libxslt-devel unixODBC-devel sqlite-devel
#####################################################################################################
Mysql的编译安装和一些拓展
1.1 mysql的源码编译安装,版本Mysql 5.6.14
下载地址:http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.14.tar.gz
先检查一下是否有以前安装的mysql,
rpm �Cqa | grep mysql
如果有,就删除
rpm -e mysql //普通删除模式
rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
编译安装,要用cmake来引导安装,所以要先安装cmake
yum -y install cmake
cd /app
groupadd mysql
useradd mysql -g mysql
mkdir -p /usr/local/mysql
mkdir -p /app/mysql/data
mkdir -p /app/mysql/log
tar zxvf mysql-5.6.14.tar.gz
cd mysql-5.6.14
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/app/mysql/data \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306
make
make install
make clean
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /app/mysql/data
chown -R mysql:mysql /app/mysql/log
cd /usr/local/mysql
scripts/mysql_install_db --user=mysql --datadir=/app/mysql/data
/usr/local/mysql/bin/mysqld_safe &
cp support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin
ln -s /usr/local/mysql/bin/mysql /usr/sbin
chkconfig mysql on
service mysql start
1.2 数据库账号权限
mysql给权限:
grant 权限 on 数据库.* 用户名@登录主机 identified by “密码”
例子:grant all on *.* to [email protected] identified by "123456"
刷新:fulsh privileges
1.3、数据库备份mysqldump
mysqldump 导出数据
a. 只导出库结构
mysqldump --opt -d 数据库名 -u 用户名 -p 密码> xx.sql
如: mysqldump --opt -d shop_data -u root -p admin >onlystruct_shop_data.sql
b. 只导出库数据
mysqldump -t 数据库名 -u 用户名-p 密码 > xx.sql
如: mysqldump --t shop_data -u root -p admin > onlydata_shop_data.sql
c. 导出库所有数据和结构
mysqldump 数据库 -u 用户名 -p 密码 > xx.sql
如: mysqldump shop_data -u root -p admin > shop_data.sql
d. 导出特定表
mysqldump 数库 -u 用户名 -p 密码 -B --table 表名 > xx.sql
如: mysqldump shop_data -u root -p admin -B --table user > user.sql
默认字体格式 --default-character-set=utf8
1.4、导入数据sources
source 导入数据
source /home/root/XX.sql 只能用mysql -uroot -padmin 登录后才能使用
如: source /home/root/test.sql
1.5、数据库修改密码mysqladmin
mysql修改密码:
加上新密码:mysqladmin -uroot password新密码
旧密码修改新密码:mysqladmin -uroot -p旧密码 password新密码
##########################################################################
nginx的编译安装
2.1、下载安装包
wget http://nginx.org/download/nginx-1.5.7.tar.gz
2.2 、解决依赖关系
编译安装nginx需要事先需要安装开发包组"Development Tools"和 "Development Libraries"。同时,还需要专门安装pcre-devel包:
yum groupinstall "Development Tools" -y
yum -y install pcre-devel
2.3、编译安装
首先添加用户nginx,实现以之运行nginx服务进程:
groupadd -r nginx
useradd -r -g nginx nginx
tar -xzvf nginx-1.5.7.tar.gz
接着开始编译和安装:
./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
make && make install
cp /app/nginx-1.7.0/conf/nginx.conf /etc/nginx/nginx.conf
说明:如果想使用nginx的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。
2.3、为nginx提供SysV init脚本:
新建文件/etc/rc.d/init.d/nginx,内容如下:
vim /etc/rc.d/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/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
chmod u+x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
service nginx start
#######################################################################################
memcached安装
3.1、下载安装包
下载 memcached-1.4.15.tar.gz 安装包
wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
3.2、安装依赖关系
yum install libevent libevent-devel -y
3.3、编译安装
cd /app/
tar -xvzf memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure
make
make install
memcached -d -m 64 -u root -p 11211 -c 10000 -P /tmp/memcached.pid
---------------------------------------------------------------------
start::
memcached -d -m 64 -u root -p 11211 -c 10000 -P /tmp/memcached.pid
shutdown:
kill `cat /tmp/memcached.pid`
----------------------------------------------------------------------
启动参数解析
d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是64MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了10000,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
####################################################################################
php安装
4.1、下载安装包
wget http://cn2.php.net/get/php-5.4.23.tar.gz/from/this/mirror
4.2、安装依赖关系
yum -y install libmcrypt libmcrypt-devel
编译安装
tar zxvf php-5.4.23.tar.gz
cd php-5.4.23
./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt --with-openssl \
--enable-mbstring --enable-pdo --with-curl --disable-debug --disable-rpath \
--enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir
make
make test
make install
sh /bin/iptables.sh
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
启动
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php-fpm.conf
---------------------------------------------------------------------------------------------
apache+php(yum安装)
yum install -y httpd
yum -y install --enablerepo=remi php
yum -y install --enablerepo=remi php-pdo
yum -y install --enablerepo=remi php-mysql
yum -y install --enablerepo=remi php-pear
yum -y install --enablerepo=remi php-mcrypt
yum -y install --enablerepo=remi php-pear-DB
yum -y install --enablerepo=remi php-mbstring
yum -y install --enablerepo=remi php-devel
yum -y install --enablerepo=remi php-gd
yum -y install --enablerepo=remi zlib-devel
yum -y install --enablerepo=remi pcre-devel
yum --enablerepo=remi install php-memcache
printf "\n" | pecl install apc
printf "\n" | pecl install memcache
pecl install redis
yum -y remove libmcrypt
yum -y install libmcrypt libmcrypt-devel
sh /bin/iptables.sh
####################################################################################
redis的安装
5.1、下载安装包
wgethttp://download.redis.io/releases/redis-2.8.3.tar.gz
5.2、编译安装
tar zxvf redis-2.8.3.tar.gz
cd redis-2.8.3
make
make install
5.3、配置参数:redis.conf
vim /usr/local/redis/etc/redis.conf
---------------------------------------------------------
#!/bin/bash
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
timeout 300
loglevel debug
logfile /usr/local/redis/var/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis/var/
appendonly no
appendfsync always
glueoutputbuf yes
shareobjects no
shareobjectspoolsize 1024
---------------------------------------------------------------------------
cp /usr/local/redis/etc/redis.conf /app/redis-2.8.3/redis.conf
启动redis
cd /app/redis-2.8.3/src/
./redis-server &
---------------------------------------------------------------------------------------------
redis的yum安装
yum -y install --enablerepo=remi redis
echo "1" > /proc/sys/vm/overcommit_memory
redis-server /etc/redis.conf
redis-cli shutdown
#################################################################################################
总上所述的架构部署都是经过在工作中实践得来,还有很多不足,后面会继续修改