部署Nginx服务与基本配置

个人站点原部署在GitHub,借助其GitHub-Pages功能实现免费的个人站点部署。后考虑到VPS资源闲置,加上GitHub-Pages貌似不能用https,就想迁移过去玩玩。

当然,实现方法有很多,我选的是用Nginx + Hexo部署。

环境说明

  • 操作系统:Ubuntu 16.04.2 LTS (xenial)
  • Nginx version : nginx/1.12.2 built with OpenSSL 1.0.2g

Nginx相关

Nginx安装与配置。

服务搭建

Ubuntu 有对应apt安装包,但需配置。

1.获取Nginx认证签名

1
wget http://nginx.org/keys/nginx_signing.key

2.将该key添加到apt中

1
sudo apt-key add nginx_signing.key

3.添加nginx软件源

For Ubuntu replace codename with Ubuntu distribution codename, and append the following to the end of the etc/apt/sources.listfile:

1
2
deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

4.更新源并安装nginx

1
2
apt-get update
apt-get install nginx

5.启动nginx

1
systemctl start nginx

6.默认nginx配置文件路径

/usr/local/nginx/conf,/etc/nginx, or/usr/local/etc/nginx

配置Nginx

编辑 /etc/nginx/conf.d/xx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80;
server_name binlv.top;
#实现访问http的时候自动跳转到https
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name binlv.top;

#不输出 Nginx 版本号及其他错误信息
server_tokens off;

#设置根目录,必须为已存在目录
location / {
root <--路径-->;
index index.php index.htm index.html;
}
}

重启nginx

1
systemctl restart nginx

使nginx开机启动

1
systemctl enable nginx

参考

本文完。

© 2019 lvbin's Blog All Rights Reserved.
Theme by hiero