Previously we have completed the blog site project function development, and in the local test through the use of. So now we also need to publish it to the server production environment, in order to bind the domain name to other users to see. Here is how to deploy our developed blog site on the server.

Server purchase and domain name purchase

To deploy to the online server, we first need to purchase a cloud server. Cloud servers can be purchased from ali Cloud, Tencent Cloud, Baidu Cloud, Huawei Cloud, Bird cloud and other cloud server providers. They have the advantages of service, price and so on, according to individual needs to buy.

By default, a blog’s traffic is not very large, so we only need to buy the minimum configuration, if there is an active price, we can also buy a higher configuration, such as 1 core 2 gb memory 3M bandwidth, 2 core 4 gb memory 5M bandwidth, etc. One important point is that the server should not install Windows system as much as possible. We should try to use Linux as the operating system of the cloud server, CentOS or Ubuntu can be selected either. The following uses CentOS as an example to explain how to deploy the CentOS.

Several cloud server vendors above also support the purchase of domain names. For example, we have bought a kandaoni.com domain name on it, and we will take this domain name as an example for our configuration later. After the domain name is purchased, the @ and WWW domains are resolved to the server IP address purchased above, and the domain name is available. The domain name that domestic buys, it is to need real name attetic, attetic by oneself can.

After purchasing the domain name, it is best to apply for an HTTPS for the domain name. The importance of HTTPS is not discussed here. The above service providers all provide free HTTPS certificate services. In later configurations, the HTTPS certificate is purchased by default.

Configuration of the server build environment

Similarly, we also need to install MySQL, Nginx, Go environment on the server, also need to install Git tools.

Git tool installation

For convenience, let’s first install and configure Git. After logging in to the server using SSH, run the following command using the root account:

yum install git
Copy the code

If N/Y is displayed during the installation, enter Y to complete the installation. After the installation, let’s test whether Git can be used normally:

[root@localhost ~]# git --versionGit Version 1.8.3.1 [root@localhost ~]#
Copy the code

If the version information is displayed, the system can use it normally. Git user info:

// Configure basic information. [root@localhost ~]# git config --global user.name "sinclair"
[root@localhost ~]# git config --global user.email [email protected][root@localhost ~]# git config --list
user.name=sinclair
[email protected]
[root@localhost ~]#
Copy the code

Install the GO environment

The golang installation guide for Windows and Linux is complete. Here we use the Linux CentOS installation method:

sudo yum -y install golang
Copy the code

After the installation is complete, let’s configure the path environment again:

Create GOPATH directory
mkdir /data/go
# open
vim /etc/profile
Copy the code

At the end of the open file, add the following code:

export GOPATH=/data/go
export PATH=$PATH:$GOPATH/bin
export GOPROXY=https://goproxy.cn,direct
Copy the code

In this case, we set GOPATH to the /data/go folder and gave Golang a proxy address of https://goproxy.cn, so that when we download packages that are not accessible externally, we can download them through the domestic proxy.

Install the LNMP environment

LNMP is the first letter of Linux, Nginx, MySQL, and PHP. It refers to the installation of Nginx, MySQL, and PHP software on Linux.

For simplicity and convenience, we use LNMP one-click installation package to install.

LNMP one-click installation package is a can be written in Linux shells for CentOS/RHEL/Fedora/Aliyun/Amazon, Debian/Ubuntu/Raspbian/Deepin/Mint Linux VPS or independent host installation LNMP(Nginx/MySQL/PHP), LNMPA(Nginx/MySQL/PHP/Apache), LAMP(Apache/MySQL/PHP) production environment Shell programs.

The latest LNMP one-click install package is available by visiting lnmp.org/.

First, we use the wget command to download the LNMP installation package back:

cd ~
# If wget is not installed, we can install wget first:
yum install wget
Get LNMP one-click install packageWget HTTP: / / http://soft.vpser.net/lnmp/lnmp1.7-full.tar.gzUnpack the LNMP one-click installation packageThe tar ZXVF lnmp1.7 - full. Tar. GzGo to the installation directory
cdLnmp1.7 - fullRun the install command
./install.sh
Copy the code

During the installation process, just choose the latest one for each item. During the installation, you need to set the root password for mysql. Please remember this password, which will be used in the blog installation later. It’s best to make this password as complex as possible and write it down in one text. Get in the habit of using and writing down complex passwords to reduce the risk of forgetting your password.

A more detailed installation tutorial is available at lnmp.org. Installation errors can also be found in the above solution.

After LNMP is installed, let’s go to the nginx configuration file to configure the proxy:

Enter the nginx configuration directory
cd /usr/local/nginx/conf/vhost
Create a blog profile
vim kandaoni.conf
Copy the code

In the open kandaoni.conf file, add the following code:

Upstream goblog {server 127.0.0.1:8001; } server{ listen 80; server_name kandaoni.com; rewrite ^/(.*)$ https://www.kandaoni.com/The $1 permanent;
}
server{
    listen 80;
    server_name www.kandaoni.com;
    root  /data/goblog/public;
    index index.html;

    error_page 404 /404.html;

    location / {
        try_files $uri $uri/index.html @go;
    }

    location @go {
        proxy_pass http://goblog;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

    access_log off;

    return 301 https://$host$request_uri;
}

server
{
    listen       443 ssl;
    server_name www.kandaoni.com;
    index index.html;
    root  /data/goblog/public;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate      /data/ssl/kandaoni.pem;
    ssl_certificate_key  /data/ssl/kandaoni.key;

    error_page 404 /404.html;

    location / {
        try_files $uri $uri/index.html @go;
    }

    include enable-php.conf;

    location @go {
        proxy_pass http://goblog;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|svg|webp)$ { expires 30d; } location ~ .*\.(js|css)? $ { expires 30d; } access_log /home/wwwlogs/kandaoni.log; }Copy the code

First we define a node to use the upstream module:

Upstream goblog {server 127.0.0.1:8001; }Copy the code

Then jump to the domain starting with WWW from the top level domain 301 without WWW:

server{
    listen 80;
    server_name kandaoni.com;
    rewrite ^/(.*)$ https://www.kandaoni.com/The $1 permanent;
}
Copy the code

Next, configure port 80 to be accessible

server{
    listen 80;
    server_name www.kandaoni.com;
    root  /data/wwwroot/goblog/public;
    index index.html;

    error_page 404 /404.html;

    location / {
        try_files $uri $uri/index.html @go;
    }

    location @go {
        proxy_pass http://goblog;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

    access_log off;
    If HTTPS is enabled, place it 301 over HTTPS. If not, comment it out
    return 301 https://$host$request_uri;
}
Copy the code

We purchased the HTTPS certificate above, so we configure HTTPS again

server
{
    listen       443 ssl;
    server_name www.kandaoni.com;
    index index.html;
    root  /data/wwwroot/goblog/public;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate      /data/ssl/kandaoni.pem;
    ssl_certificate_key  /data/ssl/kandaoni.key;

    error_page 404 /404.html;

    location / {
        try_files $uri $uri/index.html @go;
    }

    include enable-php.conf;

    location @go {
        proxy_pass http://goblog;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|svg|webp)$ { expires 30d; } location ~ .*\.(js|css)? $ { expires 30d; } access_log /home/wwwlogs/kandaoni.log; }Copy the code

After configuring HTTPS configuration, we also need to create the wwwlogs folder

mkdir /home/wwwlogs/
Copy the code

You also need to place the HTTPS certificate in the /data/ SSL folder on the server

mkdir /data/ssl

Put the contents of the pem file into Kandaoni.pem
vim /data/ssl/kandaoni.pem
Put the contents of the key file into Kandaoni.key
vim /data/ssl/kandaoni.key
Copy the code

At this point, we have Nginx deployed and mysql ready to use. Forget PHP, I don’t need it right now. Nginx startup and shutdown commands are as follows:

# start nginx
service nginx start
# restart nginx
service nginx restart
# Reload the nginx configuration
service nginx reload
# stop nginx
service nginx stop
Copy the code

Mysql > start/stop mysql

# start mysql
service mysql start
Close # mysql
service mysql stop
# restart mysql
service mysql restart
Copy the code

Blog website system released online

With all of the above environment configuration in place, we are finally ready to publish our blog site on the server for online use.

In general, websites need to be restricted to executing under WWW accounts, not root accounts. Generally, after installing LNMP, it has already created a WWW account for us, but the WWW account cannot be logged in because it is disabled by default. In order to use the WWW account, we also need to allow the WWW account to log in:

vim /etc/passwd
Copy the code

Find the WWW user and change /sbin/nologin to /bin/bash. This way WWW users can use it. For security purposes, we also need to set a complex password for the WWW user:

passwd www
Copy the code

Then enter the same password twice in a row.

Get the blog code

Now that we have configured the WWW user, let’s create the website directory and switch to the WWW user:

Create the data folder first
mkdir /data
# Create the wwwroot folder
mkdir /data/wwwroot
Assign the /data directory permissions to WWW
chown -R www:www /data
# Switch to WWW user
su www
Copy the code

Next, we use Git to pull the blog code to the /data/wwwroot/ directory on the server:

Enter the wwwroot folder
cd /data/wwwroot
Run git
git clone https://github.com/fesiong/goblog.git
Go to the goblog directory
cd goblog
Copy the code

This way we pull the blog code to the server. Then we can compile the project for use.

Compilation of blog site projects

Compiling on the server:

Go to the /data/wwwroot/goblog folder and run the following command:

Load dependencies into the local cache
go mod tidy
Place the dependency package in the vendor directory
go mod vendor
# execute the compile operation
go build -o goblog main/main.go
Copy the code

Good. After executing the above three commands, we will have a goblog binary, which is an executable file. Right now we can’t run it because we haven’t installed the database yet.

Cross-compile across operating systems on a local computer:

In addition to compiling on the server, you can also compile to binaries locally. This reduces code leakage and eliminates the need to deploy the Golang environment on the server.

To compile a Linux executable locally, run the following command:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o goblog main/main.go
To compile Windows executables locally, run the following command:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o goblog.exe main/main.go
To compile a Mac OS executable locally, run the following command:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o goblog main/main.go
Copy the code

Cross-platform compilation, first need to disable CGOCGO_ENABLED=0, and then set GOOS to the corresponding target system, can compile different system executable files.

After compiling, package the compiled file, template folder, and public folder together and upload the file to the server /data/wwwroot folder.

Blog site project running

Normal command execution is as simple as executing this binary:

./goblog
Copy the code

But with this execution, as soon as we turn off the terminal, the project stops. Therefore, we also need to put it in the background to execute. Here we use Linux’s nohup command to keep the blog site running in the background:

nohup /data/goblog/goblog&
Copy the code

Nohup (No hang up) is used to run commands in the background without interrupting the running of the program.

The format of the nohup command is:

Nohup Command [Arg...] [&]Copy the code

Parameter Description:

  • Command: Indicates the Command to be executed.
  • Arg: Some parameters that can specify the output file.
  • & : Allows commands to be executed in the background and continue to be executed after the terminal exits.

By default, it generates an output file of nohup.out in the current directory. If you want to use a different filename you can use it like this:

For example, redirect output to the goblog.log file:

nohup /data/goblog/goblog > goblog.log 2>&1 &
Copy the code

2 > &1 explanation:

Standard error 2 is redirected to standard output &1, which is then redirected to the runoob.log file.

  • 0 — stdin (standard input, standard input)
  • 1 — stDout (standard output, standard output)
  • 2 — Stderr (Standard error)

It’s already started, but what if I want to turn it off?

We can use the process lookup command to find the PID of the process and then kill the PID:

ps -def | grep "goblog"
Copy the code

Once the PID is found, we can kill it with the kill command. For example, we search for PID: 3251

# kill -9 Process ID PID
kill3251-9Copy the code

Since our blog project listens on port 8001, we can also look up the process PID by port:

# lsof -i: indicates the port number
lsof -i:8001
Copy the code

If the lsof command is not installed, install it first:

Remember to switch to root when installing
yum install lsof
Copy the code

Initialize the blog

Now that everything is ready, we pass the domain namewww.kandaoni.comVisit the blog and initialize it:This time you need to pay attention to the database information you fill in, do not enter the wrong. The data password has been set when the database is installed above. Since the blog initializer does not include the creation of a database, the database used here needs to be created beforehand. For example, we can log in to the database on the terminal and create the database we need:

mysql -u root -p
Enter your password and log in to the database. You can't see the cursor moving when you type the password, just press Enter.
Create database. The name of the database we created was irisWeb
create schema irisweb;
Copy the code

Check whether the command is successfully created:

show databases;
Copy the code

If the IRISWeb database is listed, the database is created successfully.

Then enter more information on the initialization screen and set up the administrator account and administrator password. That’s another thing to keep in mind. If you can’t remember it, record it in a local notepad to prevent forgetting it later.

After initialization, add the TDK information of the home page to config.json in the root directory:

"server": {
    "site_name": "See your blog"."env": "development"."port": 8001."log_level": "debug"."title": "See your blog: Golang combat development introduction graphic tutorial, Golang combat technology online learning website"."keywords": "Golang, Golang combat development, Golang combat learning."."description": "There are many golang development tutorials online, and they all start from the basics of the installation environment, golang syntax, for a little white, it is helpful. However, as we all know, blindly accept learning, is a boring to suffocating process. More often, learning is over, should forget, should not forget most of the forgotten, not according to their actual needs to learn, most of the memory is not deep enough. In order to break this inefficient learning process, I will start from here and introduce a golang practical learning method with demand learning."."icp": "Guangdong ICP 19130249-2"
}
Copy the code

We changed config.json, we need to restart the blog for it to take effect. Go back to the above start and close commands and execute them once.

At this point, the deployment of the blog site project is complete. Congratulations, you have a new blog site.

The complete project sample code is hosted on GitHub. The complete project code can be viewed at github.com/fesiong/gob… You can also fork a copy to make changes on it.