- Configure github/bitbucket ssh key.
I assume you’re familiar with git clone by ssh. Probably like lots of developers, you will use your own private key. I highly diagree with that. Too dangerous! Your ssh private key can unlock lots of precious treasures, far more than this git repo. Right?
How to improve? Create a readonly git deploy key in github/bitbucket repo setting. To do that, we need create a new ssh key pair. So it’s new and isolated. And git deploy key has a nice limitation: we can’t use the same deploy key for multiple repos. It adds extra security.
For simplicity or reasonable security compromise, some people may still use their private key. I admit, it makes sense to some extent. Then protect your ssh key with passphrase. It’s easy and simple.
For simplicity, here we use root to checkout code and setup website. To improve the security, try with an ordinary OS user.
# Update ssh key to below
vim /root/git_deploy_key
# ssh private key can't be widely open
chmod 400 /root/git_deploy_key
- Specify ssh key for git clone
vim /root/.ssh/config
host github.com
HostName github.com
StrictHostKeyChecking no
User git
IdentityFile /root/git_deploy_key
- Run git clone to get the repo.
# Run with correct parameters.
cd /var/www/
git clone git@github.com:XXX/denny_www.git
- Create crontab for periodical git pull. Thus no need for manul login and update anymore.
crontab -e
# Change file path
*/10 * * * * cd /var/www/denny_www; git pull
1.3 For Static Websites, Create Apache Vhost
For static websites of plain html, we can don’t need to run it in our own server. It can be done by Github Pages.[1] See more discussion here.[2]
Well, Github Pages only solves 80% of the problem. If you’re in China, the accessibility of Github is not gurantee. Or you want to use your own domain, instead of XXX.github.io. Use apache2 or nginx to serve the requests.
- Apache vhost example. And nginx is pretty similar.
# Install apache
apt-get install -y apache2
# Define vhost
vim /etc/apache2/sites-enabled/my-static.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy.test.com
DocumentRoot /var/www/denny_www
<Directory '/var/www/denny_www'>
Options All
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/apache2/my-error.log
CustomLog /var/log/apache2/my.log common
</VirtualHost>
# Reload apache
service apache2 reload
1.4 For Dynamic Websites, Use Apache For HTTP Reverse Proxy
Let’s say your dynamic website listens on port 8082. And you want to open by http://www.mytest.com or http://mytest.com.
- Configure DNS setting properly or /etc/hosts is properly changed.
- Enable apache modules for http reverse proxy
a2enmod proxy
a2enmod proxy_http
# Restart apache to load modules
service apache2 restart
vim /etc/apache2/sites-enabled/dynamic.conf
<VirtualHost *:80>
ServerName www.mytest.com
ServerAlias mytest.com
ServerAdmin webmaster@smallco.example.com
ErrorLog /var/log/apache2/dynamic_error_log
TransferLog /var/log/apache2/dynamic_access_log
DocumentRoot /var/www
ProxyPass / http://127.0.0.1:8082/
ProxyPassReverse / http://127.0.0.1:8082/
</VirtualHost>
- If necessary, configure firewall to allow the traffic.
ufw allow 80/tcp
# Some URI might has port number attached
ufw allow 8082/tcp
curl -I http://www.mytest.com
1.5 Add Password Protection To Your Existing Websites
Sometime you may only want certain people to visit your websites. With apache htpasswd[3], we can create username-password pair easily. People will need to input the credential first in web browser, before the actual pages load.
- Create an user credential
apt-get install -y apache2-utils
# create password file for a new user
/etc/apache2/.htpasswd $username
- Update vhost setting to enforce the authentication
From:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html">
</Directory>
</VirtualHost>
To:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
</VirtualHost>
I would set up a general user account and dir for this rather than using /root good article otherwise
Rob, I definitely agree with you. We should avoid root as much as possible.
Let me update the article, per your feedback.