インストールガイド · 5 min read · Nov 29, 2025

CentOS 8にYOURLS自己ホスト型URL短縮サービスをインストールする方法

YOURLSは、PHPで書かれた無料のオープンソースの自己ホスト型URL短縮サービスです。TinyURLやBitlyに非常に似ており、自分自身のURL短縮サービスを運営することができます。また、短縮URLにブランドを追加することも可能です。プライベートおよびパブリックリンク、カスタムURLキーワード、履歴クリックレポート、Ajaxインターフェース、Jsonpサポートなど、豊富な機能を提供しています。

このチュートリアルでは、Let’s Encrypt SSLを使用してCentOS 8にYOURLSをインストールする方法を示します。

前提条件

  • CentOS 8を実行しているサーバー。
  • サーバーIPにポイントされた有効なドメイン名。
  • サーバーに設定されたrootパスワード。

LEMPサーバーのインストール

まず、サーバーにNginx、MariaDB、PHPおよび必要なPHP拡張をインストールする必要があります。以下のコマンドで全てをインストールできます。

dnf install nginx mariadb-server php php-fpm php-json php-common php-mysqlnd php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath git unzip wget -y

すべてのパッケージがインストールされたら、PHP-FPM設定ファイル/etc/php-fpm.d/www.confを編集し、ユーザーをapacheからnginxに変更します。

nano /etc/php-fpm.d/www.conf

次の行を変更します。

user = nginx
group = nginx

ファイルを保存して閉じたら、Nginx、MariaDB、PHP-FPMサービスを起動し、システム再起動時に自動的に起動するように有効にします。

systemctl start nginx  
systemctl enable nginx  
systemctl start mariadb  
systemctl enable mariadb  
systemctl start php-fpm  
systemctl enable php-fpm

完了したら、次のステップに進むことができます。

YOURLS用のデータベースを作成

次に、YOURLS用のデータベースとユーザーを作成する必要があります。まず、以下のコマンドでMariaDBにログインします。

mysql

ログインしたら、以下のコマンドでデータベースとユーザーを作成します。

MariaDB [(none)]> CREATE DATABASE yourlsdb;  
MariaDB [(none)]> GRANT ALL PRIVILEGES ON yourlsdb.* TO 'yourlsuser'@'localhost' IDENTIFIED BY 'password';

次に、特権をフラッシュし、以下のコマンドでMariaDBから退出します。

MariaDB [(none)]> FLUSH PRIVILEGES;  
MariaDB [(none)]> \q

この時点で、MariaDBはインストールされ、設定されています。

YOURLSのインストール

まず、ディレクトリをNginxのウェブルートに変更し、以下のコマンドでYOURLSの最新バージョンをダウンロードします。

cd /var/www/html  
git clone https://github.com/YOURLS/YOURLS.git

次に、以下のコマンドでサンプル設定ファイルの名前を変更します。

cd YOURLS/user/  
cp config-sample.php config.php

次に、config.phpファイルを編集し、データベース設定を定義します。

nano config.php

次の行を変更します。

/** MySQLデータベースのユーザー名 */
define( 'YOURLS_DB_USER', 'yourlsuser' );
 
/** MySQLデータベースのパスワード */
define( 'YOURLS_DB_PASS', 'password' );
 
/ YOURLS用のデータベース名
  小文字のアルファベット[a-z]、数字[0-9]、アンダースコア[_]のみを使用 */
define( 'YOURLS_DB_NAME', 'yourlsdb' );
 
/ MySQLホスト名。
  非標準ポートを使用する場合は、'hostname:port'のように指定します。例:'localhost:9999'または'127.0.0.1:666' */
define( 'YOURLS_DB_HOST', 'localhost' );
 
/ MySQLテーブルのプレフィックス
  YOURLSはこのプレフィックスを使用してテーブルを作成します(例:`yourls_url`、`yourls_options`、...)
 ** 小文字のアルファベット[a-z]、数字[0-9]、アンダースコア[_]のみを使用 */
define( 'YOURLS_DB_PREFIX', 'yourls_' );
 
define( 'YOURLS_SITE', 'http://yourls.example.com' );
$yourls_user_passwords = array(
        'admin' => 'yourpassword',

完了したらファイルを保存して閉じます。次に、以下のコマンドで.htaccessファイルを作成します。

nano /var/www/html/YOURLS/.htaccess

次の行を追加します。


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]

ファイルを保存して閉じたら、以下のコマンドで適切な権限と所有権を与えます。

chown -R nginx:nginx /var/www/html/YOURLS  
chmod -R 775 /var/www/html/YOURLS

完了したら、次のステップに進むことができます。

YOURLS用のNginxの設定

次に、YOURLS用の新しいNginx仮想ホスト設定ファイルを作成します。

nano /etc/nginx/conf.d/yourls.conf

次の行を追加します。

server {
  listen 80;
  server_name yourls.example.com;
  root /var/www/html/YOURLS;
  index index.php index.html index.htm;
  location / {
    try_files $uri $uri/ /yourls-loader.php$is_args$args;
  }

  location ~ \.php$ {
    include fastcgi.conf;

    fastcgi_index index.php;
    fastcgi_pass unix:/run/php-fpm/www.sock;
  }
}

ファイルを保存して閉じたら、以下のコマンドでNginxとPHP-FPMサービスを再起動します。

systemctl restart nginx  
systemctl restart php-fpm

以下のコマンドでNginxのステータスを確認することもできます。

systemctl status nginx

次のような出力が得られるはずです。

? nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           ??php-fpm.conf
   Active: active (running) since Tue 2020-10-20 09:37:40 EDT; 5min ago
  Process: 12864 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 12862 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 12860 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 12871 (nginx)
    Tasks: 3 (limit: 12523)
   Memory: 5.5M
   CGroup: /system.slice/nginx.service
           ??12871 nginx: master process /usr/sbin/nginx
           ??12872 nginx: worker process
           ??12873 nginx: worker process

Oct 20 09:37:40 centos systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Oct 20 09:37:40 centos systemd[1]: Starting The nginx HTTP and reverse proxy server...
Oct 20 09:37:40 centos nginx[12862]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 20 09:37:40 centos nginx[12862]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 20 09:37:40 centos systemd[1]: Started The nginx HTTP and reverse proxy server.

SELinuxとファイアウォールの設定

デフォルトでは、SELinuxはCentOS 8で有効になっています。したがって、YOURLSウェブサイト用に設定する必要があります。

以下のコマンドでSELinuxを設定できます。

setsebool httpd_can_network_connect on -P  
chcon -R -u system_u -t httpd_sys_rw_content_t -r object_r /var/www/html/YOURLS

次に、以下のコマンドでファイアウォールを通過させるためにポート80と443を許可します。

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https  
firewall-cmd --reload

完了したら、次のステップに進むことができます。

YOURLSにアクセス

今、ウェブブラウザを開き、URL http://yourls.example.com/adminを使用してYOURLSにアクセスします。次のページが表示されるはずです。

YOURLS

Install YOURLSボタンをクリックします。次のページが表示されるはずです。

YOURLS Installer

YOURLS Administration Page”をクリックします。YOURLSのログインページが表示されるはずです。

Log-in

config.phpで定義した管理者のユーザー名とパスワードを入力し、Loginボタンをクリックします。次のページにYOURLSダッシュボードが表示されるはずです。

YOURLS admin dashboard

Let’s Encrypt SSLでYOURLSを保護

次に、YOURLSウェブサイト用にLet’s Encrypt SSLをダウンロードしてインストールするために、Certbotユーティリティをシステムにインストールする必要があります。

以下のコマンドでCertbotクライアントをインストールできます。

wget https://dl.eff.org/certbot-auto  
mv certbot-auto /usr/local/bin/certbot-auto  
chown root /usr/local/bin/certbot-auto  
chmod 0755 /usr/local/bin/certbot-auto

次に、以下のコマンドでYOURLSウェブサイト用のSSL証明書を取得してインストールします。

certbot-auto --nginx -d yourls.example.com

上記のコマンドは、最初にサーバーに必要な依存関係をすべてインストールします。インストールが完了すると、メールアドレスを提供し、以下のように利用規約に同意するように求められます。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to

cancel): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for yourls.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/yourls.conf

HTTPトラフィックをHTTPSにリダイレクトするかどうかを選択します。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for

new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

2を入力してEnterを押して続行します。インストールが成功裏に完了すると、次の出力が得られるはずです。

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/yourls.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://yourls.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=yourls.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/yourls.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/yourls.example.com/privkey.pem
   Your cert will expire on 2020-06-11. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again with the "certonly" option. To non-interactively renew *all*
   of your certificates, run "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

これで、URL https://yourls.example.comを使用してYOURLSウェブサイトに安全にアクセスできるようになりました。

結論

おめでとうございます!CentOS 8にNginxとLet’s Encrypt SSLを使用してYOURLSを正常にインストールしました。これで、YOURLSを使用して自分自身のURL短縮サービスを簡単にホストできます。質問があればお気軽にお尋ねください。

Share: X/Twitter LinkedIn

新しい投稿を受信箱で受け取る

スパムはありません。いつでも購読を解除できます。