summary

apache2 + nodejs + mysql + vue

前端

前端用的是 vue-cli 工具生成的带有 webpack 开发依赖的 vue 项目,所以需要 npm run build 进行编译后拷贝到 /var/www/rootDir/to/doc

新建站点配置

1
2
cd /etc/apache2/sites-available
vi www.myWebsite.com.conf

编辑站点配置

1
2
3
4
5
6
7
8
<VirtualHost *:80>
ServerAdmin admin@myWebsite.com
ServerName www.myWebsite.com
ServerAlias www.myWebsite.com
DocumentRoot /var/www/rootDir/to/doc
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

启动站点配置

1
2
a2ensite www.myWebsite.com
service apache2 reload

代码仓库

1
2
cd /srv
git init --bare frontEndCodeRepo.git

本地获取代码

1
git clone ssh://userName@ip:port/srv/frontEndCodeRepo.git

git hook 更新代码

1
2
cd /srv/frontEndCodeRepo.git/hooks/
vi post-receive

post-receive:

1
2
3
cd
unset GIT_DIR
~/updateFrontEndCode.sh

~/updateFrontEndCode.sh:

1
2
3
4
5
6
7
8
9
cd ~/project/frontEnd
git pull origin master
npm install
npm run build
cd /var/www/rootDir/to/doc
rm index.html
rm -r static
mv ~/project/frontEnd/dist/index.html .
mv ~/project/frontEnd/dist/static .

后端

后端用的是 express 框架,假定 express 监听了 10086 端口,这里将发送到 api.myWebsite.com 的请求都交给 express 处理。

站点配置

/etc/apache2/sites-available/api.myWebsite.com.conf

1
2
3
4
5
6
7
8
9
10
11
<VirtualHost *:80>
ServerAdmin admin@myWebsite.com
ServerName api.myWebsite.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://127.0.0.1:10086/
ProxyPassReverse / http://127.0.0.1:10086/
</VirtualHost>

git hook 更新代码

1
2
3
4
5
cd ~/project/endPoint
git pull origin master
kill -9 $(lsof -ti :10086)
npm install
node index.js >> output &

数据库

创建数据库之后,将所有权限赋给一个新建的用户名是 fooUser 的用户。

1
2
3
create database foo;
create user 'fooUser'@'%' identified by 'aStrongPassword';
grant all on foo.* to 'fooUser';