openresty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
性能之高与具体内容就不贴了,官网介绍很详细了

对我而言,我是觉得在nginx上加上这个lua的扩展来处理网关,路由和鉴权甚至加解密都是是非常合理的,这些工作在nginx就处理好,后端类似php程序等只管业务实现和界面展示,整个系统的架构也变得非常清晰。加上openresty 的性能还不是一般高的话就非常有可用性了。

学习的第一步是搭建环境。以下教程是在centos上编译安装openresty,php,redis,mysql的全部过程

编译前的准备工作

//设置时区和NTP
rm -rf /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum install -y ntp
ntpdate -u pool.ntp.org
date

//关闭selinux
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config

//安装编译环境
cp /etc/yum.conf /etc/yum.conf.bak
sed -i 's:exclude=.*:exclude=:g' /etc/yum.conf

yum -y install wget flex patch bison unzip tar vim-minimal gettext gettext-devel  unzip file screen
yum -y install make cmake gcc gcc-c++ gcc-g77
yum -y install curl curl-devel libcurl libcurl-devel
yum -y install kernel-devel gd gd-devel libtool-libs

//一些依赖库
yum -y install libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel
yum -y install libxml2 libxml2-devel libXpm-devel
yum -y install glib2 glib2-devel
yum -y install libevent libevent-devel

yum -y install openssl openssl-devel net-tools
yum -y install psmisc libc-client-devel
yum -y install ca-certificates c-ares-devel libicu-devel
yum -y install ncurses ncurses-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel
yum -y install libxslt libxslt-devel bzip2 bzip2-devel
yum -y install gmp-devel pspell-devel libcap diffutils
yum -y install libtool autoconf zlib zlib-devel

yum -y install readline-devel
yum -y install pcre-devel

还有几个需要编译安装的。这些依赖包很多都是为了添加一些扩展功能

//上传安装libiconv-1.14.tar.gz 和相关补丁libiconv-glibc-2.16.patch
tar xzvf libiconv-1.14.tar.gz
cd libiconv-1.14
patch -p0 < ../patch/libiconv-glibc-2.16.patch
./configure --enable-static
make && make install
cd ../

//上传安装libmcrypt-2.5.8.tar.gz
tar xzvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make && make install
ln -sf /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -sf /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ldconfig
cd ../
cd ../

//上传安装mhash-0.9.9.9.tar.gz
tar xzvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make && make install
ln -sf /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -sf /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -sf /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -sf /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -sf /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ldconfig
cd ../

//上传安装mcrypt-2.6.8.tar.gz
tar xzvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure
make && make install
cd ../

---下面这几个centos7是不需要了
//上传安装autoconf-2.13.tar.gz
tar xzvf autoconf-2.13.tar.gz
cd autoconf-2.13
./configure --prefix=/usr/local/autoconf-2.13
make && make install
cd ../

//上传安装freetype-2.4.12.tar.gz
tar xzvf freetype-2.4.12.tar.gz
cd freetype-2.4.12
./configure --prefix=/usr/local/freetype
make && make install
cat > /etc/ld.so.conf.d/freetype.conf<<EOF
/usr/local/freetype/lib
EOF
ldconfig
ln -sf /usr/local/freetype/include/freetype2 /usr/local/include
ln -sf /usr/local/freetype/include/ft2build.h /usr/local/include

//上传安装curl-7.42.1.tar.gz
tar xzvf curl-7.42.1.tar.gz
cd curl-7.42.1
./configure --prefix=/usr/local/curl --enable-ares --without-nss --with-ssl
make && make install
---上面这几个centos7是不需要了

安装这些依赖包后还需要做些设置

ln -sf /usr/lib64/libpng.* /usr/lib/
ln -sf /usr/lib64/libjpeg.* /usr/lib/
ulimit -v unlimited
echo "/lib" >> /etc/ld.so.conf
echo "/usr/lib" >> /etc/ld.so.conf
echo "/usr/lib64" >> /etc/ld.so.conf
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
cat >>/etc/security/limits.conf<<eof
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
eof
echo "fs.file-max=65535" >> /etc/sysctl.conf

接着就是安装配置openresty(版本1.9.7.4)

groupadd www
useradd -s /sbin/nologin -g www www
tar xzvf openresty-1.9.7.4.tar.gz
cd openresty-1.9.7.4
./configure --with-luajit --user=www --group=www --with-http_stub_status_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-http_iconv_module
//Centos6 需要
cd ../
tar xzvf pcre-8.36.tar.gz
cd openresty-1.9.7.4
./configure --with-luajit --user=www --group=www --with-http_stub_status_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-http_iconv_module --with-pcre=../pcre-8.36 --with-pcre-jit
//Centos6 需要
gmake
gmake install

cd ../
ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/bin/nginx
rm -f /usr/local/openresty/nginx/conf/nginx.conf
cp conf/nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
cp conf/rewrite/dabr.conf /usr/local/openresty/nginx/conf/dabr.conf
cp conf/rewrite/discuz.conf /usr/local/openresty/nginx/conf/discuz.conf
cp conf/rewrite/sablog.conf /usr/local/openresty/nginx/conf/sablog.conf
cp conf/rewrite/typecho.conf /usr/local/openresty/nginx/conf/typecho.conf
cp conf/rewrite/typecho2.conf /usr/local/openresty/nginx/conf/typecho2.conf
cp conf/rewrite/wordpress.conf /usr/local/openresty/nginx/conf/wordpress.conf
cp conf/rewrite/discuzx.conf /usr/local/openresty/nginx/conf/discuzx.conf
cp conf/rewrite/none.conf /usr/local/openresty/nginx/conf/none.conf
cp conf/rewrite/wp2.conf /usr/local/openresty/nginx/conf/wp2.conf
cp conf/rewrite/phpwind.conf /usr/local/openresty/nginx/conf/phpwind.conf
cp conf/rewrite/shopex.conf /usr/local/openresty/nginx/conf/shopex.conf
cp conf/rewrite/dedecms.conf /usr/local/openresty/nginx/conf/dedecms.conf
cp conf/rewrite/drupal.conf /usr/local/openresty/nginx/conf/drupal.conf
cp conf/rewrite/ecshop.conf /usr/local/openresty/nginx/conf/ecshop.conf
cp conf/pathinfo.conf /usr/local/openresty/nginx/conf/pathinfo.conf
cp conf/enable-php.conf /usr/local/openresty/nginx/conf/enable-php.conf
cp conf/enable-php-pathinfo.conf /usr/local/openresty/nginx/conf/enable-php-pathinfo.conf
cp conf/proxy-pass-php.conf /usr/local/openresty/nginx/conf/proxy-pass-php.conf
cp conf/enable-ssl-example.conf /usr/local/openresty/nginx/conf/enable-ssl-example.conf

mkdir -p /home/wwwroot/default
chmod +w /home/wwwroot/default
mkdir -p /home/wwwlogs
chmod 777 /home/wwwlogs

chown -R www:www /home/wwwroot/default
mkdir /usr/local/openresty/nginx/conf/vhost

cat >/home/wwwroot/default/.user.ini<<EOF
open_basedir=/home/wwwroot/default:/tmp/:/proc/
EOF
chmod 644 /home/wwwroot/default/.user.ini
chattr +i /home/wwwroot/default/.user.ini

cp conf/init.d/init.d.nginx /etc/init.d/nginx
chmod +x /etc/init.d/nginx

安装配置php(版本7.0.4)

tar xzvf php-7.0.4.tar.gz
cd php-7.0.4
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-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 --enable-intl --enable-pcntl --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-xsl
make ZEND_EXTRA_LIBS='-liconv'
make install

ln -sf /usr/local/php/bin/php /usr/bin/php
ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
ln -sf /usr/local/php/bin/pear /usr/bin/pear
ln -sf /usr/local/php/bin/pecl /usr/bin/pecl
ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm

mkdir -p /usr/local/php/etc
cp php.ini-production /usr/local/php/etc/php.ini

sed -i 's/post_max_size =.*/post_max_size = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/upload_max_filesize =.*/upload_max_filesize = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/;date.timezone =.*/date.timezone = PRC/g' /usr/local/php/etc/php.ini
sed -i 's/short_open_tag =.*/short_open_tag = On/g' /usr/local/php/etc/php.ini
sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
sed -i 's/max_execution_time =.*/max_execution_time = 300/g' /usr/local/php/etc/php.ini
sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini

pear config-set php_ini /usr/local/php/etc/php.ini
pecl config-set php_ini /usr/local/php/etc/php.ini

cat >>/usr/local/php/etc/php.ini<<EOF

;eaccelerator

;ionCube

;opcache

[Zend ZendGuard Loader]
;php7 do not support zendguardloader @Sep.2015,after support you can uncomment the following line.
;zend_extension=/usr/local/zend/ZendGuardLoader.so
;zend_loader.enable=1
;zend_loader.disable_licensing=0
;zend_loader.obfuscation_level_support=3
;zend_loader.license_path=

;xcache

EOF

mkdir -p /home/wwwroot/test
chown -R www:www /home/wwwroot/test
cp ../conf/ocp.php /home/wwwroot/test/ocp.php

cat >/usr/local/php/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice

[www]
listen = /tmp/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

cat >/home/wwwroot/test/phpinfo.php<<eof
<?
phpinfo();
?>
eof

tar zxf ../conf/p.tar.gz
cp p.php /home/wwwroot/test/p.php

cp ../conf/index.html /home/wwwroot/test/index.html
cp ../conf/lnmp.gif /home/wwwroot/test/lnmp.gif

tar zxf ../conf/phpMyAdmin-4.4.15.2-all-languages.tar.gz
mv phpMyAdmin-4.4.15.2-all-languages /home/wwwroot/test/phpmyadmin
cp ../conf/config.inc.php /home/wwwroot/test/phpmyadmin/config.inc.php
mkdir /home/wwwroot/test/phpmyadmin/{upload,save}
chmod 755 -R /home/wwwroot/test/phpmyadmin/
chown www:www -R /home/wwwroot/test/phpmyadmin/

安装php的redis扩展

unzip phpredis-php7.zip
cd phpredis-php7

sed -i '/redis.so/d' /usr/local/php/etc/php.ini

/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

sed -i '/the dl()/i
extension = "redis.so"' /usr/local/php/etc/php.ini

安装redis(版本3.0.7)

tar xzvf redis-3.0.7.tar.gz
cd redis-3.0.7
make PREFIX=/usr/local/redis install
mkdir -p /usr/local/redis/etc/
cp redis.conf  /usr/local/redis/etc/
sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/etc/redis.conf
sed -i 's/^# bind 127.0.0.1/bind 127.0.0.1/g' /usr/local/redis/etc/redis.conf
cp ../conf/init.d/init.d.redis /etc/init.d/redis
chmod +x /etc/init.d/redis

ln -s  /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli
ln -s  /usr/local/redis/bin/redis-server /usr/local/bin/redis-server
ln -s  /usr/local/redis/bin/redis-benchmark /usr/local/bin/redis-benchmark
ln -s  /usr/local/redis/bin/redis-check-aof /usr/local/bin/redis-check-aof
ln -s  /usr/local/redis/bin/redis-check-dump /usr/local/bin/redis-check-dump
ln -s  /usr/local/redis/bin/redis-sentinel /usr/local/bin/redis-sentinel

安装mysql(5.6.27)

groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql
screen -S mysql
tar xzvf mysql-5.6.27.tar.gz
cd mysql-5.6.27
patch -p1 < ../patch/mysql-openssl.patch
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1
make && make install

cat > /etc/my.cnf<<EOF
[client]
#password   = your_password
port        = 3306
socket      = /tmp/mysql.sock

[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
table_open_cache = 64

explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id   = 1
expire_logs_days = 10

#loose-innodb-trx=0
#loose-innodb-locks=0
#loose-innodb-lock-waits=0
#loose-innodb-cmp=0
#loose-innodb-cmp-per-index=0
#loose-innodb-cmp-per-index-reset=0
#loose-innodb-cmp-reset=0
#loose-innodb-cmpmem=0
#loose-innodb-cmpmem-reset=0
#loose-innodb-buffer-page=0
#loose-innodb-buffer-page-lru=0
#loose-innodb-buffer-pool-stats=0
#loose-innodb-metrics=0
#loose-innodb-ft-default-stopword=0
#loose-innodb-ft-inserted=0
#loose-innodb-ft-deleted=0
#loose-innodb-ft-being-deleted=0
#loose-innodb-ft-config=0
#loose-innodb-ft-index-cache=0
#loose-innodb-ft-index-table=0
#loose-innodb-sys-tables=0
#loose-innodb-sys-tablestats=0
#loose-innodb-sys-indexes=0
#loose-innodb-sys-columns=0
#loose-innodb-sys-fields=0
#loose-innodb-sys-foreign=0
#loose-innodb-sys-foreign-cols=0

default_storage_engine = InnoDB
#innodb_data_home_dir = /usr/local/mysql/var
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/var
#innodb_buffer_pool_size = 16M
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
EOF

sed -i 's:^#innodb:innodb:g' /etc/my.cnf

if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 32M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 8192 && ${MemTotal} -lt 16384 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 16384 && ${MemTotal} -lt 32768 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 2048#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 256M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 512M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 32768 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 4096#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 512M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf
fi

mkdir -p /usr/local/mysql/var

chown -R mysql:mysql /usr/local/mysql/var
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql
chgrp -R mysql /usr/local/mysql/.
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql

cat > /etc/ld.so.conf.d/mysql.conf<<EOF
/usr/local/mysql/lib
/usr/local/lib
EOF
ldconfig
ln -sf /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -sf /usr/local/mysql/include/mysql /usr/include/mysql

ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -sf /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -sf /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -sf /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
ln -sf /usr/local/mysql/bin/mysqlcheck /usr/bin/mysqlcheck

如果是安装5.7.9版本,按一下步骤

groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql
screen -S boost
tar xzvf boost_1_59_0.tar.gz
cd boost_1_59_0
./bootstrap.sh
./b2
./b2 install
cd ../
tar xzvf mysql-5.7.9.tar.gz
cd mysql-5.7.9
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1
make && make install

cat > /etc/my.cnf<<EOF
[client]
#password   = your_password
port        = 3306
socket      = /tmp/mysql.sock

[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
table_open_cache = 64

explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id   = 1
expire_logs_days = 10

#loose-innodb-trx=0
#loose-innodb-locks=0
#loose-innodb-lock-waits=0
#loose-innodb-cmp=0
#loose-innodb-cmp-per-index=0
#loose-innodb-cmp-per-index-reset=0
#loose-innodb-cmp-reset=0
#loose-innodb-cmpmem=0
#loose-innodb-cmpmem-reset=0
#loose-innodb-buffer-page=0
#loose-innodb-buffer-page-lru=0
#loose-innodb-buffer-pool-stats=0
#loose-innodb-metrics=0
#loose-innodb-ft-default-stopword=0
#loose-innodb-ft-inserted=0
#loose-innodb-ft-deleted=0
#loose-innodb-ft-being-deleted=0
#loose-innodb-ft-config=0
#loose-innodb-ft-index-cache=0
#loose-innodb-ft-index-table=0
#loose-innodb-sys-tables=0
#loose-innodb-sys-tablestats=0
#loose-innodb-sys-indexes=0
#loose-innodb-sys-columns=0
#loose-innodb-sys-fields=0
#loose-innodb-sys-foreign=0
#loose-innodb-sys-foreign-cols=0

default_storage_engine = InnoDB
#innodb_data_home_dir = /usr/local/mysql/var
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/var
#innodb_buffer_pool_size = 16M
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
EOF

sed -i 's:^#innodb:innodb:g' /etc/my.cnf

if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 32M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 8192 && ${MemTotal} -lt 16384 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 16384 && ${MemTotal} -lt 32768 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 2048#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 256M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 512M#" /etc/my.cnf
elif [[ ${MemTotal} -ge 32768 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" /etc/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 4096#" /etc/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" /etc/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" /etc/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" /etc/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" /etc/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 512M#" /etc/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" /etc/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" /etc/my.cnf
    sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf
fi

mkdir -p /usr/local/mysql/var

chown -R mysql:mysql /usr/local/mysql/var
/usr/local/mysql/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql
chgrp -R mysql /usr/local/mysql/.
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql

cat > /etc/ld.so.conf.d/mysql.conf<<EOF
/usr/local/mysql/lib
/usr/local/lib
EOF
ldconfig
ln -sf /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -sf /usr/local/mysql/include/mysql /usr/include/mysql

ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -sf /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -sf /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -sf /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
ln -sf /usr/local/mysql/bin/mysqlcheck /usr/bin/mysqlcheck

mysql安装后,设置远程登陆

/usr/local/mysql/bin/mysqladmin -u root password "yourpassword"
mysql -u root -p yourpassword
DELETE FROM mysql.user WHERE User='';
DROP USER ''@'%';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE test;
grant all privileges on *.* to 'root'@'%' identified by 'yourpassword' with grant option;
flush privileges;

开机自动启动,防火墙设置等

chkconfig redis on
chkconfig nginx on
chkconfig php-fpm on
chkconfig mysql on

/sbin/iptables -I INPUT 1 -i lo -j ACCEPT
/sbin/iptables -I INPUT 2 -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables -I INPUT 3 -p tcp --dport 22 -j ACCEPT
/sbin/iptables -I INPUT 4 -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT 5 -p tcp --dport 3306 -j DROP
/sbin/iptables -I INPUT 6 -p icmp -m icmp --icmp-type 8 -j ACCEPT
/sbin/iptables -A INPUT -p tcp --dport 6379 -j DROP

service iptables save
systemctl stop firewalld
systemctl disable firewalld

安装告一段落,安装包连接(包括依赖包、安装包和配置文件):
https://yunpan.cn/OcRYC8xdMAyVIe 访问密码 c406
接下来就看教程和实践了,教程首推OpenResty最佳实践