2016年6月13日 星期一

YOURLS 你也可以提供短網址服務,自架短網址轉址伺服器 & 安裝 Random Keywords 隨機 short URL

前陣子收到一個想要自架短網址服務的需求,就花了一些時間搜尋相關的方案
目前已經有許多線上的短網址服務提供大家使用,如 Google(goo.gl),PPT(ppt.cc),Godaddy(x.co) ..  等等,對於企業來說,老闆都會希望很多很炫的東西在自己公司也有,相對於 IT 人員增加了管理上的風險及成本,不過相對的使用線上服務就要有相對的代價,就是你不知道他何時停止營運 or 不再維護。

 

短網址(short url) 就是隨機產生一個很短的 URL,然後協助轉址到目的地,就是一個 return 301 的概念,讓提供網址時可以不再"漏漏長",對於文章的版面,或是程式碼的整潔都是一個正向的服務



 

從 GitHub 裡面找到大神分享的 YOURLS 專案,已經在短網址界造成流行,並且提供許多 plugins API、分析報表根本超好用!

 

在按照本篇安裝之前,你必須先擁有以下環境:

  • Nginx

  • MySQL

  • PHP、php-fpm、php-mysql


 

YOURLS 安裝

Step.1 安裝 YOURLS
$ cd /usr/share/nginx/html/ 
$ git clone https://github.com/YOURLS/YOURLS

 

Step.2 建立 YOURLS 資料庫
mysql> create database yourls; 
mysql> grant all privileges on yourls.* to yourls_USER@localhost identified by 'youls_PWD';
mysql> flush privileges;

 

Step.3 YOURLS 設定 php-fpm
$ vim /etc/php-fpm.d/yourls.conf 

[php-yourls]
listen = /var/run/php-fpm/php-yourls.socket
listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log

$ service php-fpm restart

 

 

 

Step.4 YOURLS 設定 Nginx,yourls-loader.php 是第一次的 loading 轉址,之後管理使用 /admin/
$ vim /etc/nginx/conf.d/yourls.conf 

server {
listen 80;
server_name short.com;
root /usr/share/nginx/html/YOURLS;
access_log /var/log/nginx/yourls_access.log;
error_log /var/log/nginx/yourls_error.log;

location /admin {
index index.php;
}

location / {
try_files $uri $uri/ /yourls-loader.php;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-yourls.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

$ service php-fpm restart

 

Step.5 YOURLS 設定 config.php,擷取重點部分
$ cd /usr/share/nginx/html/YOURLS/user/ 
$ mv config.sample.php config.php
$ vim config.php

#MySQL setting
define( 'YOURLS_DB_USER', 'yourls_USER' );
define( 'YOURLS_DB_PASS', 'yourls_PWD' );
define( 'YOURLS_DB_NAME', 'yourls' );
define( 'YOURLS_DB_HOST', 'localhost' );
define( 'YOURLS_DB_PREFIX', 'ys_' );

#Site URL
define( 'YOURLS_SITE', 'http://short.com' );

#Timezone GMT; Taiwan +8
define( 'YOURLS_HOURS_OFFSET', '+8' );

define( 'YOURLS_LANG', '' );

define( 'YOURLS_PRIVATE', true );

#Random cookieKey
define( 'YOURLS_COOKIEKEY', 'D@o2S49mPge9tv^8uF4z6_b4z' );

#Login Account , first login auto encrypted
$yourls_user_passwords = array(
'root' => 'rootP@ssw0rd'
);

其中 YOURLS_LANG 的部份可以參考 YOURLS in your language,由於沒有支援繁體中文(有支援簡體中文)索性就直接使用英文了

 

Step.6 登入 YOURLS,http://hostname/admin/

ScreenShot 2016-06-13 13.04.04

 

Step.7 管理介面,可以在 Enter the URL 輸入你要轉址的 URL,可以自定義 short URL

ScreenShot 2016-06-13 13.06.42

由於 YOURLS 預設產生的短網址是由 1、2、3 .... 連續的數字,如果想要隨機產生 short URL 可以接下去安裝 Random Keywords

 

 

 

YOURLS - Random Keywords

Random Keywords 隨機產生 short URL link

安裝
$ cd /usr/share/nginx/html/YOURLS/user/plugins/ 
$ git clone https://github.com/YOURLS/random-keywords

 

啟用 Ramdom Keywords

到 plugin 頁面; http://hostname/admin/plugins.php,在 Action 的地方點選 Activate 啟動

ScreenShot 2016-06-13 14.59.37

 

之後產生的 short URL 都會隨機產生囉。

Orignal From: YOURLS 你也可以提供短網址服務,自架短網址轉址伺服器 & 安裝 Random Keywords 隨機 short URL

沒有留言:

張貼留言