进入/usr/local/nginx/conf/vhost
这个目录(可以复制粘贴到地址栏里,敲回车键直接进入),新建一个域名配置文件,你也可以在PC本地创建这个文件,再上传到服务器里。这里以我的域名为例,如www.himiku.com.conf
。
用编辑器(推荐使用 Visual Studio Code)打开此文件,进行下面的操作。
一般配置
一开始建站,肯定是什么都不懂的。关于SSL证书可以自行搜索其作用,总之先把博客与域名链接起来再说。
把下面的代码复制粘贴进去,修改第5行域名配置、第7行博客程序所在路径配置、以及第35行内的域名。
server
{
listen 80;
#listen [::]:80;
server_name www.himiku.com himiku.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/blog;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include rewrite/typecho.conf;
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log /home/wwwlogs/www.himiku.com.log;
}
其中,第13行include rewrite/typecho.conf;
是伪静态的内容,加上之后可以去掉Typecho地址栏中的index.php
。
然后进入ssh控制台,输入以下命令重启 Nginx
lnmp nginx restart
添加 SSL 证书
为博客加上https
,需要添加SSL证书。这玩意具体自查,直接上教程。
部署证书签发服务
有关更详细的acme.sh
的使用方法需要参考官方教程。
安装acme.sh
curl https://get.acme.sh | sh
使用dnsapi
的方式验明域名所有权,同时可以极大地方便续签的操作,这里以 Dnspod (腾讯云)为例,有关dnsapi
的说明需要参考官方教程,基本上都差不多。
export DP_Id="123456"\
export DP_Key="sADDsdasdgdsf"
签发泛域名证书,以本站域名为例,请修改成自己的域名。
acme.sh --issue --dns dns_dp -d '*.himiku.com' -d himiku.com
签发的证书一般是默认生成在root/.acme.sh/
下,为安全起见,不要直接使用这里的证书,而是复制到/usr/local/nginx/conf/ssl
目录,只是需要手动新建ssl
和域名文件夹,
acme.sh --installcert -d '*.himiku.com' --key-file /usr/local/nginx/conf/ssl/himiku.com/himiku.com.key --fullchain-file /usr/local/nginx/conf/ssl/himiku.com/fullchain.cer --reloadcmd "lnmp nginx restart"
如要申请ECC证书,只要在末尾添加 --keylength ec-256
acme.sh --issue --dns dns_dp -d '*.himiku.com' -d himiku.com --keylength ec-256
复制证书只要加个--ecc
,但是域名部分也要做相应的变更。如果害怕出错,请直接照着我的来。
acme.sh --installcert -d '*.himiku.com' --ecc --key-file /usr/local/nginx/conf/ssl/himiku.com/himiku.com_ecc.key --fullchain-file /usr/local/nginx/conf/ssl/himiku.com/fullchain_ecc.cer --reloadcmd "lnmp nginx restart"
证书在 60 天以后会自动更新, 无需任何操作. 今后有可能会缩短这个时间, 不过都是自动的。但还是可以用这个命令检查
acme.sh --renew-all
同时建议开启自动更新
acme.sh --upgrade --auto-upgrade
之后,还需要生成一个dhparam.pem
文件,增加安全性。
openssl dhparam -dsaparam -out /usr/local/nginx/conf/ssl/dhparam.pem 4096
Nginx 配置文件修改
在证书签发成功后,需要修改配置文件,强制使用https
。直接替换原来没有使用证书的文件,填入以下内容。同样修改域名,有些地方需要解释下。
- 第5行的作用是,访问
http://himiku.com
或http://www.himiku.com
时,会一律跳转到https://himiku.com
。 - 第11-13行的作用是,如果访问
https://www.himiku.com
时,会强制跳转到http://himiku.com
。 - 第17-19行是证书的部分,请根据自己的证书位置及名称修改。
- 第26行的作用是启用HSTS,具体作用见:小站已加入HSTS Preload List。若不需要可以删除。
server {
listen 80;
listen [::]:80;
server_name himiku.com www.himiku.com;
return 301 https://himiku.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name himiku.com www.himiku.com;
if ($host = 'www.himiku.com') {
return 301 https://himiku.com$request_uri;
}
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/blog;
ssl_certificate /usr/local/nginx/conf/ssl/himiku.com/fullchain.cer;
ssl_certificate_key /usr/local/nginx/conf/ssl/himiku.com/himiku.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
include rewrite/typecho.conf;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\. {
deny all;
}
access_log /home/wwwlogs/himiku.com.log;
}
关于数据库
因为 LNMP 的官方添加虚拟主机的教程里有一步是添加同名数据库,如果直接照我的方法来的化,相当于直接跳过了,因此需要手动创建数据库。
如果你刚安装完 LNMP,访问http://你的ip/phpmyadmin/
,进入 phpMyAdmin 的登陆页面,填入用户名root
,密码是你安装 LNMP 时设置的。
建议重命名 phpMyAdmin
这个文件夹,让别人猜不到是啥。
然后点击左侧新建
,左边填写数据库名称,如blog
,右边选择utf8mb4_general_ci
,点击创建即可。
关于博客安装完毕的数据库配置,我会在其他文章中说明。这里先咕了。
本文作者:mikusa
本文链接:https://www.himiku.com/archives/add-vhost.html
版权声明:所有文章除特别声明外,均系本人自主创作,转载及引用请联系作者,并注明出处(作者、原文链接等)。