This article was first published on my personal blog Orxing. Top Welcome to visit. The server is Ali Cloud ECS CentOS, which was originally used to deploy WordPress. Recently, however, I found that Coding Pages were dying, so I decided to redeploy Hexo back to the server

basis

  • Generate static files using Hexo
  • Build a Git environment on the cloud host and upload static files to the cloud host through Git
  • Use Git-hooks for automatic deployment
  • Nginx is used as a static file server to achieve external access to blogs

The environment

  • The local desktop is Win10, and the terminal used is cmder. Cmder comes with Git. In theory, bash is the same

  • The server is CentOS 7 64-bit and does not use the pagoda panel by default


start


Logging In to the Server

$SSH [email protected]Let me tell you a big secret, this is baidu's IP address.
# Login password is the password you set when you get the server
Copy the code

Install Git and nginx

$ yum install git
$ yum install nginx
Copy the code

Add a Git user

$ adduser git   Add git user
$ chmod 740 /etc/sudoers   # change the permission of the sudoers file to writable by the file owner
$ vim /etc/sudoers   #vim is a powerful editor that uses Google's own methods
Copy the code

Find root ALL=(ALL) ALL and add a line below it

git ALL=(ALL) ALL
Copy the code

$ chmod 400 /etc/sudoers   Change its permissions to be readable by the file owner
$ sudo passwd git  Git user password
Copy the code

Add SSH keys to git users

If you’ve used github or coding before, you probably know what this key means, but I won’t go into that. Okay

$ su git	Switch to the git user
$ mkdir ~/.ssh	# create.ssh folder
$ touch ~/.ssh/authorized_keys	Create authorized_keys file
$ chmod 600 ~/.ssh/authorzied_keys	Authorized_keys authorized_keys authorized_keys
$ chmod 700 ~/.ssh	# grant read, write, and execute permissions to the.ssh folder owner
$ vim ~/.ssh/authorized_keys	Paste in the SSH key
Copy the code

Shut down the terminal, log in to the server again using SSH [email protected], and check whether you can log in to the git user without password

Create a Git repository and use Git-hooks for automatic deployment

$ sudo mkdir /var/repo	  This is the location of the Git repository
$ sudo mkdir /var/www		  #
$ sudo mkdir /var/www/hexo  This is the directory for the blog source files
Copy the code
$ cd /var/repo	Go to the git repository folder
$ sudo git init --bare blog.git	Create a repository called blog
$ sudo vim /var/repo/blog.git/hooks/post-update	
Copy the code

/ var/rebo/blog.git: checkout -f from /var/www/hexo, which is the result of a push from git-hooks. /var/ WWW /hexo After each push, we can update the deployment directory to the latest status of the blog. I’m a little confused here

#! /bin/bash
git --work-tree=/var/www/hexo --git-dir=/var/repo/blog.git checkout -f
Copy the code

And then give permissions

$ cd blog.git/hooks/
$ sudo chown -R git:git /var/repo/   	Change the owner of the folder and its subfiles
$ sudo chown -R git:git /var/www/hexo  Git :git
$ sudo chmod +x post-update  # give it executable permission
Copy the code

In fact, there is another way, that is, after push, delete the previous file and clone the repository to /var/www/hexo. You can try it yourself, and the link is here

Configure nginx

$ sudo nginx -t  # View nginx configuration path, pagoda panel install nginx not the following path
$ sudo vim /etc/nginx/nginx.conf
Copy the code

Listen, server_name, root Listen is the port, server_name is your domain name orxing.top, root is the blog source file path /var/www/hexo

If the domain name is not registered, port 80 cannot be used and server_name cannot be added. The port can be changed to any port. If your port 80 is occupied, you can also change it to another port

Domain name is registered, fill in the domain name, and go to the domain name console to resolve the domain name to your server IP address

Because ali cloud server does not open port 80 by default, so you must go to open, other ports are the same

Set permissions for git users

You can not use SSH [email protected] or su git to switch to user git.

Git operations are not affected

$ sudo vim /etc/passwd
# Change the last line
# change /bin/bash to /usr/bin/git-shell
Copy the code

Refer to the link

  • Ali Cloud ECS environment to build a static blog platform based on Hexo+Git+Nginx
  • Hexo build technology blog deployed on Ali Cloud server tutorials
  • How does Git execute instructions against a Repository in another path