环境
- nginx-1.8.0
- mysql-5.6.25
- php-5.6.11
所需的软件包如图所示:
注意:可将这些软件包和mysql配置文件my.cnf(配置已经设定好)以及php配置文件php-fpm.conf(配置已经设定好)一并打包,整合成一个tar包。
注:这次的安装是通过脚本进行安装,通过分发服务器实现,脚本及所需工具以及配置文件都是提前配置好,在安装过程中,配置文件直接放到指定目录下,从而实现快捷高效的配置。(配置中并不是特别详细,有很多地方可以优化,后续进行更改)
scp分发所需文件
这里略去分发脚本:scp -P端口 所需文件 用户名@hosts:放置目录
一般目录单独创建,我这里的目录为/home/tools/
一台服务器上搭建环境
如果多带服务器组成集群,需在配置文件中更改,lnmp注意要点:
各版本的兼容以及php所需依赖库的下载,如果mysql在单独的一台服务器,虽然可以在编译php的时候添加一条参数来获取mysql的库,但不建议。最好将mysql编译进行到make install完成,无需初始化即可。
下面来进行安装~~~~~~开始喽!!!!
部署脚本
if [ ! -d /home/tools ]
then
mkdir /home/tools -p
fi
cd /home/tools
PING=`ping -c 1 www.baidu.com |grep ttl=|wc -l`
if [ "$PING" -eq 0 ]
then
echo "Network does not work"
exit 1
fi
if [ -e /home/tools/pcre-8.30.tar.gz ]
then
tar xf pcre-8.30.tar.gz
else
echo "no file /home/tools/pcre-8.30.tar.gz"
fi
cd pcre-8.30 &&\
./configure
if [ $? -ne 0 ]
then
echo "pcre configure is false"
exit 1
fi
make && make install
if [ $? -ne 0 ]
then
echo "pcre make install is false"
exit 1
fi
cd ..
RPM=`rpm -qa|grep "openssl-"|wc -l`
if [ $RPM -ne 2 ]
then
yum -y install openssl openssl-devel
else
echo "yum install openssl openssl-devel is error"
exit 1
fi
cd /home/tools
if [ ! -e nginx-1.8.0.tar.gz ]
then
echo " nginx-1.8.0.tar.gz is false"
exit 1
fi
tar xf nginx-1.8.0.tar.gz
cd nginx-1.8.0
NGINXUSER=`cat /etc/passwd |grep nginx|wc -l`
if [ "$NGINXUSER" -eq 0 ]
then
useradd nginx -s /sbin/nologin -M
fi
./configure --user=nginx --group=nginx --prefix=/home/nginx-1.8.0 --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
if [ $? -eq 0 ]
then
make
else
echo "nginx configure is error"
exit 1
fi
if [ $? -eq 0 ]
then
make install
else
echo "nginx make is error"
exit 1
fi
if [ $? -ne 0 ]
then
echo "nginx make install is error"
exit 1
fi
cd ..
if [ -d /home/nginx-1.8.0/ ]
then
ln -s /home/nginx-1.8.0 /home/nginx
else
echo "no directory /home/nginx-1.8.0/"
exit 1
fi
if [ -f /etc/ld.so.conf ]
then
echo "/usr/local/lib/" >>/etc/ld.so.conf
else
echo "no file /etc/ld.so.conf"
exit 1
fi
if [ -d /lib64 ]
then
ln -s /usr/local/lib/libpcre.so.1 /lib64
else
echo "no directory /lib64"
exit 1
fi
/home/nginx/sbin/nginx -t
if [ $? -eq 0 ]
then
echo "nginx is ok"
else
echo "nginx is false"
exit 1
fi
/home/nginx/sbin/nginx
chown -R nginx.nginx /home/nginx/*
clear
echo "==================nginx install is all finish=================="
sleep 2
cd /home/tools
echo "=====================starting cmake install======================="
tar xf cmake-3.2.3.tar.gz
cd cmake-3.2.3
./configure
if [ $? -ne 0 ]
then
echo "cmake configure is error"
exit 1
fi
gmake
if [ $? -ne 0 ]
then
echo " cmake gmake is error"
exit 1
fi
gmake install
if [ $? -ne 0 ]
then
echo " cmake gmake install is error"
exit 1
fi
cd ../
PING=`ping -c 1 www.baidu.com |grep ttl=|wc -l`
if [ "$PING" -ne 1 ]
then
echo "Network does not work"
exit 1
fi
echo "=====================starting ncurses-devel install======================="
RPM=`rpm -qa |grep ncurses-devel |wc -l`
if [ $RPM -ne 1 ]
then
yum install ncurses-devel -y
fi
echo "=====================next is install mysql...======================="
cd /home/tools/
if [ -e /home/tools/mysql-5.6.25.tar.gz ]
then
tar zxf mysql-5.6.25.tar.gz
else
echo "no file /home/tools/mysql-5.6.25.tar.gz"
fi
NGINXUSER=`cat /etc/passwd |grep mysql|wc -l`
if [ "$NGINXUSER" -eq 0 ]
then
useradd mysql -s /sbin/nologin -M
fi
cd mysql-5.6.25
cmake . -DCMAKE_INSTALL_PREFIX=/home/mysql-5.6.25 \
-DMYSQL_DATADIR=/home/mysql-5.6.25/data \
-DMYSQL_UNIX_ADDR=/home/mysql-5.6.25/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
if [ $? -ne 0 ]
then
echo "mysql cmake is error"
exit 1
fi
make
if [ $? -ne 0 ]
then
echo "mysql make is error"
exit 1
fi
make install
if [ $? -ne 0 ]
then
echo "mysql make install is error"
exit 1
fi
echo "=====================configuration mysql======================="
if [ -d /home/mysql-5.6.25 ]
then
ln -s /home/mysql-5.6.25/ /home/mysql
else
echo "no directory /home/mysql-5.6.25/"
exit 1
fi
cd /home/mysql
cp /home/tools/my.cnf /etc/my.cnf
chown -R mysql.mysql /home/mysql/
chown -R mysql.mysql /tmp
echo "=====================mysql is INIT======================="
/home/mysql/scripts/mysql_install_db --basedir=/home/mysql --datadir=/home/mysql/data --user=mysql
INIT=`/home/mysql/scripts/mysql_install_db --basedir=/home/mysql --datadir=/home/mysql/data --user=mysql|grep OK|wc -l`
if [ "$INIT" -eq 2 ]
then
echo "=====================mysql INIT is ok====================="
fi
if [ -f /home/mysql/support-files/mysql.server ]
then
cp /home/mysql/support-files/mysql.server /etc/init.d/mysqld
else
echo "no file /home/mysql/support-files/mysql.server"
fi
chmod +x /etc/init.d/mysqld
/etc/init.d/mysqld start
echo 'PATH=/home/mysql/bin/:$PATH' >>/etc/profile
. /etc/profile
chkconfig mysqld on
mysqladmin -u root password '123456'
MYSQLLOGIN=`mysql -uroot -p123456 -e "select version();"|wc -l`
if [ $MYSQLLOGIN -ne 0 ]
then
echo "mysql is installed and mysql login is ok..."
else
echo "mysql don't login..."
fi
clear
echo "Pls input user@localhost and password login!!!!!"
sleep 1
clear
echo "==============other installation package=============="
cd /home/tools/
yum install zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel libtool-ltdl-devel -y
yum install libxslt libxslt-devel -y
YUM=`rpm -qa zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel|wc -l`
if [ $YUM -lt 3 ]
then
echo "yum bag is error,bag is lack..."
exit 1
fi
if [ ! -e libiconv-1.14.tar.gz ]
then
echo "no gz libiconv-1.14.tar.gz"
exit 1
fi
tar zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
if [ $? -ne 0 ]
then
echo "libiconv-1.14 configure is error"
exit 1
fi
make&&make install
if [ $? -ne 0 ]
then
echo "libiconv-1.14 make&&make install is error"
exit 1
fi
cd ../
if [ ! -e libmcrypt-2.5.8.tar.gz ]
then
echo "no gz libmcrypt-2.5.8.tar.gz"
exit 1
fi
tar zxf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
if [ $? -ne 0 ]
then
echo "libmcrypt-2.5.8 configure is error"
exit 1
fi
make&&make install
if [ $? -ne 0 ]
then
echo "libmcrypt-2.5.8 make&&make install is error"
exit 1
fi
sleep 2
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
if [ $? -ne 0 ]
then
echo "libmcrypt-2.5.8/libltdl configure is error"
exit 1
fi
make&&make install
if [ $? -ne 0 ]
then
echo "libmcrypt-2.5.8/libltdl make&&make install is error"
exit 1
fi
cd ../../
if [ ! -e mhash-0.9.9.9.tar.gz ]
then
echo "no gz mhash-0.9.9.9.tar.gz"
exit 1
fi
tar zxf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
if [ $? -ne 0 ]
then
echo "mhash-0.9.9.9 configure is error"
exit 1
fi
make&&make install
if [ $? -ne 0 ]
then
echo "mhash-0.9.9.9 make&&make install is error"
exit 1
fi
sleep 2
cd ../
rm -f /usr/lib64/libmcrypt.*
rm -f /usr/lib64/libmhash*
ln -s /usr/local/lib64/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib64/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib64/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib64/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib64/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib64/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib64/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib64/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib64/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
if [ ! -e mcrypt-2.6.8.tar.gz ]
then
echo "no gz mcrypt-2.6.8.tar.gz"
exit 1
fi
tar zxf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure LD_LIBRARY_PATH=/usr/local/lib
if [ $? -ne 0 ]
then
echo "mcrypt-2.6.8 configure is error"
exit 1
fi
make&&make install
if [ $? -ne 0 ]
then
echo "mcrypt-2.6.8 make&&make install is error"
exit 1
fi
cd ../
clear
echo "==============other installation package is ok=============="
sleep 2
echo "==================starting install php===================="
if [ ! -e php-5.6.11.tar.gz ]
then
echo "no gz php-5.6.11.tar.gz"
exit 1
fi
tar xf php-5.6.11.tar.gz
cd php-5.6.11
echo "/home/mysql-5.6.25/lib/" >>/etc/ld.so.conf
ldconfig
./configure \
--prefix=/home/php-5.6.11 \
--with-mysql=/home/mysql \
--with-iconv-dir=/usr/local/libiconv \
--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-fpm \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp
if [ $? -ne 0 ]
then
echo "php-5.6.11.tar.gz configure is error"
exit 1
fi
make
if [ $? -eq 0 ]
then
make install
else
echo "make is error"
exit 1
fi
ln -s /home/php-5.6.11/ /home/php
cp php.ini-production /home/php/lib/php.ini
cd /home/php/etc/
cp /home/tools/php-fpm.conf ./
CONF=`ls php-fpm.conf|wc -l`
if [ $CONF -ne 1 ]
then
echo "no file /home/php/etc/php-fpm.conf"
exit 1
fi
clear
echo "=====================php install is ok========================"
sleep 1
/home/php/sbin/php-fpm
PHPSTART=`ss -luntp |grep php-fpm|wc -l`
if [ $PHPSTART -eq 0 ]
then
echo "php-fpm is stopped..."
fi
clear
echo "===========lnmp all install is ok============="
echo "==============!!!!happy!!!!================"
echo " * * * * "
echo " * * * * * * "
echo " ***** * * * * "
echo " * * * * * * "
echo " * * * * * "
echo "==============!!!!happy!!!!================"
上述是执行的脚本,如果需要分开各个软件包的操作,请参考:http://www.lmyw.net.cn/?p=883
各配置文件及参数解释
nginx.conf配置
worker_processes 1; #启动进程,通常设置成和cpu的数量相等
#worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 65535; #更改worker进程的最大打开文件数限制
user nginx; #指定用户
events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections 10240; #单个后台worker process进程的最大并发链接数
}
http {
include mime.types; #设定mime类型,类型由mime.type文件定义
default_type application/octet-stream;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件
#对于普通应用,必须设为 on
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off
#以平衡磁盘与网络I/O处理速度,降低系统的uptime
sendfile on;
tcp_nopush on;
keepalive_timeout 60; #连接超时时间
tcp_nodelay on;
client_header_timeout 15;
client_body_timeout 15;
send_timeout 15;
client_max_body_size 10m;
#设定日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server_tokens off;
#######gzip##############
gzip on; #开启gzip压缩
gzip_min_length 1k; #当返回内容大于此值时才会使用gzip进行压缩,以K为单位,当值为0时,所有页面都进行压缩
gzip_buffers 4 32k; #设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间
gzip_http_version 1.1; #用于识别http协议的版本,早期的浏览器不支持gzip压缩,用户会看到乱码,所以为了支持前期版本加了此选项,目前此项基本可以忽略
gzip_comp_level 2; #设置gzip压缩等级,等级越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大
gzip_types text/html text/css text/xml application/javascript; #设置需要压缩的MIME类型,非设置值不进行压缩
gzip_vary off; #隐藏Vary头信息
#########################
include web/*.conf; #配置了一个server标签存放目录,方便管理
}
*.conf配置文件
server {
root /home/www/phpmyadmin;
listen 80;
server_name www.haoruijing.cn xxtao.admin.cn;
location / {
root /home/www/phpmyadmin;
index index.php index.html index.htm;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
#########expires###### 静态资源控制页面缓存时间
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 3650d;
}
location ~ .*\.(js|css)?$
{
expires 30d;
}
######################
access_log /home/log/admin_access.log main;
}
}
php-fpm.conf配置
pid = run/php-fpm.pid #pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启
error_log = /home/log/php-fpm.log #错误日志,默认在安装目录中的var/log/php-fpm.log
log_level = error #错误级别. 可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
rlimit_files = 32768
events.mechanism = epoll
listen.owner = nginx
listen.group = nginx
pm.max_children = 1024 #子进程最大数
pm.start_servers = 16 #启动时的进程数
pm.min_spare_servers = 5 #保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
pm.max_spare_servers = 20 #保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
pm.process_idle_timeout = 15s;
pm.max_requests = 2048 #设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 '0' 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.
slowlog = /home/log/$pool.log.slow #慢请求的记录日志,配合request_slowlog_timeout使用
request_slowlog_timeout = 10 #当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 '0' 表示 'Off'
php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f ***@163.com
总结
以上是对安装及主要配置参数的简要说明,如果想更深入的了解,可以去详细差下,可以根据公司业务需求来设定参数