DEV Community

Bidhan Khatri
Bidhan Khatri

Posted on • Edited on • Originally published at bidhankhatri.com.np

How to Install YOURLS on CentOS 7 using NGINX + HTTPS

In this guide, I will explain how to install YOURLS on a Centos7 VPS with MariaDB 10.4, PHP 7.3, and Nginx with HTTPS. YOURLS stands for Your Own URL Shortener. YOURLS is an open-source self-hosted application build with PHP which allows you to run your own URL shortening service.

Also, check yourls.org! Learn more tweaks in the Wiki documentation.

First Download the latest stable version MariaDB 10.4 on centos 7.

cat <<EOF | sudo tee -a /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.4 CentOS repository list - created 2020-01-03 16:44 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

yum install MariaDB-server

systemctl start mariadb

systemctl enable mariadb
Enter fullscreen mode Exit fullscreen mode

Once the database server is installed, execute the ‘mysql_secure_installation’ post-installation script to set your MySQL root password and secure the server.

mysql_secure_connection
Enter fullscreen mode Exit fullscreen mode

Login to MySQL shell as root and configure the database.

mysql -u root -p
MariaDB [(none)]> CREATE DATABASE yourlsdb;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON yourlsdb.* TO ‘username’@‘localhost' IDENTIFIED BY ‘password’;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit
Enter fullscreen mode Exit fullscreen mode

Now Install PHP 7.3 and require modules.

yum install epel-release yum-utils
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php73
yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd php-fpm
php -v
Enter fullscreen mode Exit fullscreen mode

Configure PHP-FPM:

Create a new PHP-FPM pool for yourls.

vim /etc/php-fpm.d/yourls.conf
[yourls]
listen = /var/run/php-fpm/php-fpm.sock
listen.allowed_clients = 127.0.0.1
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
user = nginx
group = nginx
pm = ondemand
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
security.limit_extensions = .php .php3 .php4 .php5
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
Enter fullscreen mode Exit fullscreen mode

Now Restart PHP-FPM

systemctl restart php-fpm
Enter fullscreen mode Exit fullscreen mode

Now Install and configure YOURLS.

yum install git
cd /var/www/html/
git clone https://github.com/YOURLS/YOURLS.git
chown -R nginx: /var/www/html/YOURLS
mv /var/www/html/YOURLS/user/config-sample.php /var/www/html/YOURLS/user/config.php

vim /var/www/html/YOURLS/user/config.php
(Replace below lines according to your configuration.)

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

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

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

/** MySQL hostname.
define( 'YOURLS_DB_HOST', 'localhost' );

/** MySQL tables prefix */
define( 'YOURLS_DB_PREFIX', 'yourls_' );
define( 'YOURLS_SITE', 'https://link.bdn.com.np’ );
define('YOURLS_ADMIN_SSL', true);

/** A random secret hash used to encrypt cookies. You don't have to remember it, make it long and complicated.
define( 'YOURLS_COOKIEKEY', ‘0101011’01010x0x0x0x0xx0x0x0xx0asadqw23232dsdsd2e22232 );

$yourls_user_passwords = array(
        'bdnadmin => ‘password123' /* This credential is used for web login later.*/ ,
        // 'username2' => 'password2',   // You can have one or more 'login'=>'password' lines
        );
Enter fullscreen mode Exit fullscreen mode

Install and configure Nginx:

yum install nginx
vim /etc/nginx/conf.d/link.bdn.com.np.conf

server {
        listen 80;
    listen  [::]:80;
        server_name link.bdn.com.np www.link.bdn.com.np;
        return 301 https://$host$request_uri;
}
server {
    listen 443 ssl;
    listen  [::]:443 ssl;
        server_name  link.bdn.com.np www.link.bdn.com.np;
    ssl_certificate /etc/nginx/ssl/bundle.crt;
        ssl_certificate_key /etc/nginx/ssl/bdncom.np.key;
    root   /var/www/html/YOURLS;
        index index.php  index.html index.htm;
    access_log /var/log/nginx/link.bdn.com.np_access.log;
    error_log /var/log/nginx/link.bdn.com.np_error.log;
    index index.php;

# Rewrites
    location / {
        try_files $uri $uri/ /yourls-loader.php;
        expires 14d;
        add_header Cache-Control 'public';
    }

# PHP Engine
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }
}
Enter fullscreen mode Exit fullscreen mode

Test the Nginx configuration and restart Nginx:

nginx -t
systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Final step:

Open** https://link.bdn.com.np/admin/install.php** in your favorite web browser and you should see the YOURLS install screen. Click on the ‘Install YOURLS’ button to populate the database.

Now login to YOURLS Administration Page.

https://link.bdn.com.np/admin
username: bdnadmin
password: password123
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
ozh profile image
྅༻ Ǭɀħ ༄༆ཉ

Neat article. Could you open a PR on github.com/YOURLS/awesome-yourls to get it listed?