Switching from Debian to Ubuntu: Beginner’s Guide

on in Blog
Last modified on

Server - Stripe

I just switched my development server from Debian to Ubuntu. I decided to use Ubuntu 16.04, nginx 1.10, PHP 7 and Percona MySQL.

Here are the steps involved in creating this configuration from scratch.

Ubuntu

Get the ISO file from https://www.ubuntu.com/download/server, add it to your boot sequence and run it. The installer will ask for general details such as name, location, keyboard type and installation type. For a non-GUI installation, just go with the default settings and do not check any extra modules.

When installation is complete, you need to configure your root password by running:

sudo passwd

and following the steps. You will have to type your password twice. Whenever you need root access, just use:

su

When everything is done, update your Ubuntu installation by running:

apt update

apt upgrade

nginx

We use nginx-extras from Ubuntu’s repository. We use the default paths.

apt install nginx-extras

Edit /etc/nginx/nginx.conf:

nano /etc/nginx/nginx.conf

Modify error_log to:

error_log /var/log/nginx/error.log warn;

Open your server IP on a browser (use ip addr show to display it) and you’ll see the default nginx webpage.

Percona MySQL

We are going to use Percona MySQL. Percona MySQL and normal SQL has no difference in commands, it is a special optimised version.

gpg --keyserver  hkp://keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A

gpg -a --export CD2EFD2A | apt-key add -

sudo sh -c 'echo "deb https://repo.percona.com/apt/ xenial main" >> /etc/apt/sources.list.d/percona.list'

apt update

sudo apt install percona-server-5.7

sudo mysql_secure_installation

Delete all test/default users and databases. Now login to MySQL server and run the commands specific for Percona:

mysql -u root -h localhost -p

CREATE FUNCTION fnv1a_64 RETURNS INTEGER SONAME 'libfnv1a_udf.so';

CREATE FUNCTION fnv_64 RETURNS INTEGER SONAME 'libfnv_udf.so';

CREATE FUNCTION murmur_hash RETURNS INTEGER SONAME 'libmurmur_udf.so';

exit

service mysql restart

After completion of this step, you can run a cat command on /etc/mysql/my.cnf. Percona has no my.cnf by default. The most important step is how to enable auto restart of MySQL, in order to prevent the “Error Establishing a Database Connection” error message.

echo " " > /etc/mysql/my.cnf

cd ~ && wget https://raw.githubusercontent.com/AbhishekGhosh/Nginx-PHP5-FPM/master/percona-mysql-server/fail-safe-my.cnf

cat fail-safe-my.cnf > /etc/mysql/my.cnf

PHP

For the latest PHP version, use the following commands:

sudo add-apt-repository ppa:ondrej/php

apt install libcurl3 libmcrypt4 libmemcached11 libxmlrpc-epi0 php7.0-cli php7.0-common php7.0-curl php7.0-fpm php7.0-gd php7.0-intl php7.0-json php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-readline php7.0-xml php7.0-xmlrpc psmisc libmcrypt-dev mcrypt php-pear php-mysql php-mbstring php-mcrypt php-xml php-intl libmhash2 php-common
nano /etc/php/7.0/fpm/php.ini

Find cgi.fix_pathinfo. This will be commented out with a semi-colon (;) and set to 1 by default. We will change both of these conditions by uncommenting the line and setting it to 0 like this:

cgi.fix_pathinfo=0

Edit default nginx file with sudo nano /etc/nginx/sites-available/default command and make it like this:

server {
    listen 80 default_server;
    root /usr/share/nginx/html;
    index index.html index.htm index.php;
    server_name localhost;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        include fastcgi_params;
    }
}

Then restart PHP and Nginx:

sudo systemctl restart php7.0-fpm

or

sudo service php7.0-fpm restart

and

service nginx restart

Now, delete the default file in /usr/share/nginx/html and add a PHP file:

cd /usr/share/nginx/html

rm -r *

nano index.php

Add “Hello World”, save and exit.

Redis

Before you install Redis, there are a couple of prerequisites that need to be downloaded to make the installation as easy as possible.

Update all of the apt packages:

apt update

Once the process finishes, download a compiler with build essential which will help us install Redis from source:

sudo apt install build-essential

sudo apt install tcl8.5

Since we won’t need to keep the source code that we’ll compile long term, we will build in the /tmp directory.

cd /tmp

Download the latest stable version of Redis.

wget http://download.redis.io/redis-stable.tar.gz

Unzip and install:

tar xzvf redis-stable.tar.gz

cd redis-stable

make

make test

sudo make install

Now that Redis is installed, we can begin to configure it.

sudo mkdir /etc/redis

sudo cp /tmp/redis-stable/redis.conf /etc/redis

sudo nano /etc/redis/redis.conf

In the file, find the supervised directive. Currently, this is set to no. Since we are running an operating system that uses the systemd init system, we can change this to systemd:

[...]

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd

[...]

Next, find the dir directory. This option specifies the directory that Redis will use to dump persistent data. We need to pick a location that Redis will have write permission and that isn’t viewable by normal users.

We will use the /var/lib/redis directory for this, which we will create in a moment:

[...]

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis

[...]

Save and exit.

Create and open the /etc/systemd/system/redis.service file to get started:

sudo nano /etc/systemd/system/redis.service

Add this:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

Save and exit.

Now, we just have to create the user, group, and directory that we referenced in the previous two files.

Begin by creating the redis user and group. This can be done in a single command by typing:

sudo adduser --system --group --no-create-home redis

Now, we can create the /var/lib/redis directory by typing:

sudo mkdir /var/lib/redis

We should give the redis user and group ownership over this directory:

sudo chown redis:redis /var/lib/redis

Adjust the permissions so that regular users cannot access this location:

sudo chmod 770 /var/lib/redis

Start up the systemd service by typing:

sudo systemctl start redis

If all of your tests worked, and you would like to start Redis automatically when your server boots, you can enable the systemd service.

sudo systemctl enable redis

That’s it. These are the steps I’ve followed. For a more in-depth explanation, check the reference links below.

References:

Image Credit: www.cwcs.co.uk

Related posts