Recently, I have been thinking about building a personal blog, so I finally chose Vuepress. After several days of suffering, I finally set up the blog locally. Therefore, I plan to deploy the blog to the server, so that I can access it anywhere.
The premise
Buying a server
Buy servers, like Ali Cloud, Tencent cloud, Tianyi cloud, etc., it is best to buy when doing activities (cheap), I bought tianyi cloud (cheaper). Then configure your server, security group should be enabled, system recommended to choose CentOS and Ubuntu. It starts with Ubuntu, too.
Logging In to the Server
Ubuntu is not like Windows where you can directly log in to see the desktop, all using the command line to operate, so the basic Linux naming you have to look at.
Connect to the server remotely using the command:
Open the local terminal and use SSH to log in to the remote server, followed by your server address. Root is the current user name.
$SSH [email protected]Copy the code
Login using tools:
It is recommended to use Xshell and Xftp for connection. Xshell uses shell script commands to connect and operate cloud servers, while Xftp desktop connection and operation are of the same name.
Note: Be sure to check whether your username is root or Ubuntu or whatever.
Installing Common Tools
1. Install Node:
sudo apt-get install nodejs
sudo apt install nodejs-legacy
sudo apt install npm
Copy the code
Update NPM package image source for quick download
sudo npm config set registry https://registry.npm.taobao.org
sudo npm config list
Copy the code
Global install n manager (for managing nodeJS versions)
sudo npm install n -g
Copy the code
Install the latest NodeJS (stable version)
Sudo n stable # check whether the installation is successful node -v NPM -vCopy the code
After node is installed, you can install various dependencies on the server as well as on your own computer
2. Install WebPack
sudo npm install webpack-cli -g
sudo npm install webpack -g
Copy the code
3. Install Git and SSH
sudo apt-get install git
sudo apt-get install ssh
Copy the code
4. Install nginx
Sudo apt-get install nginx sudo apt-get install nginxCopy the code
Installed file location:
/usr/sbin/nginx: main program /etc/nginx: stores configuration files /usr/share/nginx: stores static files /var/log/nginx: stores logsCopy the code
Default HTML file location:
/var/www/html
Copy the code
If you start nginx with sudo nginx, you will probably be able to access your address.
Nginx configuration:
After nginx is installed, the /etc/nginx/nginx.conf file is the basic configuration, we can modify it to add server to start service. Can also by modifying the/etc/nginx/conf. D/default. Conf configuration to open services, it will replace other configuration in the configuration file by default. If there is nothing in /etc/nginx/conf.d, you can create a default.conf file.
Customize the default.conf configuration
Server {listen 3000 default_server; Root /home/gitname/ WWW /blog; Index.htm index.nginx-debian.html; Server_name localhost; Try_files $uri $uri/ =404; }}Copy the code
Nginx basic commands (global)
Start the nginx service
sudo nginx
Copy the code
Restart the nginx service
Sudo nginx -s reloadCopy the code
Quickly stop the nginx service
sudo nginx -s stop
Copy the code
Stop the nginx service
sudo nginx -s quit
Copy the code
Check to see if nginx is running
sudo -aux | grep nginx
Copy the code
Server Git usage
The server pulls the Github code
Once git is installed, if you want to pull the Github repository code from the server, you can configure Git as if it were on your own computer
Configuring personal Information:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Copy the code
Check whether SSH exists:
$ cd ~/ .ssh
$ ls
> authorized_keys id_rsa id_rsa.pub known_hosts
Copy the code
We need to find a pair of files named id_dsa or ID_rsa, one of which has the.pub extension. The.pub file is the public key, and the other is the corresponding private key. If you can’t find such a file (or if you don’t have a.ssh directory at all), you need to create your own SSH.
Create an SSH key:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Copy the code
After entering this command, you do not need to enter anything, but enter directly. Note that the directory address is the address of the key file, usually /root/.ssh.
View the public key:
$ cat ~/.ssh/id_rsa.pub
> ssh-rsa XXXXXXXXXXXX... == Your Email Address
Copy the code
Adding a public key:
After the public key is created, you can add the public key to Github, log in to github, go to Settings, click SSH and GPG keys, then click New SSH key, create any name, and add the public key to github. Save OK.
Multiple public keys can be added to a GitHub account. We can add it to multiple computers we use, and after adding it, we can boldly pull our Github repository code on the server.
Set up your own Git server on the server
By configuring a Git server on your own computer, you can create your own repository on the server, like GitHub, and submit your code directly to the server, so that projects that involve corporate privacy can be put there and deployed automatically.
Create git user:
Sudo adduser gitName # gitName Adding user 'gitName' sudo adduser gitName # gitName Adding user 'gitName' Adding new group `gitname' (1006) ... Adding new user `gitname' (1006) with group `gitname' ... Creating home directory `/home/gitname' ... Copying files from `/etc/skel' ... New password: xxxxxx Retype new password: xxxxxx passwd: Password updated successfully Changing the user information for gitname # or press ENTER for the default Full Name []: Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] yCopy the code
You can see that the git user we created is saved directly to /home.
Add the local public key to the git server, so that you do not need to enter a password every time you commit. (If you have not added the public key, you may get an error when entering a password even if your host is connected to a Git service.)
2. Create.ssh for git server
$sudo touch. SSH /authorized_keys. $sudo touch. SSH /authorized_keysCopy the code
Once the authorized_keys file is created, you can copy the local. Pub public key to authorized_keys, and you can also add public keys for multiple machines.
3. Create a repository
Before creating the repository, you must switch to the current Git user. Otherwise, you will be unable to connect when uploading code.
$sudo su gitname # go to your gitname file. $mkdir bare # go to your gitname file - bare bare. Git # to initialize the new empty warehouse > the Initialized the empty git repository in/home/gitname/bare/bare git /Copy the code
Adding the –bare parameter to the git init command means that when initializing the git repository, do not create a local working directory.
Now remote warehouse created, git repository address is [email protected]: / home/gitname/bare git
All you need to do is associate your Git repository locally
Git init git remote the add server [email protected]: / home/gitname/bare/bare git git add. The git commit - am "server init" git push server masterCopy the code
4. Automatic deployment
Git hooks are scripts that are called when certain points in the git repository are triggered. Hooks allow you to customize related behavior (such as Git push) within Git, triggering custom behavior at key points in the development cycle. Git has two types of hooks: client-side and server-side. Client-side hooks are invoked by operations such as commit and merge, while server-side hooks are invoked by networked operations such as receiving pushed commits.
Configure git hook
The first method:
$CD/home/gitname/bare/bare git # / hooks into the bare warehouse hooks file $cp post - update. The sample post - update # copy and rename the post - update. The sample files $vim post-update # Edit the post-update file and add the execution scriptCopy the code
Post-update content configuration:
Git /home/gitname/ WWW /blog/, connect to gitname/bare.git, pull the code, and then do something else.
#! /bin/sh unset GIT_DIR DIR_ONE=/home/gitname/ WWW /blog/ Under the gitname CD $DIR_ONE git init git remote add origin/home/gitname/bare/bare git # add remote gitname warehouse git clean - df git pull # NPM I # NPM run build #pm2 restart XXX #pm2 restart # sudo nginx -s reload # Nginx restartCopy the code
If submit code prompt git repository has been initialized, we can change again put the git init and git remote add origin/home/gitname/bare/bare git command removed. Or use rm -rf. git to empty the Git repository and pull again for each commit.
Modify the post-Update permission after the configuration is complete
$ chmod +x post-update
Copy the code
The second method:
Directly modify the hook file under the post-receive file, directly pull code.
$CD/home/gitname/bare/bare git # / hooks into the bare warehouse hooks file $vi post - receive # edit post - receive pull code executionCopy the code
Post-receive Writes the following information:
#! /bin/sh git --work-tree=/home/gitname/www/blog --git-dir=/home/gitname/bare/bare.git checkout -fCopy the code
Modify the post-Update permission after the configuration is complete
$ chmod +x post-receive
Copy the code
Select either method before performing the last step
# $exit exit from the current git user, switch to the root of $chown -r gitname: gitname/home/gitname/bare/bare git # add permissionsCopy the code
After adding remote repository sources to the local repository, git hooks are triggered as soon as local repository changes are committed and the driver automatically deploits.
Errors that can occur
Push error message after local client connects to remote Git repository
kex_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Copy the code
If you log in to the server using SSH and do not use the exit command to exit the server correctly, the number of connected processes increases. SSH login is restricted, so you cannot log in to the server correctly. The easiest way is to restart the server, or kill each process one by one. Either increase the number of server connections, but several methods have been tried, and it seems that git’s error has nothing to do with that. It turned out that SSHD was either not restarted or not turned on at all.
Modification:
vi /etc/hosts.allow
Copy the code
Change the value to SSHD :ALL.
Then restart the SSHD
sshd
service sshd restart
Copy the code
Solve password expiration problem:
Chage -l root # Change the expiration time chage -m 0 root chage -m 99999 root # Change the password will never expireCopy the code
Common Linux Commands
The sudo command is used as the administrator to perform operations. Sometimes, if you do not have permission to perform operations, you need to add the sudo command to perform operations.
Create folder:
sudo mkdir file
Copy the code
Create file:
sudo touch test.txt
sudo touch default.conf
Copy the code
List directories so files:
ls -al
Copy the code
Delete file:
Rm -rf file #Copy the code
View the current working directory path:
pwd
Copy the code
Vim edit file:
Vi Go to the editing page, press I to edit, press Esc to exit the editing, enter :wq to exit, and press! Forced out of
Link: 45 common Linux commands
Reference links:
Set up Git service on personal server and create your own private repository
Ali Cloud set up Git server
Build Hexo blog and deploy to Aliyun automatically using Git Hook
Ali cloud server (1, nGINx configuration combat)
Set up a private Git server
Simple automatic deployment with Git hooks