Write the Code. Change the World.

4月 21

什么是 brotli

brotli 是 Google 开发的最新压缩算法,有效减少网站传输数据。具体内容请查看 WIKI。

https://en.wikipedia.org/wiki/Brotli

安装

安装依赖 cmake

https://blog.vini123.com/289

下载安装

下载安装模块

https://github.com/google/ngx_brotli

https://github.com/eustas/ngx_brotli

cd /usr/local/src

git clone https://github.com/eustas/ngx_brotli.git 

cd ngx_brotli

git submodule update --init --recursive

原文

https://www.xp8.net/server/645.html

阅读全文 >>

4月 21

官网:https://www.openssl.org/ 当前最新版本 1.1.1

下载安装

cd /usr/local/src

wget https://www.openssl.org/source/openssl-1.1.1b.tar.gz

tar -xzvf openssl-1.1.1b.tar.gz

cd openssl-1.1.1b

# 制定编译安装后的位置
./config --prefix=/usr/local/openssl

make && make install

查看安装

which openssl

建立软链接

ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl

执行

cd /usr/local/openssl

ldd /usr/local/openssl/bin/openssl

查看版本

openssl version

# 找不到动态库

解决动态库问题

vim /etc/ld.so.conf

# 在尾部追加
/usr/local/openssl/lib

# 然后执行
ldconfig /etc/ld.so.conf
openssl version
# 这个时候,版本号就出来了

阅读全文 >>

4月 21

官网:http://www.bzip.org/downloads.html
源码包:https://sourceforge.net/projects/bzip2/

步骤

当前最新版本 1.0.6

cd /usr/local/src

wget https://nchc.dl.sourceforge.net/project/bzip2/bzip2-1.0.6.tar.gz 

tar -xzvf bzip2-1.0.6.tar.gz

cd bzip2-1.0.6

# 为编译做准备,创建libbz2.so动态链接库

make -f Makefile-libbz2_so

# 编译安装
make && make install

安装完成。

阅读全文 >>

4月 20

当下 (2019-04-20)最新的稳定版本的 nginx 版本是 1.14.2。下边来进行编译安装该版本,使其支持 http2 以及 TLS1.3 还有 Brotli 。 都是好东西,可以慢慢去了解。

安装依赖包

### 先更新包,软件,系统内核
yum update 

安装 gcc

安装 nginx 源码进行编译,编译依赖 gcc 环境,需要安装gcc: yum install gcc-c++

安装 pcre-devel

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。

yum install  pcre pcre-devel

zlib 安装

nginx 使用 zlib 对 http 包的内容进行 gzip。可是,不是用 Brotli 么。两个都装,装了也没事。gzip 编译安装来个。如果不编译,使用 yum 安装,版本达不到要求。

# 版本没要求可以这样
yum install zlib zlib-devel

先安装 cmake
https://blog.vini123.com/289

再安装 zlib
https://blog.vini123.com/299

再安装 bzip2
https://blog.vini123.com/300

最后安装 libzib
https://blog.vini123.com/290

OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

https://blog.vini123.com/301

安装 ngx_brotli

cd /opt
git clone https://github.com/eustas/ngx_brotli.git
cd ngx_brotli
git submodule update --init --recursive

可以看这里: https://blog.vini123.com/302

增加用户组

我们创建一个新的用户组和用户来运行服务,这样可以把nginx和root分开,保证nginx不具备root权限。但是,我们并不希望nginx成为一个真实的可以登陆到远程进行操作的用户,所以,我们并不给它创建home目录,在useradd的时候,用-M参数:

 groupadd www
 useradd -g www -M www

-g参数为nginx用户指定了一个组。-M参数保证其不自动生成home目录。

但通过上面的用户创建之后,www 用户可以通过设置一个密码登陆到服务器,这个不是我们想要的,我们禁用它的 ssh 登陆权限.禁止用户登陆也很方便,只需要修改配置文件中有关用户和用户组的信息即可。

vi /etc/passwd

找到www,将后面的/bin/bash改为/sbin/nologin即可。

下载安装 nginx

官网:http://nginx.org/en/download.html 当前最新稳定版本 1.14.2

cd /usr/local/src

wget http://nginx.org/download/nginx-1.14.2.tar.gz

tar -xzvf nginx-1.14.2.tar.gz

cd nginx-1.14.2 

配置编译

./configure \
--user=www \
--group=www \
--prefix=/alidata/service/nginx \
--pid-path=/alidata/service/nginx/run/nginx.pid \
--with-http_stub_status_module \ ## 监控模块,nginx自带,但默认不安装
--with-threads \
--with-file-aio \
--with-pcre-jit \
--with-http_ssl_module \ ## 开启 HTTPS 支持
--with-http_v2_module \ ## 开启 HTTP/2 
--with-http_gzip_static_module \ ## 开启 GZip 压缩
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_realip_module \
--with-http_addition_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-http_slice_module \
--with-pcre \
--with-openssl=/usr/local/src/openssl-1.1.1b/  \ ## 指定单独编译入 OpenSSL 的源码位置
--with-openssl-opt=enable-tls1_3 \ ## 开启 TLS 1.3 支持
--add-module=/usr/local/src/ngx_brotli/ ## 编译入 ngx_BroTli 扩展

如果有问题,将这些配置写成一行。没有问题会提示:


Configuration summary + using threads + using system PCRE library + using OpenSSL library: /usr/local/src/openssl-1.1.1b + using system zlib library nginx path prefix: "/alidata/service/nginx" nginx binary file: "/alidata/service/nginx/sbin/nginx" nginx modules path: "/alidata/service/nginx/modules" nginx configuration prefix: "/alidata/service/nginx/conf" nginx configuration file: "/alidata/service/nginx/conf/nginx.conf" nginx pid file: "/alidata/service/nginx/run/nginx.pid" nginx error log file: "/alidata/service/nginx/logs/error.log" nginx http access log file: "/alidata/service/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"

好了,现在开始编译安装。

make && make install

安装完毕后,启动 nginx

/alidata/server/nginx/sbin/nginx 
ps -ef|grep nginx

# 查看编译的模块
/alidata/service/nginx/sbin/nginx -V

配置开机自启动

切换到 /lib/systemd/system/ 目录,创建 nginx.service 文件 vi nginx.service
编辑 nginx.service

[Unit]
Description=nginx 
After=network.target 

[Service] 
Type=forking 
ExecStart=/alidata/service/nginx/sbin/nginx
ExecReload=/alidata/service/nginx/sbin/nginx reload
ExecStop=/alidata/service/nginx/sbin/nginx quit
PrivateTmp=true 

[Install] 
WantedBy=multi-user.target

保存,并退出。再执行 systemctl enable nginx.service 激活重启自启动功能

systemctl enable nginx.service

systemctl start nginx.service #启动nginx
systemctl stop nginx.service #结束nginx
systemctl restart nginx.service #重启nginx

curl 127.0.0.1 查看欢迎页面。或输入外网 ip,如果外网 ip 访问不到,可能是 80端口没开放。去开放端口。比如去阿里云添加安全组,再重启 nginx。

参考

https://www.mf8.biz/nginx-install-tls1-3/

阅读全文 >>

4月 17

https 测试

https://myssl.com

nginx 配置

ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000";
add_header Content-Security-Policy "default-src 'self';script-src * 'unsafe-inline';style-src * 'unsafe-inline';";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options 'SAMEORIGIN';

TLS 1.3 也渐渐流行。可以自行配置。

阅读全文 >>

4月 14

owasp 安全测试,其中一项就是关于请求头中的 servers 信息和 x-powered-by 信息要隐藏。该怎么隐藏呢。servers 对应的是 nginx 或 apache 的信息,x-powered-by 可能是 php 的版本号。

如下图:

用户,可以很方便的看到你服务器环境的版本信息。想要隐藏只需下边这样操作即可。

隐藏 servers

nginx

vim nginx.conf

# http 里边添加下边的设置

server_tokens off;

然后重启 nginx 。 ../sbin/nginx -s reload

隐藏 php 版本

# 找到 php.ini 的位置
php -i|grep php.ini

vim php.ini

# 设置下边的值即可。即将 on 改成 off
expose_php = Off

然后重启 php 。 /etc/init.d/php-fpm restart

阅读全文 >>

4月 02

想做聊天这样的app或页面,就会使用到长连接。websocket 是一种方式。服务方可以用 nodejs 这些。对于 php,还是使用 workman 或 swoole 比较方便。这里就记录下 laravel 框架使用 swoole 的过程。

编译安装 swoole 扩展

http://pecl.php.net/package/swoole
当前,最新版本是 4.31。所有一切都是新的,服务器是新的,装的环境也是最新的。所以没有心理压力,都用最新的。

cd /usr/local/src

wget http://pecl.php.net/get/swoole-4.3.1.tgz

tar -xzvf swoole-4.3.1.tgz

cd swoole-4.3.1

phpize

# 对 ssl 的支持。这个还是比较重要的
./configure --enable-openssl 

make && make install

make test

vim /alidata/service/php/etc/php.ini

# 尾部追加
extension=swoole.so

# 重启 php
/etc/init.d/php-fpm restart

# 查看扩展
# php -m|grep swoole

阅读全文 >>

3月 19

本来 php7.3 使用的蛮好的。突然有一个旧项目,框架所使用的 php 版本是 php5.6。这个时候就尴尬了。不可能一一去修改源码。这个时候,就得再安装一个 php5.6。还好 nginx 配合多版本 php 使用非常的方便。

PHP 下载地址: http://php.net/releases/

下载编译安装

# 还是一样,习惯将文件下载的 `/usr/local/src` 下

cd /usr/local/src

wget http://cn2.php.net/get/php-5.6.40.tar.gz

tar -xzvf php-5.6.40.tar.gz

cd php-5.6.40

# 下边就开始配置

配置如下:

# 因为之前有一个 /alidata/service/php 目录,这里使用 php5.6 加以区分
./configure --prefix=/alidata/service/php5.6 \
--with-config-file-path=/alidata/service/php5.6/etc \
--enable-inline-optimization --disable-debug \
--disable-rpath --enable-shared --enable-opcache \
--enable-fpm --with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--enable-mbstring \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--enable-session \
--enable-ftp \
--enable-xml \
--with-curl --with-zlib \
--enable-zip \
--with-bz2 \
--enable-pdo \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-readline

注意要使用的库一定要带上。比如 gd 库。要不额外弄,伤脑筋。

阅读全文 >>