LNMP 代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
实践centos6.5编译安装 LNMP生产环境 架构 web生产环境 使用 xcache 优化php
最小化安装centos6.5
建立一个软件包目录存放
mkdir -p /usr/local/src/
清理已经安装包
rpm -e httpd
rpm -e mysql
rpm -e php
yum -y remove httpd
yum -y remove mysql
yum -y remove php
搜索apache包
rpm -qa http*
强制卸载apache包
rpm -e --nodeps 查询出来的文件名
检查是否卸载干净
rpm -qa|grep http*
selinux可能会致使编译安装失败,我们先禁用它。永久禁用,需要重启生效
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
临时禁用,不需要重启 setenforce 0
yum -y install make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap lsof
按照标准需要给mysql创建所属用户和用户组
创建群组
groupadd mysql
创建一个用户,不允许登陆和不创主目录
useradd -s /sbin/nologin -g mysql -M mysql
检查创建用户
tail -1 /etc/passwd
centos最小化安装后,会有mysql的库因此先卸载!
检查安装与否
rpm -qa|grep mysql
强制卸载
rpm -e mysql-libs-5.1.73-3.el6_5.x86_64 --nodeps
MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具。 因此,我们首先要在系统中源码编译安装cmake工具。
wget https://cmake.org/files/v3.5/cmake-3.5.1.tar.gz
tar zxvf cmake-3.5.1.tar.gz
cd cmake-3.5.1
./configure
make && make install
使用cmake来编译安装mysql5.6.17
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz
tar zxvf mysql-5.6.17.tar.gz
cd mysql-5.6.17
cmake \
-DCMAKE_INSTALL_PREFIX=/Data/apps/mysql \ -DMYSQL_DATADIR=/Data/apps/mysql/data \ -DSYSCONFDIR=/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DMYSQL_USER=mysql \ -DWITH_DEBUG=0 \ -DWITH_SSL=system make && make install
修改/usr/local/mysql权限
chmod +w /Data/apps/mysql
chown -R mysql:mysql /Data/apps/mysql
关于my.cnf配置文件:
在启动MySQL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到则会搜索”$basedir/my.cnf” 就是安装目录下 /usr/local/mysql/my.cnf,这是新版MySQL的配置文件的默认位置! 注意:在CentOS 6.x版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字。 如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。 由于我们已经卸载了最小安装完成后的mysq库所以,就没必要操作了。
进入support-filecd support-files/有my.cnf请备份
mv /etc/my.cnf /etc/my.cnf.bak
如果愿意也可以复制配置文件到etc下
cp my-default.cnf /etc/my.cnf
执行初始化配置脚本,创建系统自带的数据库和表,注意配置文件的路径
/Data/apps/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/Data/apps/mysql --datadir=/Data/apps/mysql/data --user=mysql
拷贝mysql安装目录下support-files服务脚本到init.d目录
拷贝脚本
cp support-files/mysql.server /etc/init.d/mysqld
赋予权限
chmod +x /etc/init.d/mysqld
设置开机启动
chkconfig mysqld on
启动MySQL
service mysqld start
或者
/etc/init.d/mysql start
MySQL5.6.x启动成功后,root默认没有密码,我们需要设置root密码。 设置之前,我们需要先设置PATH,要不,不能直接调用mysql
修改/etc/profile文件
vi /etc/profile
在文件末尾添加
PATH=/Data/apps/mysql/bin:$PATH
export PATH
让配置立即生效
source /etc/profile
登陆测试,默认是没有密码,直接回车就可进入
mysql -uroot -p
设置mysql密码
/usr/local/mysql/bin/mysqladmin -uroot -p password '你的密码'
登陆进命令行模式
mysql -uroot -p
查看用户
select user,host from mysql.user;
删除不必要的用户
drop user ""@localhost;
drop user ""@c65mini.localdomain;
drop user [email protected];
drop user root@'::1';
赋予账号远程访问的权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY 'your pass' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'your pass' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'c65mini.localdomain' IDENTIFIED BY 'your pass' WITH GRANT OPTION;
关于删除MySQL的默认root用户参考:http://blog.chinaunix.net/uid-16844903-id-3377690.html
其它一些信息查询: 检查mysql版本
mysql -uroot -p"密码" -e "select version();"
验证mysql安装路径
ls -ld /Data/apps/mysql/
安装依赖关系
libiconv库为需要做转换的应用提供了一个iconv()的函数,以实现一个字符编码到另一个字符编码的转换。 错误提示:configure: error: Please reinstall the iconv library.
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make && make install
cd ..
libmcrypt是加密算法扩展库。 错误提示:configure: error: Cannot find imap library (libc-client.a). Please check your c-client installation.
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
tar zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make && make install
cd ..
Mhash是基于离散数学原理的不可逆向的php加密方式扩展库,其在默认情况下不开启。 mhash的可以用于创建校验数值,消息摘要,消息认证码,以及无需原文的关键信息保存 错误提示:configure: error: “You need at least libmhash 0.8.15 to compile this program. http://mhash.sf.net/”
wget http://downloads.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz?r=&ts=1459532667&use_mirror=nbtelecom
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make && make install
cd ..
mcrypt 是 php 里面重要的加密支持扩展库,Mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原。
wget http://iweb.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure
make && make install
cd ..
编译mcrypt可能会报错:configure: error: * libmcrypt was not found
vi /etc/ld.so.conf
最后一行添加
/usr/local/lib/
载入
ldconfig
编译mcrypt可能会报错:/bin/rm: cannot remove `libtoolT’: No such file or directory
修改 configure 文件,把RM=’ RM′改为RM=′ RM -f’ 这里的$RM后面一定有一个空格。 如果后面没有空格,直接连接减号,就依然会报错。
正式开始编译php!
wget http://mirrors.sohu.com/php/php-5.6.20.tar.gz
tar zxvf php-5.6.20.tar.gz
cd php-5.6.20
./configure --prefix=/Data/apps/php --with-config-file-path=/Data/apps/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
make && make install
修改fpm配置php-fpm.conf.default文件名称
mv /Data/apps/php/etc/php-fpm.conf.default /Data/apps/php/etc/php-fpm.conf
复制php.ini配置文件
cp php.ini-production /Data/apps/php/etc/php.ini
复制php-fpm启动脚本到init.d
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
赋予执行权限
chmod +x /etc/init.d/php-fpm
添加为启动项
chkconfig --add php-fpm
设置开机启动
chkconfig php-fpm on
按照标准,给php-fpm创建一个指定的用户和组
创建群组
groupadd www
创建一个用户,不允许登陆和不创主目录
useradd -s /sbin/nologin -g www -M www
立即启动php-fpm
service php-fpm start
/etc/init.d/php-fpm start
回到/usr/local/src/目录
nginx所需的依赖关系,一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。如果系统已经yum 安装了这些库也没关系,无需卸载。直接编译安装最新的就可以了。为了一次性完成编译,先准备编译下面的依赖关系!
1.安装PCRE库
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
tar -zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure
make && make install
2.安装zlib库
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install
准备工作完成,现在开始安装nginx!,这里添加了前面准备的库关系,注意路径!
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure \
--user=www \ --group=www \ --prefix=/Data/apps/nginx \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre=/usr/local/src/pcre-8.38 \ --with-zlib=/usr/local/src/zlib-1.2.8 \ make && make install
cd ..
使用xcache优化php性能。
wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
tar zxvf xcache-3.2.0.tar.gz
cd xcache-3.2.0
/Data/apps/php/bin/phpize
./configure --enable-xcache --with-php-config=/Data/apps/php/bin/php-config
make && make install
cat >>/Data/apps/php/etc/php.ini<<EOF
[xcache-common]
;注意路径
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20121212/xcache.so
[xcache.admin]
xcache.admin.enable_auth = on
xcache.admin.user = "admin"
xcache.admin.pass = "e10adc3949ba59abbe56e057f20f883e"
;运行: echo -n "password" |md5sum |awk '{print $1}' 计算出MD5加密过的密码
;替换xcache.admin.pass=的值
[xcache]
xcache.shm_scheme = "mmap"
xcache.size = 64M
xcache.count = 1
xcache.slots = 8K
xcache.ttl = 3600
xcache.gc_interval = 60
xcache.var_size = 16M
xcache.var_count = 1
xcache.var_slots = 8K
xcache.var_ttl = 3600
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.readonly_protection = Off
xcache.mmap_path = "/dev/zero"
xcache.coredump_directory = "/tmp/phpcore"
xcache.coredump_type = 0
xcache.disable_on_crash = Off
xcache.experimental = Off
xcache.cacher = On
xcache.stat = On
xcache.optimizer = Off
[xcache.coverager]
xcache.coverager = Off
xcache.coverager_autostart = On
xcache.coveragedump_directory = "/tmp/pcov"
EOF
nginx 重启,启动,载入脚本
vi /etc/init.d/nginx
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults ', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx '
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/Data/apps/nginx/sbin/$NAME
CONFIGFILE=/Data/apps/nginx/conf/$NAME.conf
PIDFILE=/Data/apps/nginx/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() {
kill -INT `cat $PIDFILE` || echo -n "nginx not running"
}
do_reload() {
kill -HUP `cat $PIDFILE` || 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 +x /etc/init.d/nginx
经过优化的nginx.cnf配置文件
user www;
worker_processes 4;
error_log logs/error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 5120;
events {
use epoll;
worker_connections 5120;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
proxy_ignore_client_abort on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
log_format access ' $http_x_forwarded_for- $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
include vhost/*.conf;
}
php-fpm优化,注意一个fpm进程大约20M,我这个机器是小内存的云主机,因此开启2个就可以。大内存的话,根据情况换算。
vi php-fpm.conf
pm = dynamic
pm.max_children = 20
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 100