Host A Web Analytics Service On Your Server

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 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
相关文章
|
3月前
|
IDE Linux 开发工具
如何在Linux运行RStudio Server并实现Web浏览器远程访问
如何在Linux运行RStudio Server并实现Web浏览器远程访问
78 0
|
3月前
|
算法 前端开发 JavaScript
什么是 Web 开发的 Server Side Model
什么是 Web 开发的 Server Side Model
26 0
|
2月前
|
应用服务中间件 nginx
【报错】Failed to start A high performance web server and a reverse proxy server.
【报错】Failed to start A high performance web server and a reverse proxy server.
111 2
|
3月前
|
Linux
【web server】基于升序链表的定时器
【web server】基于升序链表的定时器
|
27天前
SpringCloud启动Consider defining a bean of type ‘org.springframework.web.client.RestTemplate‘ in your
SpringCloud启动Consider defining a bean of type ‘org.springframework.web.client.RestTemplate‘ in your
10 1
|
1月前
|
弹性计算 算法 应用服务中间件
倚天使用|Nginx性能高27%,性价比1.5倍,基于阿里云倚天ECS的Web server实践
倚天710构建的ECS产品,基于云原生独立物理核、大cache,结合CIPU新架构,倚天ECS在Nginx场景下,具备强大的性能优势。相对典型x86,Http长连接场景性能收益27%,开启gzip压缩时性能收益达到74%。 同时阿里云G8y实例售价比G7实例低23%,是Web Server最佳选择。
|
1月前
|
Windows
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
58 2
|
3月前
|
XML C++ 数据格式
C++使用gSoap写Web Server和Web Client
C++使用gSoap写Web Server和Web Client
36 1
|
3月前
【web server】HTTP协议如何解析or封装
【web server】HTTP协议如何解析or封装
|
3月前
|
Java
【web server】整体流程解析
【web server】整体流程解析