如无特殊说明,本系列中涉及的操作系统版本均为CentOS 7.3

安装Node.js

使用官方已编译包安装

首先到官网https://nodejs.org/download/release/找到需要的版本,此处以v7.9.0为例:

1
2
3
4
wget https://nodejs.org/download/release/latest-v7.x/node-v7.9.0-linux-x64.tar.gz
sudo tar --strip-components 1 -xzvf node-v* -C /usr/local
node -v
# v7.9.0

下载对应的包并解压安装至/usr/local
安装完成后可运行node -v,返回版本号即安装成功。

如果服务器在国内,安装完成之后可以使用淘宝镜像来加速:

1
npm config set registry https://registry.npm.taobao.org

从源码编译安装

在某些版本的系统中使用这种方法安装可能需要先安装相关的依赖:

1
yum install gcc gcc-c++ kernel-devel

首先到官网http://nodejs.org/dist/注意:源码下载路径和已编译版本下载路径不同)找到需要的版本,此处以v7.9.0为例:

1
2
3
4
5
6
7
8
wget http://nodejs.org/dist/v7.9.0/node-v7.9.0-linux-x64.tar.gz
tar xzvf node-v* && cd node-v*
./configure
make
make test
make install
node -v
# v7.9.0

使用nvm安装(不推荐)

nvm(Node version manager)是一个Node.js的版本管理工具,使用nvm可以轻松的切换Node.js版本。
这里可以找到nvm的最新地址。

1
2
3
4
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
nvm list-remote # 列出当前可安装的版本
nvm install v7.9.0
nvm use v7.9.0

不推荐nvm的原因如下:

  1. 服务器中很少遇到需要切换Node.js版本的情况
  2. nvm的部分内容在CentOS 7重启后会被自动删除
  3. 使用nvm安装的全局包在各个版本之间不能共享

安装OpenSSL

系统中自带的OpenSSL版本一般也较旧,为了HTTP2等新特性,我们需要重新安装1.0.2以上版本的OpenSSL。

首先安装所有可能需要的依赖:

1
yum install gc gcc gcc-c++ pcre-devel zlib-devel make wget openssl-devel libxml2-devel libxslt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel gperftools gperftools-devel libatomic_ops-devel perl-ExtUtils-Embed dpkg-dev libpcrecpp0 libgd2-xpm-dev libgeoip-dev libperl-dev -y

然后从官网https://www.openssl.org/source/获取OpenSSL下载链接,下载并解压编译:
1
2
3
4
5
6
7
8
9
10
11
cd /usr/src
wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz
tar -zxf openssl-1.1.0e.tar.gz
cd openssl-1.1.0e.tar.gz
./config
make
make test
make install
# 备份原来的openssl
mv /usr/bin/openssl /root/
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl

完成后使用openssl version查看版本,如出现 /usr/bin/openssl: No such file or directory,使用如下方式解决
1
2
3
4
5
6
7
8
9
ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
# 删除旧的符号链接
rm /bin/openssl
# 添加新版本的符号链接
ln -s /usr/local/bin/openssl /bin/openssl
# 重新查看版本
openssl version
# OpenSSL 1.1.0e 16 Feb 2017

安装 Nginx

使用yum安装

1
2
3
sudo yum install nginx
nginx -V
sudo systemctl start nginx

检查系统中firewalld防火墙服务是否开启,如果显示active (running),我们需要修改防火墙配置,开启Nginx外网端口访问。

1
2
3
4
sudo systemctl status firewalld
firewall-cmd --permanent --zone=internal --add-service=http
firewall-cmd --permanent --zone=internal --add-service=https
firewall-cmd --reload

也可以在/etc/firewalld/zones/public.xml文件的zone一节中增加:

1
2
3
4
5
<zone>
··· ···
<service name="http"/>
<service name="https"/>
<zone>

保存后重新加载firewalld服务:
1
sudo systemctl reload firewalld

完成后即可通过使用浏览器访问 http://<外网IP地址>,如果出现Nginx欢迎页面,则Nginx成功启动。

使用这种方式安装的nginx版本通常较旧,如果需要安装更新的版本,可以自行在/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo来修改yum源:

1
vim /etc/yum.repos.d/nginx.repo

文件内容如下:
1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

保存后使用yum info nginx可查看源信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@Forever ~]# yum info nginx
Loaded plugins: fastestmirror
··· ···
Determining fastest mirrors
Available Packages
Name : nginx
Arch : x86_64
Epoch : 1
Version : 1.12.0
Release : 1.el7.ngx
Size : 716 k
Repo : nginx/7/x86_64
Summary : High performance web server
URL : http://nginx.org/
License : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.

检查无误后运行yum install nginx -y即可完成安装。

通过源码编译安装

有时我们对nginx有些特殊需求,比如开启SSL需要使用1.0.2以上版本的OpenSSL编译的nginx,这时我们需要自己编译安装nginx。

如果已经安装了nginx,首先使用nginx -V来查看相关信息:

1
2
3
4
5
6
[root@Forever ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

其中configure arguments:之后的就是编译参数,可以从此处复制一份备用。--prefix为nginx安装路径,如果需要开启HTTP2,http_v2_modulehttp_ssl_module这两个模块必备,还需要加上--with-openssl=/usr/src/openssl-1.1.0e来指定所用的OpenSSL版本,还需要启用哪些模块可以根据自己实际情况来决定。

1
2
3
4
cd /root
wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0

使用 cloudflare 的 TLS nginx__dynamic_tls_records 补丁来优化 TLS Record Size

1
2
wget https://raw.githubusercontent.com/cloudflare/sslconfig/master/patches/nginx__dynamic_tls_records.patch
patch -p1 < nginx__dynamic_tls_records.patch

如果提示 patch 命令找不到的话,则先安装 patch
1
yum install patch

加上之前保存的参数开始编译

1
2
3
4
5
6
7
8
9
10
11
./configure --prefix=/etc/nginx --with-openssl=/usr/src/openssl-1.1.0e --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

make
sudo make install

# 查看版本:
nginx -V
# nginx version: nginx/1.12.0
# built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
# built with OpenSSL 1.1.0e 16 Feb 2017
# TLS SNI support enabled

出现built with OpenSSL 1.1.0e,安装成功。

配置LNMP环境

安装PHP7.1

首先删除旧版本的PHP
通过yum list installed | grep php可以查看所有已安装的php软件
使用yum remove php ……删除,如yum remove php70w-common.x86_64 -y

通过yum list php*查看是否有自己需要安装的版本,如果没有就需要添加第三方yum源, 推荐webtatic、rpmforge或网易的源

1
2
3
# CentOs 7.X
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装完成后可以使用yum repolist查看已经安装的源,也可以通过ls /etc/yum.repos.d/查看。

然后再yum install php71w……就可以安装新版本PHP了,常用相关组件的安装如下:

1
yum install -y php71w php71w-curl php71w-common php71w-cli php71w-mysql php71w-mbstring php71w-fpm php71w-xml php71w-pdo php71w-zip php71w-gd php71w-mcrypt php71w-soap php71w-xmlrpc

安装MySQL5.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 安装MySQL源
yum install https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

# 安装MySQL
yum install mysql-community-server

# 启动MySQL并设置开机启动
systemctl start mysqld
systemctl enable mysqld

# 找到随机生成的密码
grep ''A temporary password is generated for root@localhost' /var/log/mysqld.log |tail -1
# 执行下面的命令,设置MySQL(需要上面的密码)
mysql_secure_installation

# 执行mysql -u root -p,使用上面的密码连接成功,MySQL安装完成。

参考资料

  1. http://www.ehowstuff.com/how-to-install-and-update-openssl-on-centos-6-centos-7/
  2. http://www.restran.net/2017/01/24/nginx-letsencrypt-https/
  3. https://imququ.com/post/my-nginx-conf.html
  4. https://www.joomlagate.com/index.php?option=com_content&view=article&id=325&Itemid=19
  5. http://www.zkt.name/centos-7-an-zhuang-phpkai-fa-huan-jing/