Recently bought the cloud server and domain name of Ali cloud, oneself toss over and over again build blog. Here I set up the process record to share!

1. Local computer

1.1 installation node. Js

Go to the NodeJS official website and install the Long Term Support (LTS) version.

Press Win +R and enter CMD to go to the CMD command line tool. Enter node -v to view the Node version.

If the version information is displayed, the node is successfully installed.

1.2 installation hexo

Run NPM install hexo-cli -g on the COMMAND-LINE interface (CLI) to install hexo. After the installation is complete, run the hexo -v command to view the version information.

  • Initialize the root directory

    Create a folder hexo-blog, go to the directory on the CMD command line, and type Hexo init to initialize the root directory.

  • The local view

    Type hexo g&&hexo s (generate static file and start local server)

    Follow the prompts and open http://localhost:4000 in your browser to see the initial blog page.

  • beautify

To their own blog to change the style, beautify, add and delete functions, publish articles and other operations, will not expand the introduction.

Buy a domain name

  • Buy a domain name

    Purchased the domain name lzxjack.top on Aliyun with a student authentication discount.

  • The domain name registration

    The domain name is bound to the domestic server, you must carry out the record operation, otherwise it will not be able to access.

    It took less than 10 days for the registration number to come down from the submission of archival information to the approval of the administration bureau.

  • Domain name resolution

    Domain name resolution is to point the domain name to the site space IP, so that people can easily access to the site through the registered domain name of a service. An IP address is a digital address that identifies a site on the network. In order to facilitate memory, a domain name is used instead of an IP address to identify the site address.

    • In the host record,wwwIndicates the prefix of the domain namewww., such aswww.lzxjack.top.@It representslzxjack.topSo that both addresses can access the server
    • Record typeAIt means to point the domain name to aIPV4address
    • The recorded value is the public IP address of the server

Third, server side

3.1 Purchasing a Server

To enable other users to access their own blogs on their own clients, you need to deploy the blogs on the server. Other users can access the resources on the server using the public IP address of the server. If a domain name is bound, you can access the domain name.

Purchased a lightweight application server through aliyun developer growth plan. As a server system, CentOS is stable and powerful. Therefore, CentOS 7.3 of Linux is selected as the system image.

3.2 Configuring Ports

Click Firewall on the Cloud Server management page and then click Add Rules.

To access the server using the HTTP protocol, you need to listen on port 80 on the server, because port 80 is the default port number of the server. By default, Ali Cloud server closes port 80, so we need to manually open and listen to port 80. In addition, when Git is configured on the server, SSH protocol needs to be used to connect the local server to the server, and the server needs to listen on port 22, which also needs to be manually opened.

3.3 Setting up the Server Environment

This step needs to be done on the cloud server using either a remote connection or an Xshell client.

  • Install nginx

    1. Switch to the root account

      sudo su root
      Copy the code

    2. We need to use Nginx as the Web server, so we need to install nginx first. You can install directly using the yum command.

      yum install -y nginx
      Copy the code

    3. After the installation, start the Nginx server

      systemctl start nginx
      Copy the code
      systemctl enable nginx
      Copy the code

  • Create a blog root directory

    To put the blog page file in /home/www/website/, you need to create these files first.

    cd /home
    mkdir www
    cd www
    mkdir website
    Copy the code

    View the created file:

  • Configure the NGINx route

    After the root directory of the blog is established, you need to point the Nginx server to the root directory to access the blog page. Therefore, you need to modify the nginx configuration file.

    The default configuration file of Ali Cloud is nginx based on Fedora. The default configuration file is nginx.conf under etc/nginx/.

    To view the default nginx configuration file:

    cd ~
    cd /etc/nginx
    ls
    Copy the code

    Nginx. conf is the default configuration file.

    Instead of modifying the nginx configuration file directly, you create a new folder and write your own configuration in the new folder. Use include to import the folders into the nginx.conf configuration file. In this way, if there is a new requirement, you only need to add the configuration file of the new requirement to the folder, and the configuration file nginx.conf will not be modified again, improving efficiency.

    Switch to the /etc/nginx/directory and create a folder vhost in this directory:

    cd /etc/nginx
    mkdir vhost
    cd vhost
    Copy the code

    Enter vim blog.conf to create a blog.conf file and edit the content:

    server{
            listen    80;
            root /home/www/website;
            server_name lzxjack.top;
            location /{
            }
    }
    Copy the code

    Listen indicates listening on port 80. Root is the root directory of the blog, where the page is stored. If server_name is the server name, enter lzxjack.top and bind the domain name to the root directory of the blog page.

    To make the blog page accessible to http://www.lzxjack.top/, create a new configuration file wwwblog.conf and set server_name to www.lzxjack.top:

    /etc/nginx/vhost there are two configuration files, blog.conf and wwwblog.conf: /etc/nginx/vhost

    Open the nginx.conf file in /etc/nginx/and add the following line of code to import the newly created configuration file. *.conf means to import all files with the.conf suffix in the vhost folder. Note: Write inside the HTTP {}.

    include /etc/nginx/vhost/*.conf;
    Copy the code

    At this point, the nGINx route configuration is complete, and the domain name is bound to the specified path of the cloud server.

3.4 installation node. Js

curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install -y nodejs
Copy the code

After the installation is complete, run node -v and NPM -v. If the version number is displayed, the installation is successful

Configure Git on the server

The main purpose of this part is that the local computer can connect to the cloud server through SSH, and then the blog can be uploaded to the server through the command line.

4.1 install Git

yum install git
Copy the code

4.2 Configuring Git Users

Enter the following to add a Git user:

adduser git
Copy the code

Modifying user rights:

chmod 740 /etc/sudoers
Copy the code

Open the/etc/sudoers:

vi /etc/sudoers
Copy the code

Add a statement at this location:

git ALL=(ALL) ALL
Copy the code

After saving and exiting, change the permissions of the sudoers file back to the original:

chmod 400 /etc/sudoers
Copy the code

Set Git user password:

sudo passwd git
Copy the code

Switch to the git user and create a.ssh folder under the ~ directory

su git
cd ~
mkdir .ssh
cd .ssh
Copy the code

4.3 Configuring Git Keys

Generate a public key file:

ssh-keygen
Copy the code

Press Enter after input!!

There are two files in the directory, id_rsa and id_rsa.pub, where id_rsa.pub is the public key file, copy a copy:

cp id_rsa.pub authorized_keys
Copy the code

In this case, there will be an authorized_keys file in the directory, and change its permissions:

chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
Copy the code

Client generated key:

Enter ssh-keygen -t rsa in the CMD tool on the local PC and press Enter. The key will be generated in the user folder on the local computer:

Next, copy the contents of the id_rsa.pub file on the local computer to !!!! at the end of the authorized_keys file on the cloud server The methods available are:

  • Copy the content directly
  • throughXftpFile transfer tool, first to the local computerid_rsa.pubThe file is transferred to a location on the cloud server and then stored on the cloud serverThe locationperformcat id_rsa.pub >> ~/.ssh/authorized_keysCan be

Then we open CMD on the local computer, use SSH to connect to the cloud server, and type:

SSH -v git@Public IP address of the cloud serverCopy the code

Welcome to Alibaba Cloud Elastic Compute Service! You can log in successfully without entering the password, that is, you have successfully configured the Git key, so you don’t need to enter the Git password when updating the blog deployment in the future!

4.4 Creating a Git Repository

Create a Git repository and create a new post-receive file

cd ~
git init --bare blog.git
vi ~/blog.git/hooks/post-receive
Copy the code

Enter the following:

git --work-tree=/home/www/website --git-dir=/home/git/blog.git checkout -f
Copy the code

Save to exit and grant executable permission to the file

chmod +x ~/blog.git/hooks/post-receive
Copy the code

The server configuration is complete.

Configure Hexo, deploy and publish

After both the local computer and server configuration are complete, in the Hexo root directory on the local computer, type the following to install the plug-in:

npm install hexo-deployer-git --save
npm install hexo-server
Copy the code

Open the _config.yml file and modify the deploy project as follows:

Git branch: master deploy: type: git repo: git@cloud server public IP address :/home/git/blog.git branch: masterCopy the code

In the command, type indicates that the deployment type is git, the repository repo address is git@public IP address of the cloud server :/home/git/blog.git, and branch is the master branch.

After you’ve beautified your blog, added features, and written articles, you can run the hexo Clean command to clear the local cache and the hexo g -d command to generate a static file and deploy it to a remote repository.

At this time, the local blog page has been deployed to the server /home/www/website directory:

After the deployment is complete, if the page is displayed, enter the following information on the cloud server to restart nginx:

nginx -s reload
Copy the code

Here, hexo to build a blog and deploy to ali Cloud server is finally completed!