个性化阅读
专注于IT技术分析

How to run a website for free forever?

This article outline

Apart from the annual fee for domain names, my blog site is running on a cloud server. If there is no accident, the maintenance fee should be zero.

Cloud hosting

I use Google Cloud for cloud servers. Google Cloud should be the only cloud server provider that promises to provide Always Free forever. Other cloud service providers should only offer 12-month free usage options to new users.

Of course, the always free option provided by Google Cloud can only be used with a super weak instance:

  • Computer room in the US
  • Single shared CPU core
  • 600M of memory
  • 30G storage

The performance may not be as good as a Raspberry Pi, but it is enough for me to run a website.

Environment

I used the operating system for convenience: Ubuntu 18.04 LTS

Database

The database uses MySql 8.0 for some reason, but this reports an error directly after apt-get install mysql-server. Looking at /var/log/mysql/error.log, it shows:

2018-08-12T17:55:07.393408Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.12) initializing of server has completed
2018-08-12T17:55:11.130992Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.12) starting as process 3112
2018-08-12T17:55:11.782754Z 0 [ERROR] [MY-012681] [InnoDB] InnoDB: mmap(137428992 bytes) failed; errno 12
2018-08-12T17:55:11.782817Z 1 [ERROR] [MY-012956] [InnoDB] InnoDB: Cannot allocate memory for the buffer pool
2018-08-12T17:55:11.782832Z 1 [ERROR] [MY-012930] [InnoDB] InnoDB: Plugin initialization aborted with error Generic error.
2018-08-12T17:55:11.782886Z 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine
2018-08-12T17:55:11.784009Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2018-08-12T17:55:11.784055Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-08-12T17:55:11.789921Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.12)  MySQL Community Server - GPL.
2018-08-12T17:56:06.388162Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.12) starting as process 3276
2018-08-12T17:56:07.253414Z 0 [ERROR] [MY-012681] [InnoDB] InnoDB: mmap(137428992 bytes) failed; errno 12
2018-08-12T17:56:07.253518Z 1 [ERROR] [MY-012956] [InnoDB] InnoDB: Cannot allocate memory for the buffer pool
2018-08-12T17:56:07.253535Z 1 [ERROR] [MY-012930] [InnoDB] InnoDB: Plugin initialization aborted with error Generic error.
2018-08-12T17:56:07.253564Z 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine
2018-08-12T17:56:07.255109Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2018-08-12T17:56:07.255993Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-08-12T17:56:07.320087Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.12)  MySQL Community Server - GPL.

Direct memory is insufficient, mysqld cannot start.

I had to modify the /etc/mysql/mysql.conf.d/mysqld.cnf file, adding at the end:

performance_schema = off

One line, disable performance_schema to save memory usage, mysql can start normally.

nginx

Of course, nginx is also needed, just apt-get install nginx.

Application

I used Go to write the entire website Application, and used gorazor, esc and other tools to package the used templates and static resource files into executable files.

The entire program occupies less than 20M of memory when running, and there are more than one hundred trillion of free memory:

$ cat /proc/16537/status 

Name:   goblog                                                    
Umask:  0002                                                      
State:  S (sleeping)                                              
Tgid:   16537                                                     
Ngid:   0                                                         
Pid:    16537                                                     
PPid:   16403                                                     
TracerPid:      0                                                 
Uid:    1001    1001    1001    1001                              
Gid:    1002    1002    1002    1002                              
FDSize: 256                                                       
Groups: 4 20 24 25 29 30 44 46 108 114 1000 1001 1002             
NStgid: 16537                                                     
NSpid:  16537                                                     
NSpgid: 16537                                                     
NSsid:  16403                                                     
VmPeak:    16820 kB                                               
VmSize:    16820 kB                                               
VmLck:         0 kB                                               
VmPin:         0 kB                                               
VmHWM:     13356 kB                                               
VmRSS:      9564 kB                                               
RssAnon:            5720 kB                                       
RssFile:            3844 kB                                       
RssShmem:              0 kB                                       
VmData:     8920 kB                                               
VmStk:       132 kB                                               
VmExe:      4320 kB                                               
VmLib:         8 kB                                               
VmPTE:        80 kB                                               
VmSwap:        0 kB                 

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           581M        352M         66M        900K        162M        135M
Swap:            0B          0B          0B

Traffic

The free instance of Google Cloud Service does not charge money when running, but the traffic generated by the instance running website may require money. For example, the access traffic generated from China and Australia is not free.

How to do?

Set a cloudflare CDN directly in front of the website. Cloudflare provides a free CDN for personal sites!

If cloudflare goes back to Google Cloud, it is obviously impossible to take traffic from China and Australia.

Summary

In this way, we can run a website quietly and almost permanently for free.

赞(0)
未经允许不得转载:srcmini » How to run a website for free forever?

评论 抢沙发

评论前必须登录!