How to Install YOURLS on CentOS 7

Updated on February 10, 2017
How to Install YOURLS on CentOS 7 header image

YOURLS (Your Own URL Shortener) is an open source URL shortening and data analytics application.

In this article, we will cover the process of installing YOURLS on a CentOS 7 server.

Prerequisites

  • A CentOS 7 x64 server instance.
  • A sudo user.
  • A domain example.com that points to your server’s IP address.

Step 1: Update the system

Log in as a sudo user, and then use the below commands to update the system:

sudo yum install epel-release -y
sudo yum clean all && sudo yum update -y && sudo shutdown -r now

After the reboot, log back into the server using the same sudo user.

Step 2: Install a web server—Apache

Install the Apache web server using YUM:

sudo yum install httpd -y

Remove the Apache welcome page:

sudo sed -i 's/^/#&/g' /etc/httpd/conf.d/welcome.conf

Prevent Apache from exposing files in visitors' web browser:

sudo sed -i "s/Options Indexes FollowSymLinks/Options FollowSymLinks/" /etc/httpd/conf/httpd.conf

Start the Apache service and set it to auto-start on system boot:

sudo systemctl start httpd.service
sudo systemctl enable httpd.service

Step 3: Install MariaDB 10.x

Install the latest stable release of MariaDB, MariaDB 10.1 as below:

3.1 Create the MariaDB 10.1 YUM repo file

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

3.2 Install MariaDB 10.1 using YUM

sudo yum install MariaDB-server MariaDB-client -y

3.3 Start the MariaDB service and set it as running at system startup

sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

3.4 Secure the installation of MariaDB

sudo /usr/bin/mysql_secure_installation

Answer questions as below, and ensure that you will use your own MariaDB root password:

  • Enter current password for root (enter for none): Just press the Enter button
  • Set root password? [Y/n]: Y
  • New password: your-root-password
  • Re-enter new password: your-root-password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]: Y
  • Reload privilege tables now? [Y/n]: Y

3.5 Create a MariaDB database for YOURLS

Log into the MySQL shell as root:

mysql -u root -p

Type your own MariaDB root password and then press Enter.

In the MySQL shell, create a database yourls, a database user yourlsuser, and the database user's password yourpassword as follows.

Note: For security purposes, you should use your own user password instead of the sample password yourpassword.

CREATE DATABASE yourls DEFAULT CHARACTER SET UTF8 COLLATE utf8_unicode_ci;
CREATE USER 'yourlsuser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON yourls.* TO 'yourlsuser'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

Step 4: Install PHP 7.1 and necessary PHP 7.1 extensions

Install PHP 7.1 and several PHP 7.1 extensions as follows:

sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
sudo yum install -y mod_php71w  php71w-mysqlnd php71w-common

Step 5: Install YOURLS

5.1 Get the latest YOURLS code from YOURLS GitHub repo:

sudo yum install git -y
cd /var/www/html/
sudo git clone https://github.com/YOURLS/YOURLS.git
sudo chown -R apache:apache /var/www/html/YOURLS
cd YOURLS

5.2 Configure YOURLS:

sudo cp user/config-sample.php user/config.php
sudo chown apache:apache user/config.php

Use the vi text editor to open the /var/www/html/YOURLS/user/config.php file:

sudo vi user/config.php

Find the below lines:

define( 'YOURLS_DB_USER', 'your db user name' );
define( 'YOURLS_DB_PASS', 'your db password' );
define( 'YOURLS_DB_NAME', 'yourls' );
define( 'YOURLS_SITE', 'http://your-own-domain-here.com' );
define( 'YOURLS_COOKIEKEY', 'modify this text with something random' );
$yourls_user_passwords = array(
        'username' => 'password',

Replace them one by one as follows:

define( 'YOURLS_DB_USER', 'yourlsuser' );
define( 'YOURLS_DB_PASS', 'yourpassword' );
define( 'YOURLS_DB_NAME', 'yourls' );
define( 'YOURLS_SITE', 'http://example.com' );
define( 'YOURLS_COOKIEKEY', 'fmoi4jfsjfasfjlkfjalfgcggjkihdgfjjgdfolsfmwemlgjhgigjgitjaaewesfsdfsdogmbnsin' ); // Use a long string consists of random characters.
$yourls_user_passwords = array(
        'username1' => 'password1', // Use your own username and password.

Save and quit:

:wq!

5.3 Create a virtual host for YOURLS:

cat <<EOF | sudo tee -a /etc/httpd/conf.d/yourls.conf
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/YOURLS/
ServerName yourls.example.com
ServerAlias www.yourls.example.com
<Directory /var/www/html/YOURLS/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/httpd/yourls.example.com-error_log
CustomLog /var/log/httpd/yourls.example.com-access_log common
</VirtualHost>
EOF

5.4 Apply your settings:

sudo systemctl restart httpd.service

5.5 Modify firewall rules:

sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --reload

5.6 Web access:

Point your web browser to http://example.com/admin, and then click the Install YOURLS link to finish the installation.

Having YOURLS successfully installed, click the YOURLS Administration Page link to visit the YOURLS Admin interface, and then use the username username1 and password password1 to log in.

5.7 Post-installation security measures

For security purposes, you should restrict permissions to YOURLS after the installation:

sudo chown -R root:root /var/www/html/YOURLS

When you need to upgrade the program or install a plug-in, you can revert the strict permissions for that purpose as follows:

sudo chown -R apache:apache /var/www/html/YOURLS

That concludes our tutorial. Thanks for reading.