Host A Web Analytics Service On Your Server

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
简介: Centcount Analytics is a free open-source web analytics software. Developed by PHP + MySQL + Redis, Can be easily deployed on your own server, 100% data ownership.

Why Self-host a Web Analytics Service?

What is the most important thing to you on internet? Absolutely Data Security & Privacy! Commercial data in particular. Unfortunately, There is nothing can be trust on internet, includes third party services. So, If you want data security, You have to hold 100% data ownership. Centcount Analytics is a free open-source web analytics software. It can be easily deployed on your own server, 100% data ownership.

Centcount Analytics Official Website: https://www.centcount.com

Prepare LNMP for Centcount Analytics

The nginx web server is a fast, lightweight server designed to efficiently handle the needs of high-traffic websites. Although commonly used to serve static content, it's quite capable of handling dynamic pages as well. This guide will help you install and run nginx with PHP via FastCGI for Centcount Analytics on your Ubuntu 16.04 Server.

The steps in this guide require root privileges. Be sure to run the steps below as root or with the sudo prefix. 

Set & Comfirm Your Hostname

1. To confirm your hostname, issue the following commands on your Server:

hostname
hostname -f

The first command shows your short hostname, and the second shows your fully qualified domain name (FQDN).

2. To reset your hostname, run the following commands:

hostnamectl set-hostname your_hostname
hostname -F /etc/hostname

The first command sets your hostname, the second reads hostname or NIS domain name from given file.

3. To modify hosts file after you reset your hostname, otherwise you will get a warning unable to resolve host with sudo prefix. To solve this issue, please insert a line to set your hostname under localhost in
File: /etc/hosts

127.0.0.1    localhost
127.0.0.1    your_hostname

Update Your System

Before you begin, please make sure your system is up to date. Execute the commands below to update:

sudo apt-get update
sudo apt-get upgrade
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with  sudo.

Install nginx, PHP for Processing, and Required Packages

Install the nginx web server and PHP dependencies:

sudo apt-get install nginx php7.0-cli php7.0-cgi php7.0-fpm

Configure nginx Virtual Hosting and the PHP Processor

In this guide, the domain example.com is used as an example site. Substitute your own FQDN or IP in the configuration steps that follow.

Nginx uses server directives to specify name-based virtual hosts. Nginx calls these server blocks. All server blocks are contained within server directives in site files, located in /etc/nginx/sites-available. When activated, these are included in the main nginx configuration by default.

1. We made an example configuration file for you below. You should now have the following server block in the nginx virtual host configuration. Replace all instances of example.com with your domain, modify the root path as shown below, and add the location ~ \.php$ block. The SSL certificate is recommended for connection security. You should add a new server block to listening 443 port. Replace the example SSL certificate file to your certificate file.

Create File: /etc/nginx/sites-available/example.com

server {
    listen 80;
    server_name example.com;

    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 2;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

    access_log /var/www/html/example.com/logs/access.log;
    error_log /var/www/html/example.com/logs/error.log;
    root  /var/www/html/example.com/public_html;
    index  index.html index.php;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME /var/www/html/example.com/public_html/$fastcgi_script_name;
    }
}


server {
    listen 443;
    server_name example.com;

    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 2;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

    ssl on;
    ssl_certificate /etc/ssl/private/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    access_log /var/www/html/example.com/logs/access.log;
    error_log /var/www/html/example.com/logs/error.log;
    root  /var/www/html/example.com/public_html;
    index  index.html index.php;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME /var/www/html/example.com/public_html/$fastcgi_script_name;
    }
}

2. Create the root directory referenced in this configuration, replacing example.com with your domain name:

sudo mkdir -p /var/www/html/example.com/public_html
sudo mkdir -p /var/www/html/example.com/logs

3. Configure PHP7.0-FPM to optimize server performance.

Modify File: /etc/php/7.0/fpm/pool.d/www.conf (below settings are based on a 2GB RAM VPS)

;3 possible values for pm: static(recommended), dynamic, ondemand 
pm = static

; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
pm.max_children = 100

; Note: Used only when pm is set to 'dynamic'
pm.start_servers = 40
pm.min_spare_servers = 20
pm.max_spare_servers = 100

4. Enable the site, disable the default host, and restart the web server:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart php7.0-fpm nginx

5. To deactivate a site, simply delete the symbolic link:

sudo rm /etc/nginx/sites-enabled/example.com
sudo systemctl restart nginx

6. The source file is saved, and the site can be re-enabled at any time by recreating the symbolic link.

If you are using nginx to host more than one site, create multiple virtual host files using the method above.

You may also want to edit the http block in /etc/nginx/nginx.conf, which applies across all sites and allows the following options, among others:

Hide HTTP header information using server_tokens

Configure SSL/TLS settings

Customize log file paths

Install the MySQL Database Server

The MySQL database engine is one of the leading open-source relational database engines and is a popular database solution for web-based applications.

1. Install the MySQL server packages and required PHP support for MySQL:

sudo apt-get install mysql-server php7.0-mysql

During the installation process you will be prompted to set a password for the MySQL root user via an ncurses menu. Choose a strong password and keep it in a safe place for future reference.

2. Log in to the MySQL command line interface (CLI) as the root user. When prompted, provide the password set in Step 1:

mysql -u root -p

3. To ensure that PHP will be able to access the MySQL connector you just installed, restart the PHP service by issue the following command:

sudo systemctl restart php7.0-fpm

4. Edit mysql config file to change the charset settings and sql mode.

Modify File: /etc/mysql/my.cnf

Add below settings into [client] section and [mysqld] section.

[client]
default-character-set  = utf8

[mysqld]
default-storage-engine = INNODB
character-set-server   = utf8
collation-server       = utf8_general_ci
sql-mode = "NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Note: 

If at any point you need to change the root password, log in as shown in Step 2 and enter the following command, replacing password with the new root password: 

ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH ‘mysql_native_password’ BY ‘password’; 

Install Redis & PHP-Redis Extension

sudo apt-get install redis-server php-redis

Install Other PHP Extension for Centcount Analytics

sudo apt-get install php7.0-gd php7.0-curl php7.0-mbstring php7.0-json

Install Postfix for Notification Mail

sudo apt-get install postfix

Notice: Some VPS providers denied the mail sending port. So, make sure your VPS is allowed to use mail sending port, otherwise Postfix will not work properly.

Restart PHP & Nginx To Make Sure All Changes Be Effected

sudo systemctl restart php7.0-fpm nginx

Test PHP with FastCGI

Create a file called test.php in your site's public_html directory with the following contents:

File: /var/www/html/example.com/public_html/test.php

<?php phpinfo(); ?>

When you visit http://www.example.com/test.php in your browser, the standard 'PHP info' output is shown.

Download Centcount Analytics Installation File

1. You can download Centcount Analytics installation file from GitHub or Gitee.
Execute the commands below:

cd /var/www/html/example.com/public_html
git clone https://github.com/WMJonssen/Centcount-Analytics.git CA
mv CA/.git .
rm -rf CA
git reset --hard

2. Unzip IP address library package which is included in Centcount Analytics project.
Execute the commands below:

cd /var/www/html/example.com/public_html/ipdb
unzip ipdb.zip

Centcount Analytics packed 2 free IP address libraries (IP2Location & GeoIP) and offers API to access it. You can directly replace the free edition to the commercial edition which offered ISP information & more accurate. And you don't have to change any code.

3. Modify Security Configuration File: /var/www/html/example.com/public_html/config/config_security.php

/************* Security Config Start *************/
//force ssl
define('FORCE_SSL', true);//If you don't have SSL Certificate, please set this const to "false".
//check ssl
define('IS_HTTPS', isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 1 || $_SERVER['HTTPS'] === 'on') ? true : false);
//define security transfer protocol
define('PROTOCOL', IS_HTTPS ? 'https://' : 'http://');
//define API transfer protocol
define('CURL_PROTOCOL', 'https://');//If you don't have SSL Certificate, please set this const to "http://".
/************** Security Config End **************/

We strongly recommend you apply a SSL Certificate for your analytics website.

4. Modify Common Configuration File: /var/www/html/example.com/public_html/config/config_common.php

/*************** CA CONFIG START **************/
//encode factor
define('ENCODE_FACTOR', 123456789);//RESET YOUR PRIVATE ENCODE FACTOR, IT IS VERY IMPORTANT
//mysql local root name
define('ROOT_USER_LOCAL', 'root');//set your mysql login username here (Creating Database Permission Is Necessary)
//mysql local root password
define('ROOT_PASSWORD_LOCAL', 'password');//set your mysql login password here
//administrator's timezone: PRC
define('ADMIN_TIMEZONE', 'PRC');//set administrator's timezone
//default timezone: PRC
define('DEFAULT_TIME_ZONE', 'PRC');//set default timezone
//error log host
define('ERROR_LOG_HOST', 'www.yourdomainname.com');//set Error Log host (You have to upload file errlog.php to this host under server's root directory)
/**************** CA CONFIG END ***************/

Notice: Some VPS providers denied the mail sending port. So, make sure your VPS is allowed to use mail sending port, otherwise Postfix will not work properly.

5. Modify Mail Configuration File: /var/www/html/example.com/public_html/config/config_mail.php

/************* Config Mail Start ***********/
//administrator mail
defined('ADMIN_MAIL') || define('ADMIN_MAIL', 'admin@centcount.com');
//auto response mail
defined('AUTORESPONSE_MAIL') || define('AUTORESPONSE_MAIL', 'autoresponse@centcount.com');
//notification mail
defined('NOTIFICATION_MAIL') || define('NOTIFICATION_MAIL', 'notification@centcount.com');
//fatal error mail
defined('FATALERROR_MAIL') || define('FATALERROR_MAIL', 'fatalerror@centcount.com');
/************** Config Mail End ************/

6. Modify Redis Configuration File: /var/www/html/example.com/public_html/config/config_redis.php

/************* Redis Config Start *************/
//redis instance 0 for kernel process (information of process, ticket, session)
define('REDIS_IP_0', '127.0.0.1');
define('REDIS_PORT_0', 6379);
define('REDIS_DB_0', 0);
//redis instance 1 for realtime visitor data (all information of realtime)
define('REDIS_IP_1', '127.0.0.1');
define('REDIS_PORT_1', 6379);
define('REDIS_DB_1', 1);
//redis instance 2 for CA javascript (site settings, site domains, robots list)
define('REDIS_IP_2', '127.0.0.1');
define('REDIS_PORT_2', 6379);
define('REDIS_DB_2', 2);
//redis instance 3 for session (session information)
define('REDIS_IP_3', '127.0.0.1');
define('REDIS_PORT_3', 6379);
define('REDIS_DB_3', 3);
/************** Redis Config End **************/

If your VPS configurated Multi-Core CPU. We strongly recommend setting multi-instance redis to improve Centcount Analytics gets maximum performance.

Install Centcount Analytics

For now, every thing is ready for installing Centcount Analytics. 

1. Visit the url: https://www.yourdomainname.com/install.php in browser.  

The Centcount Analytics Agreement will be shown.

d700ea22f89175a27a5aa1ea3a0e92284e4333f4

2. Please read Centcount Analytics agreement carefully before installing. 

If you agree on all the terms, then click the "Accept" button to continue.

54c900b2a454cd02475aa9601ad058422cdb6e06

3. If all settings & configurations are all right, click the "Next Step" button to continue installing. 

If there is something wrong, please make sure all issues be solved before installing.

1dcf3ab1876ac23b2209c9694fa76ee6a08f73bc

4. Input MySQL username & password, specify the administrator's login email & password for Centcount Analytics

And click the "Install CA" button to finish installing.

90d1a0e3d87efd673957a7c2ad9b44ea5fdb5731

5. Now, you can login Centcount Analytics to add a site.
Congratulations, you've configured the nginx web server to use PHP-FastCGI & Redis for Centcount Analytics!
And you can say bye to PIWIK now!

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
4月前
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
|
1月前
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
|
2月前
|
网络协议 Windows
Windows Server 2019 Web服务器搭建
Windows Server 2019 Web服务器搭建
|
4月前
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
4月前
|
开发者 前端开发 Apache
Apache Wicket Ajax揭秘:轻松几步,让你的Web应用告别“呆板”,焕发新生!
【8月更文挑战第31天】随着互联网技术的发展,Web应用的交互性成为评价网站成功的关键指标。Apache Wicket作为一款卓越的Java Web框架,不仅具备强大的组件化开发能力,还内置了对Ajax技术的支持,使开发者能轻松提升Web应用的交互体验。通过简单的代码示例展示了如何在不刷新页面的情况下异步更新页面元素,极大提升了用户体验。Wicket提供了多种Ajax组件和行为,如AjaxFallbackLink、AjaxButton等,满足不同场景需求,并支持自定义Ajax行为,帮助开发者实现复杂交互效果。合理运用Wicket的Ajax功能,可显著增强网站竞争力。
53 0
|
4月前
|
C# 开发者
全面提升开发效率:详解如何使用Blazor Server与SignalR打造实时Web应用,从零开始构建聊天室示例并掌握实时通信核心技术
【8月更文挑战第31天】提高生产力不仅关乎效率提升,更在于用更少时间完成更多任务。本文将通过具体代码示例,介绍如何结合 Blazor Server 和 SignalR 构建实时 Web 应用。从创建 Blazor 项目到添加 SignalR 支持,再到实现客户端与服务器间的实时通信,每个步骤都详细讲解。通过这一组合,C# 开发者能获得前后端一致的编程体验,轻松打造高效、响应迅速的实时应用。实时通信功能已在社交、协作等多个领域发挥重要作用,本文将助你掌握这一强大技术组合。
130 0
|
4月前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
4月前
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
|
4月前
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
|
4月前
|
Linux Python
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https