ubuntu服务器从零开始搭建
1. nginx 安装及配置
1.1. 安装
sudo apt-get update
sudo apt-get install nginx -y
1.2. 配置
cd /etc/nginx/sites-enabled/xxxx
upstream test_com_http_server {
server 127.0.0.1:xxxx fail_timeout=0;
}
server {
listen 443 ssl http2;
ssl_certificate /home/one/xxxx/config/ssl/server.crt;
ssl_certificate_key /home/one/xxxx/config/ssl/server.key;
server_name xxxx.com;
root /home/one/xxxx/html;
index index.html index.htm;
charset utf-8;
location ~* ^/api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X_FORWARDED_PROTO $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
client_max_body_size 10M;
proxy_pass http://test_com_http_server;
}
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_types
application/atom+xml
application/javascript
application/json
application/ld+json
application/manifest+json
application/rss+xml
application/vnd.geo+json
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/bmp
image/svg+xml
image/x-icon
text/cache-manifest
text/css
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy;
}
1.2.1 字体跨域
location ~* \.(eot|ttf|woff|woff2|svg|otf)$ {
add_header Access-Control-Allow-Origin *;
}
1.3. 配置 Let’s encrypt
Ubuntu 20.xx.xx 以下的
-
添加存储库
sudo add-apt-repository ppa:certbot/certbot
如果报错
sudo apt-get install software-properties-common
-
安装 nginx 软件包
sudo apt install python-certbot-nginx
Ubuntu 20.xx.xx 以上的
- 安装 certbot 和 python3-certbot-nginx
sudo apt install certbot python3-certbot-nginx -y
接下来都是一样的
-
确认 nginx 成功运行
-
获取 ssl 证书
sudo certbot --nginx
-
自动续订
sudo certbot renew --dry-run
/usr/bin/certbot /usr/sbin/nginx
2. node
1. node环境安装
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
2. 主要包安装
-
Yarn | npm 安装
sudo npm i -g yarn sudo npm i -g cnpm
安装完了之后
sudo chown -R $USER:$GROUP ~/.npm sudo chown -R $USER:$GROUP ~/.config
-
pm2 安装
sudo npm i -g pm2
-
pm2 使用
pm2 start app.js -o ./log/out.log -e ./log/err.log NODE_ENV=production pm2 reload
-
pm2 fork
module.exports = { apps : [{ name: 'xxxx', script: 'app.js', autorestart: true, watch: false, max_memory_restart: '1G', out_file: './log/out.log', error_file: './log/err.log' }] };
-
pm2 cluster
module.exports = { apps : [{ name: 'xxxx', script: 'app.js', instances: 'max', autorestart: true, watch: false, max_memory_restart: '1G', exec_mode: 'cluster', out_file: './log/out.log', error_file: './log/err.log' }] };
3. Postgres sql 最新版数据库安装
- 安装
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql
-
进入管理员账号
sudo -u postgres psql
-
创建用户
CREATE USER username WITH PASSWORD '****';
-
创建数据库
CREATE DATABASE databasename OWNER username
-
赋予创建用户数据库权限
如果上一步指定了
OWNER
则可以省略GRANT ALL ON DATABASE databasename TO username
4. Redis 最新版安装
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis
5. GitHub 配置
cd ~/.ssh
vi id_rsa
chmod 600 id_rsa
ssh -T git@github.com
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"