重构、重构、重构
大三下学期了,面试某公司,结果连技术面都没过,╮(╯▽╰)╭,还是有一点打击的。不过也有打算重构一下两个小项目的,希望以此叩开实习的大门。记录一下全过程!

重构前

这部分主要是服务器的搭建。
之前Github学生认证,送了DigitalOcean 100刀,买了一台位于新加坡节点的512 MB Memory / 20 GB Disk / SGP1 - CentOS 6.7 x64

编译nginx mongodb … 前的准备

1
2
yum update
yum install wget gcc gcc-c++ perl vim

编译 nginx

先把所需要的库下载安装好 zlib pcre openssl

zlib http://zlib.net/zlib-1.2.8.tar.gz
pcre http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-8.38.tar.gz
openssl http://www.openssl.org/source/openssl-1.0.1s.tar.gz

1
2
3
4
5
6
7
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.9.12.tar.gz
tar xzf nginx-1.9.12.tar.gz
cd nginx-1.9.12
./configure --with-http_v2_module --with-http_ssl_module --with-pcre --with-openssl=/usr/local/src/openssl-1.0.1s
make
make install

编译参数很多,但这里我用的很少,主要原因还是自己不太熟悉,至于怎么优化,留给以后深入学习nginx再继续。

当启动nginx出现了这个错误error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory,搜了一下可以这样解决ln -s /usr/local/lib/libpcre.so.1 /lib64

配置虚拟主机

为方便管理配置虚拟主机,在nginx.conf 最后一个}之前添加一行include vhost/*.conf;并建立vhost目录,并增加一个配置文件apix.xu42.cn.conf,内容暂时简单如下:

server {
listen 80;
server_name apix.xu42.cn;
root /data/wwwroot/apix.xu42.cn;
index index.html;
}

/usr/local/nginx/sbin/nginx -t测试配置是否有错,无误则 -s reload

选用了这位同学的一个快速签发证书的脚本,我的letsencrypt.conf文件内容如下:

ACCOUNT_KEY=”letsencrypt-account.key”
DOMAIN_KEY=”apix.xu42.cn.key”
DOMAIN_DIR=”/data/wwwroot/apix.xu42.cn”
DOMAINS=”DNS:apix.xu42.cn”
ECC=TRUE

我在执行时报了一个ImportError: No module named argparse的错误,安装即可yum install python argparse

修改apix.xu42.cn文件如下(部分)(参考了这位同学的配置文件):

server {
listen 443 ssl http2;
server_name apix.xu42.cn;
client_max_body_size 10M;
ssl_certificate /usr/local/nginx/certs/apix.chained.crt;
ssl_certificate_key /usr/local/nginx/certs/apix.xu42.cn.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 10s;
}

至此,nginx的配置算是告一段落,接下来是php的配置

编译PHP

编译安装了两个大版本的PHP,5.6.19和7.0.4,下载并解压。

PHP 7.0.4

同样的,先行安装依赖(参考了这篇文章):

yum install -y make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers php-mysqlnd libmcrypt-devel libtidy libtidy-devel recode recode-devel libxpm-devel

编译安装:

./configure \
–prefix=/usr/local/php7 \
–with-config-file-path=/usr/local/php7/etc \
–enable-mbstring \
–enable-zip \
–enable-bcmath \
–enable-pcntl \
–enable-ftp \
–enable-exif \
–enable-calendar \
–enable-sysvmsg \
–enable-sysvsem \
–enable-sysvshm \
–enable-opcache \
–enable-fpm \
–enable-session \
–enable-sockets \
–enable-mbregex \
–with-fpm-user=vagrant \
–with-fpm-group=nogroup \
–enable-wddx \
–with-curl \
–with-mcrypt \
–with-iconv \
–with-gd \
–with-jpeg-dir=/usr \
–with-png-dir=/usr \
–with-zlib-dir=/usr \
–with-freetype-dir=/usr \
–enable-gd-native-ttf \
–enable-gd-jis-conv \
–with-openssl \
–with-pdo-mysql=mysqlnd \
–with-gettext=/usr \
–with-zlib=/usr \
–with-bz2=/usr \
–with-recode=/usr \
–with-xmlrpc \
–with-mysqli=mysqlnd

这里遇到了configure: error: mcrypt.h not found. Please reinstall libmcrypt的错误,然而yum install libmcrypt libmcrypt-devel并不能安装,搜了一下这篇文章找到了解决办法,但在我主机上不能解析那个地址,再搜一个即可。然后编译安装libmcrypt

再次编译安装php-7.0.4,再次遇到错误virtual memory exhausted: Cannot allocate memory,512M内存的机器,内存不够。搜了一下,问题还挺普遍,是swap交换空间没设置,参考这篇文章解决了问题。

dd if=/dev/zero of=/swap bs=1024 count=1M
mkswap /swap
swapon /swap
echo “/swap swap swap sw 0 0” >> /etc/fstab

php配置文件

cp php.ini-production /usr/local/php7/etc/php.ini
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

此时启动php-fpm会报cannot get uid for user 'vagrant'的错误,这是因为运行php-fpm的用户未设定造成的,我们新建一个www用户(useradd -U www)专门来运行nginx和php,分别修改/usr/local/php7/etc/php-fpm.d/www.conf配置中的user和group、/usr/local/nginx/conf/nginx.conf配置中的user为user www www。重新启动nginx和php-fpm。

更改apix.xu42.cn.conf添加以下内容以支持php

location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

随便编写一个php测试页面测试一下是否配置成功。

为方便管理,添加系统变量在/etc/profile文件末行添加一行

export PATH=/usr/local/nginx/sbin:/usr/local/php7/bin:/usr/local/php7/sbin:$PATH

安装mongodb

参照官方安装手册即可

这样启动会有WARNING: soft rlimits too low,配置一下,参考这篇文章
修改/etc/security/limits.conf,添加下面内容

mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 32000
mongod hard nproc 32000

在php里启用扩展,参照这里安装


至此,开发环境算是配置完成。(考虑到512M的内存,并没有安装mysql服务,php7暂时没发现什么兼容性问题,就先不安装php5了)