Openresty + MySQL + Redis 安装篇,openrestyredis


Openresty + MySQL + Redis 安装篇


安装环境

VMWare + CentOS 6.7 32 位

Linux AlexWoo-CentOS 2.6.32-573.el6.i686 #1 SMP Thu Jul 23 12:37:35 UTC 2015 i686 i686 i386 GNU/Linux

MySQL 5.7.9 安装

下载

wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.9-linux-glibc2.5-i686.tar.gz

安装

安装参考 http://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

如果原有系统安装过 MySQL,需要将其完全删除,一般需要清除 /usr/local/mysql/,/var/lib/mysql/,/var/log/mysql.log,/etc/my.cnf,/etc/mysql/

开始安装:

# tar xzf  mysql-5.7.9-linux-glibc2.5-i686.tar.gz
# mv mysql-5.7.9-linux-glibc2.5-i686 /usr/local/mysql
# groupadd -r mysql
# useradd -r -g mysql mysql
# mkdir mysql-files
# chmod -R 770 /usr/local/mysql
# cd /usr/local/mysql
# chown -R mysql:mysql ./
# bin/mysqld --initialize --user=mysql
# bin/mysql_ssl_rsa_setup
# chown -R root ./
# chown -R mysql data
# cp support-files/mysql.server /etc/init.d/mysql.server

完成 bin/mysqld –initialize –user=mysql 后,会有一条日志:

2015-10-24T17:25:38.101341Z 1 [Note] A temporary password is generated for root@localhost: _4#Ernwu9xf:

记住这个密码,5.7 版本完成安装后,默认是需要密码登录的,修改密码:

./mysqladmin -u root -h localhost password 'your new password' -p

手工启动 MySQL:

# /usr/local/mysql/bin/mysqld_safe --user=mysql &

手工停止 MySQL:

# /usr/local/mysql/bin/mysqladmin shutdown -p

启动 MySQL 服务:

# service mysql start

停止 MySQL 服务:

# service mysql stop

使用客户端接入:

mysql -h localhost -u root -p

Redis 2.8.23 安装

下载

wget https://github.com/antirez/redis/archive/2.8.23.tar.gz

安装

注意:不要把 redis 安装到公网能够访问的机器上

# tar xzf 2.8.23.tar.gz
# cd redis-2.8.23/
# make && make install
# utils/install_server.sh

以上最后一步为将 redis 安装成系统服务,按照默认安装后,相关的配置和日志文件位置

Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli

启动 redis 服务:

service redis_6379 start

停止 redis 服务:

service redis_6379 stop

客户端接入:

redis-cli

OpenResty 1.9.3.1 安装

下载

下载 pcre 安装包

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.bz2

下载 zlib 安装包

wget http://zlib.net/zlib-1.2.8.tar.gz

下载 openssl 安装包

wget http://www.openssl.org/source/openssl-1.0.2d.tar.gz

下载 openresty 安装包

wget https://openresty.org/download/ngx_openresty-1.9.3.1.tar.gz

下载 drizzle 安装包(注:openresty 访问 MySQL 使用 drizzle 模块需要安装这个包)

wget http://openresty.org/download/drizzle7-2011.07.21.tar.gz

下载 lua-resty-http(这是一个基于 nginx socket 的 HTTP 客户端库)

wget https://github.com/pintsized/lua-resty-http/archive/v0.06.zip

安装

安装 pcre

# tar xjf pcre-8.36.tar.bz2
# cd pcre-8.36
# ./configure
# make && make install

安装 zlib

# tar xzf zlib-1.2.8.tar.gz
# cd zlib-1.2.8
# ./configure
# make && make install

安装 drizzle 客户端

# tar xzf drizzle7-2011.07.21.tar.gz
# cd drizzle7-2011.07.21
# ./configure --without-server
# make libdrizzle-1.0
# make install-libdrizzle-1.0
# ln -s /usr/local/lib/libdrizzle.so.1 /usr/lib/libdrizzle.so.1

安装 openresty

# mkdir /usr/local/openresty
# tar xzf openssl-1.0.2d.tar.gz
# mv openssl-1.0.2d /usr/local/openresty/openssl
# tar xzf ngx_openresty-1.9.3.1.tar.gz
# cd ngx_openresty-1.9.3.1
# ./configure \
     --with-http_stub_status_module \
     --with-http_gzip_static_module \
     --with-http_ssl_module --with-openssl=/usr/local/openresty/openssl \
     --with-http_drizzle_module
# gmake && gmake install

安装 lua-resty-http

# unzip v0.06.zip
# cp lua-resty-http-0.06/lib/resty/* /usr/local/openresty/lualib/resty/

为 www 账户创建 openresty 运行环境

# cp -r /usr/local/openresty/nginx/ /home/www
# chown -R www:www /home/www/nginx
# su - www
$ echo 'export PATH=$PATH:$HOME/nginx/sbin' >> .bash_profile
$ source .bash_profile
$ echo 'alias nginx="nginx -p /home/www/nginx"' >> .bashrc
$ source .bashrc

完成以上操作后,www 账户就可以启动 nginx 了,此时需注意 nginx 默认的监听端口是 80,这个端口是不能被普通用户绑定的,因此需要修改 ~/nginx/conf/nginx.conf 中的端口号,如 8080

iptables 配置

  • 解禁 8080 端口

    # iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
    # service iptables save
    
  • 将 80 端口映射到 8080 端口

    # iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
    # iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
    # service iptables save
    

    注:这里映射需要将目标端口号(8080)的访问权限打开,而访问端口号(80)的访问权限不用打开。使用 service iptables save 将修改保存到 /etc/sysconfig/iptables 文件中

    上面命令中

    • 第一条为打开 8080 端口的访问权限
    • 第二条为打开外部网络的 NAT 转发
    • 第三条为打开 loopback 网卡的 NAT 转发
    • 第四条为将上面配置更新到 /etc/sysconfig/iptables 里

使用 HTTPS

Nginx 的 HTTPS 需要使用 http_ssl_module 模块,前面在 Openresty 安装的时候已经加载了该模块

生成私钥和证书

$ cd /home/www/nginx/conf
$ openssl genrsa -des3 -out myopenresty.key 1024
$ openssl req -new -key myopenresty.key -out myopenresty.csr
$ openssl rsa -in myopenresty.key -out myopenresty.key
$ openssl x509 -req -days 365 -in myopenresty.csr -signkey myopenresty.key -out myopenresty.crt
  • myopenresty.key 为服务器私钥
  • myopenresty.crt 为服务器证书

配置Nginx

server {
    listen       8443 ssl;
    server_name  localhost;

    ssl_certificate      myopenresty.crt;
    ssl_certificate_key  myopenresty.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        root   html;
        index  index.html index.htm;
    }
}

因为我们不是用 root 账户进行启动的,因此,监听端口需要改为8443,然后使用 iptables 进行端口映射

需要注意,新增加 8443 端口,不能使用 nginx -s reload 重新加载配置,而需要将 Nginx 停止后,再重新启动,才能进行监听

iptables 配置

将 443 端口映射到 8443 端口

# iptables -I INPUT -p tcp --dport 8443 -j ACCEPT
# iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 8443
# service iptables save

环境验证

在 www 账户的 ~/nginx/conf/nginx.conf 的 http 的 server 下添加以下配置

location /test {
    default_type "text/plain";
    content_by_lua '
        ngx.say("Hello,world!")
    ';
}

打开网页,访问 http://your.host/test,可以看到网页上输出 Hello,world! 字样,以此判断环境安装成功

相关内容

    暂无相关文章