How to run a website for free forever?
This article outline
- Cloud hosting
- Environment
- Application
- Traffic
- Summary
Cloud hostingI 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
EnvironmentI 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.
ApplicationI 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:1001100110011001
Gid:1002100210021002
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
totalusedfreesharedbuff/cacheavailable
Mem:581M352M66M900K162M135M
Swap:0B0B0B
TrafficThe 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.
SummaryIn this way, we can run a website quietly and almost permanently for free.
推荐阅读
- Developers must see: 25 best programming sites, how many do you know?
- How does cPanel use Let’s Encrypt and Cloudflare?
- Yoast SEO插件无法识别WordPress模板中的内容
- wp_title显示页面标题,但不显示网站标题
- wp_redirect不起作用
- WP_Query参数不起作用()
- wp_logout_url($redirect);不管用
- wp_localize_script不起作用
- wp_list_pages没有显示标题属性