システムを更新し、git、vim、curl をインストールする
yum update -y
yum install curl git -y
nvm経由でNode.jsをインストールする
nvmをインストールする
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
インストールが成功したことを確認する
source ~/.bashrc
nvm --version
出力されたバージョン情報 0.33.5 は、インストールが成功したことを意味します。
MySQL 5.7 をインストールする
ダウンロードとインストール
yum install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm -y
yum install mysql-community-server -y
mysqlを起動する
systemctl start mysqld
systemctl enable mysqld
root の初期パスワードを見つける
cat /var/log/mysqld.log | grep password
パスワードを変更する
mysql -uroot -p
Enterを押した後、見つかったパスワードを入力すると、ログインが成功します。
ノードプロジェクトのデプロイ
プロジェクトのソースコードをダウンロードする
mkdir /var/www
cd /var/www
git clone https://gitee.com/icjs-cc
ThinkJS コマンドをグローバルにインストールする
npm install -g think-cli
thinkjs -V
依存関係をインストールする
cd /var/www/test
npm install
データベースを作成してデータをインポートする
mysql -uroot -p -e "create database test character set utf8mb4"
mysql -uroot -p nideshop < /var/www/test/test.sql
プロジェクトデータベース構成を変更する
vim src/common/config/database.js
修正後
const mysql = require('think-model-mysql');
module.exports = {
handle: mysql,
database: 'test',
prefix: 'test_',
encoding: 'utf8mb4',
host: '127.0.0.1',
port: '3306',
user: 'root',
password: '你的密码',
dateStrings: true
};
エンコーディングとプレフィックスの値に注意してください
プロジェクトをコンパイルする
npm run compile
node production.js
別の端末を開いて起動が成功したかどうかを確認します
curl -I http://127.0.0.1:8080/
HTTP/1.1 200 OK が出力され、成功を示します。実行を停止するには Ctrl + C を押します
PM2管理サービスを利用する
pm2のインストールと設定
npm install -g pm2
プロジェクトのルート ディレクトリ内の pm2.json を次のように変更します。
vim pm2.json
変更された内容:
{
"apps": [{
"name": "test",
"script": "production.js",
"cwd": "/var/www/test",
"exec_mode": "fork",
"max_memory_restart": "256M",
"autorestart": true,
"node_args": [],
"args": [],
"env": {
}
}]
}
サーバー構成が高い場合は、max_memory_restart とインスタンスの値を適切に調整できます。
午後2時開始
pm2 start pm2.json
アクセスを確認する
curl -I http://127.0.0.1:8080/
nginxをリバースプロキシとして使用する
yum install nginx -y
systemctl start nginx.service
systemctl enable nginx.service
ローカルエリアに正常にアクセスできるかテストする
curl -I localhost
nginx設定を変更する
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
vim /etc/nginx/nginx.conf
内容は以下の通りです(サーバー内の内容を変更するだけです)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name nideshop.com www.test.com; # 改成你自己的域名
root /var/www/test/www;
set $node_port 8080;
index index.js index.html index.htm;
if ( -f $request_filename/index.html ){
rewrite (.*) $1/index.html break;
}
if ( !-f $request_filename ){
rewrite (.*) /index.js;
}
location = /index.js {
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:$node_port$request_uri;
proxy_redirect off;
}
location ~ /static/ {
etag on;
expires max;
}
}
}
nginx を再起動し、nginx に引き続き正常にアクセスできるかどうかを確認します。
nginx -t
systemctl restart nginx.service
curl http://127.0.0.1/
インターフェイス データが返された場合、操作が成功したことを意味します。
注: Alibaba Cloud は、デフォルトでは外部ネットワークからポート 80/443 にアクセスできません。インスタンスのセキュリティ グループ設定を変更してください。設定チュートリアル
https アクセスを構成する
証明書ボットをインストールする
yum install epel-release -y
yum install certbot-nginx -y
certbot --nginx
certbot -nginx のこのステップでエラーが発生した場合は、次のコマンドを実行します。
pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3
certbot --nginx を再実行します。詳細ドキュメント
certbot renew --dry-run
https を使用したブラウザ アクセスが成功するかどうかをテストする