preface

This article aims to show the new development mode of Serverless Web through the example of rapid deployment of a wordpress website to Aliyun functional computing platform, including FUN tool to initialize NAS with one click, sync website to NAS, one-click deployment and other capabilities. Show the development agility of function computing, automatic elastic scalability, free operation and perfect monitoring facilities.

Relevant reference documents: yq.aliyun.com/articles/64…

1.1 summary of the DEMO

  • DEMO effect entry:hz.mofangdegisn.cn
    • Account: fc – test – the user
    • Password: fc – test – the PWD
  • Example Project address: FC-wordpress

Open the service

Free open function calculation, pay by volume, function calculation has a large free amount.

Free access to file storage service NAS, charge by volume

1.2 Solutions

As shown in the above, when multiple users by providing the url of the access to the web service, when hundreds of thousands of requests per second it doesn’t matter, function computing platform will automatically scale, to provide enough instances of executed in response to the user’s request, at the same time function calculation provides the perfect function and operation of monitoring equipment to monitor you.

1.3 Comparison between Serverless solution and traditional self-built Web solution

ITEM The cost of The stability of
VM based solution Ecs. T5-lc1m1. small, 22.8 yuan/month The server and database are deployed on the same VM, the active/standby Dr Is not implemented, and the host performance is poor
Lightweight application server 60 YUAN/month (1vCPU 1GB 1Mbps 20GB[SSD]) The server and database are deployed on the same VM, the active/standby Dr Is not implemented, and the host performance is poor
Function to calculate Sqlite3 version is about 1 yuan/month mysql version is about 26 yuan/month high

Function to calculate the full cost details:

  • The first 1 million function calls per month are free, the first 400,000 (GB* s) per month are free, and the memory of the function can be set to 128MB or 256MB, so it’s free for sites with less than 1 million visits per month
  • For low-cost websites, assume that the public network traffic generated in a month is 1GB and 0.8 yuan
  • NAS, US$0.06/GB/Month, website size is 50M, even if calculated according to 1G, 0.42 yuan
  • RDS iS the most basic stand-alone version of mysql, priced at $25 / month

Function to calculate the billing | NAS pricing

As mentioned above, in the field of low cost site, function to calculate the cost of it is very obvious advantage, while maintaining the stretch ability, later after business do big and no technology to switching costs (may need to do is to replace a more relational database), at the same time cooperate with prepaid can maintain smooth financial cost growth. Low cost websites become high availability and high performance websites are silky smooth. For details about high performance websites, please refer to Q1 in the FAQ at the end of this article.

Function to calculate how the PHP framework works

Before the specific operation deployment, first briefly comb through the function calculation running PHP framework principle

2.1 Operating principle of TRADITIONAL Server PHP

  • Schematic diagram of principle

  • A simple nginx conf

As can be seen from the above schematic diagram, the Web server sends the PHP script to phP-FPM for parsing according to the location in the CONF, and then returns the parsed result to the client

2.2 Engineering principle of FC driver PHP

  • The execution environment instance of the function calculation is equivalent to Apache/Nginx for traditional Web services
  • The user function is equivalent to implementing location in the Apache/Nginx conf
  • The user deploys the Web site on the NAS and then mounts the NAS to the execution environment of the function, as in the following code/mnt/autodirectory
  • The WordPress entry function code is as simple as this:index.phpWhere function computation provides one for the user$GLOBALS['fcPhpCgiProxy']Object to interact with phP-fPM and parse PHP files in PHP projects. This object provides two important interfaces:
    • requestPhpCgi
requestPhpCgi($request, $docRoot, $phpFile = "index.php", $fastCgiParams = [], $options = [])
Copy the code
- '$request' : same as the 'PHP HTTP invoke' entry - '$docRoot' : the root of the Web project - '$phpFile' : The default argument to concatenate SCRIPT_FILENAME in cgi arguments - '$fastCgiParams' : Function computations internally try to construct 'default CGI params' for you based on $request, but if you don't want it, you can override some parameters with' $fastCgiParams' (Reference: [cgi](https://en.wikipedia.org/wiki/Common_Gateway_Interface)) - `$options`: The debug_show_cGI_params parameter is set to true and will print cgi parameters for each request for PHP parsing. The default value is false. ReadWriteTimeout Sets the parsing time. The default value is 5 secondsCopy the code

If you are interested, consider the following functions to calculate PHP Runtime:

  • PHP entry functions
  • PHP Execution Environment

Case Procedure

Preparation conditions

Free open function calculation, pay by volume, function calculation has a large free amount.

Free access to file storage service NAS, charge by volume

Take a domain name, such as abc.com, and resolve the domain name CNAME to the region corresponding to function computation (FC)

If you want to in hangzhou region deployed wordpres website, will abc.com cn-hangzhou.fc.aliyuncs.com CNAME resolution to 12345., of which 12345 is your accountId

3.1 Installing the latest Fun tool

  • The installed version is 8.x latest version or 10.x or 12.x Nodejs
  • Install funcraf

3.2 Clone engineering

git clone https://github.com/awesome-fc/fc-wordpress.git

3.3 Access different directories based on the required database

  • Copy the.env_example file as.env and modify the information in.env as your own

If you are using a mysql database, refer to section 3.3.1

If you use sqlite3 database, refer to section 3.3.2

3.3.1 Using the mysql Database

  • Go to the directory fc-wp-mysql
fun nas init
fun nas info
Copy the code

Fun NAS init: Initialize the NAS based on the information obtained in your.env (the nas already meet the criteria) or create a nas available in the same region. If you have not changed the service name in templata. If there are modifications, Fun /nas/auto-default/{serviceName} (fun nas info can list new directories), Copy. Fun /nas/auto-default/fc-wp-mysql/wordpress to. Fun /nas/auto-default/{serviceName} You can also delete directories. Fun /nas/auto-default/ fC-Wp-mysql /wordpress

  • Upload wordpress site to NAS
fun nas sync
fun nas ls nas:///mnt/auto/
Copy the code

Fun nas sync: upload the contents of the local NAS (. Fun /nas/auto-default/fc-wp-mysql) to the fc-wp-mysql directory on the NAS fun NAS ls nas:/// MNT /auto/: Check to see if we have uploaded the file correctly to the NAS

3.3.2 Using the SQlite3 database

  • Go to the directory fc-WP-sqlite
fun nas init
fun nas info
Copy the code

Fun NAS init: Initialize the NAS based on the information obtained in your.env (the nas already meet the criteria) or create a nas available in the same region. If you have not changed the service name in templata. If there are modifications, Fun /nas/auto-default/{serviceName} (fun nas info can list new directories), Copy the wordpress directory of.fun/nas/auto-default/fc-wp-sqlite/wordpress from the default directory to.fun/nas/auto-default/{serviceName}. You can also delete the directory. Fun /nas/auto-default/ FC-WP-sqlite /wordpress

  • The installation process is completed locally and the SQlite3 database is initialized
    • Enter the command in the.fun/nas/auto-default/fc-wp-sqlite/wordpress directory:
PHP -s 0.0.0.0:80Copy the code
  • Modify the host file and add127.0.0.1 hz. Mofangdegisn. Cn
  • linux/mac : vim /etc/hosts
  • windows7: C:\Windows\System32\drivers\etc

Hz. mofangdegisn.cn is the domain name you have prepared

  • Enter Hz.mofangdegisn.cn in the browser. The mysql database setting page is not displayed at this time to complete the wordpress installation process

Fun /nas/auto-default/fc-wp-sqlite/wordpress/wp-content Sqlite should have the sqlite3 database file.ht.sqlite

  • Rollback the modification of the host file

Note: the base site URL of the sqlite3 database is the domain name prepared in advance, not 127.0.0.1

  • Upload wordpress site to NAS
fun nas sync
fun nas ls nas:///mnt/auto/
Copy the code

Fun NAS sync: Upload the contents of local NAS (. Fun /nas/auto-default/fc-wp-sqlite) to fc-wp-sqlite in NAS fun NAS ls nas:/// MNT /auto/: Check to see if we have uploaded the file correctly to the NAS

3.4 Deploying Functions to the FC Platform

Next deploy the function to the cloud:

  • Change the value of $host in index.php
  • Change the name of Project in template.yml LogConfig to a unique name
  • Change the template. Yml domain name prepared in advance
  • performfun deploy

Log in to the console, fc.console.aliyun.com, and you can see that the service and function have been created successfully and that the service has been configured correctly.

Open the domain name you have configured in the browser, for example, Hz.mofangdegisn.cn

  • Mysql version database, like traditional wordpress, directly into the installation process
  • Sqlite3 version of the database, because the previous initialization has been completed, you can directly enter the website home or website background

FAQ

Q1: Can functional computing develop high performance and high availability websites?

A: Yes, using the single-instance multi-concurrent functionality of function computation and high-performance database

It is necessary to add these optimizations:

  • Reserved instances eliminate cold start + prepaid optimization costs
  • Speed type of NAS
  • OSS object storage + CDN to store and distribute static resources

PHP Runtime does not support single instance multiple concurrent, use Custom Runtime, can be based on the traditional mode nginx + php-fpm + mysql development website directly simple and seamless migration to function computing platform. The example project customRun-time PHP

Use OSS to separate images from static images in WordPress

Q2: How to use the low cost SQlite3 version of the site, cold start the first time open slow?

A: Use A timer trigger function to keep warm

Q3: How much QPS can a site using a low-cost VERSION of SQLITe3 support?

A: Depending on the performance of the SQlite3 database, here are some pressure results:

Every time the pressure increases, there are some cold start, the time is slower, but support from the pressure test results to support 50 QPS is no doubt, is enough to support some small and medium-sized websites.

Q4: Is it possible to develop serverless web sites using other languages based on functional computing?

A: you can, such as python: yq.aliyun.com/articles/60… , or use the Custom Runtime, which has Java, Python, and Node built in

“Alibaba Cloud originators pay close attention to technical fields such as microservice, Serverless, container and Service Mesh, focus on cloud native popular technology trends and large-scale implementation of cloud native, and become the technical circle that knows most about cloud native developers.”