用Docker快速搭建短链接系统

公司产品的推广短信需要用到短网址服务,又不想用第三方如新浪的t.cn,那就自己搭建一个吧,开源项目官网:http://yourls.org

下载源码发布包到工作目录:https://github.com/YOURLS/YOURLS/releases

创建Dockerfile文件

FROM php:7.2-apache

#安装mysqli扩展用于php连接数据库
RUN docker-php-ext-install mysqli

#启用伪静态
RUN a2enmod rewrite

#先删除原来的文件目录
RUN rm -rf /var/www/html

#拷贝源码包
ADD YOURLS-1.7.2.tar.gz /
RUN mv /YOURLS-1.7.2/* /var/www/html/

#url重写
RUN echo -e '\n\n# BEGIN YOURLS\n<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteBase /\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^.*$ /yourls-loader.php [L]\n</IfModule>\n# END YOURLS\n\n' > /var/www/html/.htaccess

创建配置文件config.php

主要是配置域名、登录密码、cookie密码,数据库连接密码如下配置即可

<?php
/* This is a sample config file.
 * Edit this file with your own settings and save it as "config.php"
 *
 * IMPORTANT: edit and save this file as plain ASCII text, using a text editor, for instance TextEdit on Mac OS or
 * Notepad on Windows. Make sure there is no character before the opening <?php at the beginning of this file.
 */

/*
 ** MySQL settings - You can get this info from your web host
 */

/** MySQL database username */
define( 'YOURLS_DB_USER', 'yourls' );

/** MySQL database password */
define( 'YOURLS_DB_PASS', 'yourls' );

/** The name of the database for YOURLS */
define( 'YOURLS_DB_NAME', 'yourls' );

/** MySQL hostname.
 ** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */
define( 'YOURLS_DB_HOST', 'yourls-mysql' );

/** MySQL tables prefix */
define( 'YOURLS_DB_PREFIX', 'yourls_' );

/*
 ** Site options
 */

/** YOURLS installation URL -- all lowercase and with no trailing slash.
 ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */
define( 'YOURLS_SITE', 'http://s.itmx.xyz' );

/** Server timezone GMT offset */
define( 'YOURLS_HOURS_OFFSET', 0 );

/** YOURLS language
 ** Change this setting to use a translation file for your language, instead of the default English.
 ** That translation file (a .mo file) must be installed in the user/language directory.
 ** See http://yourls.org/translations for more information */
define( 'YOURLS_LANG', '' );

/** Allow multiple short URLs for a same long URL
 ** Set to true to have only one pair of shortURL/longURL (default YOURLS behavior)
 ** Set to false to allow multiple short URLs pointing to the same long URL (bit.ly behavior) */
define( 'YOURLS_UNIQUE_URLS', true );

/** Private means the Admin area will be protected with login/pass as defined below.
 ** Set to false for public usage (eg on a restricted intranet or for test setups)
 ** Read http://yourls.org/privatepublic for more details if you're unsure */
define( 'YOURLS_PRIVATE', true );

/** A random secret hash used to encrypt cookies. You don't have to remember it, make it long and complicated. Hint: copy from http://yourls.org/cookie **/
define( 'YOURLS_COOKIEKEY', 'Cookie加密密码' );

/** Username(s) and password(s) allowed to access the site. Passwords either in plain text or as encrypted hashes
 ** YOURLS will auto encrypt plain text passwords in this file
 ** Read http://yourls.org/userpassword for more information */
$yourls_user_passwords = array(
        '登录名' => '登录密码',
        // 'username2' => 'password2',
        // You can have one or more 'login'=>'password' lines
        );

/** Debug mode to output some internal information
 ** Default is false for live site. Enable when coding or before submitting a new issue */
define( 'YOURLS_DEBUG', false );

/*
 ** URL Shortening settings
 */

/** URL shortening method: 36 or 62 */
define( 'YOURLS_URL_CONVERT', 36 );
/*
 * 36: generates all lowercase keywords (ie: 13jkm)
 * 62: generates mixed case keywords (ie: 13jKm or 13JKm)
 * Stick to one setting. It's best not to change after you've started creating links.
 */

/**
* Reserved keywords (so that generated URLs won't match them)
* Define here negative, unwanted or potentially misleading keywords.
*/
$yourls_reserved_URL = array(
        'porn', 'faggot', 'sex', 'nigger', 'fuck', 'cunt', 'dick',
);

/*
 ** Personal settings would go after here.
 */

编写docker-compose.yml编排文件

version: '2'
services:
  yourls-web:
    build:
      context: ./
      dockerfile: Dockerfile
    container_name: yourls-web
    restart: always
    expose:
      - 80
    environment:
      - VIRTUAL_HOST=s.itmx.xyz
    volumes:
      - ./config.php:/var/www/html/user/config.php:ro

  yourls-mysql:
    container_name: yourls-mysql
    image: daocloud.io/mysql:5.7
    expose:
      - "3306"
    environment:
      - MYSQL_DATABASE=yourls
      - MYSQL_USER=yourls
      - MYSQL_PASSWORD=yourls
      - MYSQL_ROOT_PASSWORD=QYOII6xxxxxxxxD
      - TZ=Asia/Shanghai
    restart: always
    volumes:
      - ./mysql-manger:/var/lib/mysql
    command: mysqld --lower_case_table_names=1 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

启动

docker-compose up -d

打开浏览器输入:http://s.itmx.xyz/admin/install.php 点击安装

安装成功,有一个警告,没关系

尽情使用吧~

补充
默认情况下yourls的链接表没有设置索引,因此,在生产环境下,别忘了登录数据库增加索引

评论