Setup & Deploy PHP 7.2, MariaDB 10.2, Node 9.4, Laravel, Composer and NPM on Ubuntu 16.04.3

Wei Zhong Ng
Searix Solutions
Published in
4 min readJan 11, 2018

--

We’d learned to follow setup instructions on DigitalOcean, Vultr, and a host of other blogs, but would always meet with little blocks everywhere that would require us to spend accumulated hours Googling and experimenting. So here’s a consolidated way of setting up all our common environments on a Digital Ocean droplet, and deploy our Laravel applications.

Note: If you are not from Searix, your setup might be different and you might want to omit some parts or tweak them to your needs.

Step 1: apt update and upgrade

The first thing you want to do is to ensure that all your packages are the latest. apt is kind of a prettier version of apt-get, so they’re mostly interchangeable.

sudo apt updatesudo apt upgrade -y

Step 2: Setup Digital Ocean Monitoring

If you spun your Digital Ocean droplet with Monitoring, you’ll need to enable the monitoring daemon to report back to Digital Ocean so that you can see the reports from their portal.

sudo curl -sSL https://agent.digitalocean.com/install.sh | sh

Step 3: Install Apache 2

sudo apt install apache2

Enable mod_rewrite and change your Apache config settings so that your .htaccess works.

a2enmod mod_rewrite

and in your /etc/apache2/apache2.conf file…

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>

Step 4: Install PHP 7.2

The standard repositories will only install PHP 5 now, but many dependencies (especially if you’re using Laravel) will depend on PHP 7, so you’ll need to hack your way through.

First, add the repos

sudo apt install -y python-software-propertiessudo add-apt-repository -y ppa:ondrej/phpsudo apt update -y

Then, install PHP 7.2 with the required extensions

sudo apt install -y php7.2 libapache2-mod-php7.2 php7.2-cli php7.2-common php7.2-mbstring php7.2-gd php7.2-intl php7.2-xml php7.2-mysql php7.2-zip php7.2-fpm php7.2-bcmath php7.2-curl

Side note: If you’re deploying a CodeIgniter package, ensure that its core is updated if you’re on PHP 7.

Step 5: Install MariaDB

The current MariaDB package in the apt repository is only at 10.0.31, which will give you problems like this when you try to do php artisanon Laravel -

In Connection.php line 664:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: …)

This issue is fixed with the installation of MariaDB 10.2 and above, which uses InnoDB as the default engine rather than XtraDB as in the previous versions.

To install MariaDB 10.2 (the latest stable version at the time of writing)

sudo apt install software-properties-commonsudo apt-key adv —-recv-keys —-keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8sudo sh -c "echo 'deb https://mirrors.evowise.com/mariadb/repo/10.2/ubuntu '$(lsb_release -cs)' main' > /etc/apt/sources.list.d/MariaDB102.list"sudo apt updatesudo apt upgradesudo apt install mariadb-server mariadb-clientsudo mysql_secure_installation

The sudo before mysql_secure_installation is important — without that, it won’t work.

If mysql_secure_installation throws an error about Plugin 'unix_socket' is not loaded, you probably want to set the authentication plugin to mysql_native_password.

sudo /etc/init.d/mysql stopsudo killall mysqld_safesudo killall mysqldsudo mysqld_safe --skip-grant-tables &mysql -u rootMariaDB> update mysql.user set plugin='mysql_native_password';sudo /etc/init.d/mysql stopsudo kill -9 $(pgrep mysql)sudo /etc/init.d/mysql startsudo mysql_secure_installation

Step 6: Create SSH keys for BitBucket to pull.

READ: You won’t be able to commit and push from the server. But you’ll be able to pull without having to enter your password again.

First, you’ll need a public/private key pair on the server. You can generate one with…

ssh-keygen -t rsa -C “lance@searix.net

Your public key is located at ~/.ssh/id_rsa.pub and you’ll need to copy the contents onto your clipboard.

Now, go to BitBucket, access a repository’s settings and go to the Access keys section.

Add a key, give it a label (any label that you can understand will refer to the server that’s going to be pulling from this repository),paste the public key and save it.

Now, you can clone the repository via SSH instead of HTTPS.

git clone git@bitbucket.org:searix/repository_name.git

The server will use the SSH key pair to authenticate for read-only access and pull without asking for a password again.

Step 7:Install Node and NPM

Node is currently at 9.4.0, and in order for npm to work, you’ll need to do this.

sudo npm cache clean -fsudo npm install -g nsudo n stablesudo ln -sf /usr/local/n/versions/node/9.4.0/bin/node /usr/bin/nodejsnpm updatenpm rebuild node-sassnpm install

Now, you can compile your frontend code. For us, it’s

npm run dev

Step 8: Install composer

sudo apt install composercomposer install

Step 9: Setup Laravel

php artisan key:generatephp artisan migrate:refresh --seed

--

--