VPS安装、配置博客程序小记

V

安装Nginx

apt install nginx

安装结束后,使用ip访问,即可弹出nginx默认页。

安装Mysql(Mariadb)

以安装5.5.9为例。

apt install mysql-server

此时,在Webmin左侧边栏最下方点击“刷新模块”,刷新后可在侧边栏“服务器”一栏看到Mysql的有关设置。在这里可新建、管理、删除数据库,可操作数据库的授权用户等。这至关重要,安装博客程序时,需要这些信息。

安装PHP

从上面的图中可以知道,Debian 9.5稳定版的软件仓库中,php版本为7.0。

apt install -y php7.0 php7.0-fpm php7.0-cli php7.0-common php7.0-mbstring php7.0-gd php7.0-intl php7.0-xml php7.0-mysql php7.0-zip php7.0-json php7.0-curl

如需查看已安装php版本,可执行

php -v

查看php7.0-fpm是否运行

service php7.0-fpm status

程序配置

下面进入繁琐、闹心的配置过程。

假设您的域名是domainexample.com,VPS默认普通用户是username,ip地址为23.231.232.233。此处仅作为举例,实际安装过程中,请务必使用真实数据。

DNS

首先,请在DNS服务商那里,将你的域名指向VPS的IP地址。DNS全球生效的时间大约在10分钟~72小时,取决于DNS服务商的服务质量及你的网络情况。如果着急,可修改本机hosts文件,将域名直接指向该IP地址。具体修改方法,针对不同操作系统而有所区别。

假如您的物理机使用的Linux系统,安装的是Mate桌面,可以使用pluma打开hosts文件。如果您使用的Gnome或Unity桌面,可以使用gedit。如果您使用的KDE桌面,可使用Kedit(好像是这个名字,很久没用KDE了有点忘了)。以pluma文本编辑器为例,切换到root用户,执行

pluma /etc/hosts

加入

23.231.232.233 domainexample.com

保存并关闭,重启系统。

Nginx

VPS由于不具有图形化界面,为保证操作简便,这里使用nano作为命令行界面的文本编辑器,不建议使用vi或vim。但如果您偏爱vi或vim,请嘴下留情,谢谢。

nginx的配置相当麻烦。

在普通用户主目录新建文件夹,文件夹名字为“domainexample.com”,便于识别。后面网站文件全部上传到这里。可在Webmin面板的File Manager里操作。

新建主机配置文件:

nano /etc/nginx/conf.d/domainexample.conf

写入如下内容:

log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

server {
    listen       80;
    server_name  domainexample.com www.domainexample.com;
    access_log  /var/log/nginx/host.access.log  main;

    root   /home/username/domainexample.com;
    index  index.php index.html index.htm;

    location / {
        try_files $uri $uri/ = 404;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

请根据你实际的域名、目录位置和php版本,修改上述代码。

用下面命令查看是否配置正确。

nginx -t

如果出现下列内容,配置就是正确的。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx:

service nginx restart

检测是否配置成功,方法如下。

在/home/username/domainexample.com下新建文件info.php,写入下列内容:

<?php
phpinfo();

打开桌面浏览器,访问domainexample.com/info.php,如果出现php信息列表页面,则访问正常。

如果出现错误,可查看如下配置文件:

  • /var/log/nginx/host.access.log
  • /var/log/nginx/error.log

根据配置文件内提示的错误内容进行排查。

SSL证书

使用Neilpang的脚本安装。此处使用文件验证,申请的证书为DV型。如需申请免费的通配符证书,或使用DNS验证,请参考官方文档进行操作。

curl https://get.acme.sh | sh

新建目录。

mkdir -p /home/username/domainexample.com/.well-known/acme-challenge

将下列代码,添加到nginx个人配置文件最后一个“}”前面。

location /.well-known/acme-challenge/ {
    alias /home/username/domainexample.com/.well-known/acme-challenge;
}

重启nginx服务,命令在本文前面有。

此时需要重启一下VPS,使acme.sh命令生效,否则执行时会出现命令不存在的情况。可以在主机商网站后台管理页面重启,也可以在VPS内执行

reboot

重启。重启时,SSH会被断开连接,等待1~3分钟后重新连接即可。

申请证书。最后一个参数是采用椭圆函数加密,具体啥叫椭圆函数我也不知道,反正申请下来是ECC的证书,不是传统的RSA。

acme.sh --issue -d domainexample.com -d www.domainexample.com -w /home/username/domainexample.com --keylength ec-384

签发后的证书在~/.acme.sh/domainexample.com_ecc/ 下面。准备把证书安装到 /etc/nginx/ssl/domainexample.com。

新建目录,授予权限:

mkdir -p /etc/nginx/ssl/domainexample.com
chmod 777 /etc/nginx/ssl/domainexample.com

安装证书:

acme.sh --install-cert -d domainexample.com \
--cert-file /etc/nginx/ssl/domainexample.com/cert \
--key-file /etc/nginx/ssl/domainexample.com/key \
--fullchain-file /etc/nginx/ssl/domainexample.com/fullchain \
--reloadcmd "systemctl reload nginx.service" --ecc

生成一个 4096 位的 dhparam 文件。如果没有这个文件,安全性会降低。

这个过程时间很长,需要等待。为了保证SSH操作正常稳定,建议使用screen执行。

screen -S name
cd /etc/nginx/ssl
openssl dhparam -out dhparam.pem 4096

执行过程中,先按住键盘上的Ctrl,再依次按a d,即可退出screen,此时不影响上述命令执行,即便关闭SSH连接也不影响。你可以过一会儿重新连接SSH,然后执行

screen -r name

即可回到这个操作过程页面。

确认nginx和openssl版本:

root@ip:~# nginx -v
nginx version: nginx/1.10.3
root@ip:~# openssl version
OpenSSL 1.1.0f  25 May 2017

Mozilla SSL Configuration Generator生成配置文件,需要用到版本信息。推荐Modern方式,不要开启HSTS,否则可能出现nginx服务启动失败。

此时,根据需要编辑nginx配置文件。

log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name  domainexample.com www.domainexample.com;
    access_log  /var/log/nginx/host.access.log  main;

    # certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
    ssl_certificate /etc/nginx/ssl/domainexample.com/fullchain;
    ssl_certificate_key /etc/nginx/ssl/domainexample.com/key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;

    # intermediate configuration. tweak to your needs.
    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    add_header Strict-Transport-Security max-age=15768000;

    # OCSP Stapling ---
    # fetch OCSP records from URL in ssl_certificate and cache them
    ssl_stapling on;
    ssl_stapling_verify on;

    ## verify chain of trust of OCSP response using Root CA and Intermediate certs
    ssl_trusted_certificate /etc/nginx/ssl/domainexample.com/fullchain;

    resolver 8.8.8.8;

    root   /home/username/domainexample.com;
    index  index.php index.html index.htm;

    location / {
        try_files $uri $uri/ = 404;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
    location /.well-known/acme-challenge/ {
        alias /home/username/domainexample.com/.well-known/acme-challenge/;
    }
}

配置好后,去SSL Labs检测,结果是A+。

安装Perl-Fastcgi

如果使用WordPress、Typecho且不使用perl模块,这个步骤可以省略。如果使用MT,则需要进行这个步骤。

apt install libfcgi-perl spawn-fcgi fcgiwrap

然后执行

service fcgiwrap start

将如下内容写入nginx个人配置文件,注意写在最后一个“}”前面:

location ~ \.pl|cgi$ {
   fastcgi_pass  unix:/var/run/fcgiwrap.socket;
   fastcgi_index index.pl;
   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
   include fastcgi_params;
   }

保存,重启nginx服务。

检测是否配置成功,方法如下。

在/home/username/domainexample.com下,新建index.pl,写入如下内容:

#!/usr/bin/perl

print "Content-type:text/html\n\n";
print <<EndOfHTML;
<html><head><title>Perl Environment Variables</title></head>
<body>
<h1>Perl Environment Variables</h1>
EndOfHTML

foreach $key (sort(keys %ENV)) {
    print "$key = $ENV{$key}<br>\n";
}

print "</body></html>";

赋予权限,否则会出现403错误:

chmod a+x index.pl

在浏览器中访问该文件。如果可正常访问,则配置成功。

19 条评论

作者: 林海