Web Development for Data Scientists: Core functionality and a DevSecOps foundation
Tags: webdev, datascience, devops
Published: 2022-11-23 15:42:10


In [my last post](https://samgould.net/index.php/2022/11/02/introduction-to-web-development-for-data-scientists/) I outlined a number of different approaches for setting up a simple website architecture. I mentioned that websites are typically hosted on Cloud VM/VPS or managed hosting solutions, and recommended some follow-up actions which take a Hello World site to a more robust production level. In this post **I'm going to expand on some of these follow-up actions with a focus on bolstering our DevSecOps so that we have a solid foundation from which to build**. Some points will be specific to WordPress sites, but most of the DevSecOps applies in general (to Linux servers). Before I get to DevSecOps however, I'm going to quickly cover my overall approach to task management and implementation of some core site elements.

###How do I manage my web development tasks?

Personally, I find it important to maintain structured notes and TODO lists for my projects (not just web dev). I am using the following structure which I created to manage my web development tasks:

<u>
- Deployment

- Plugins

- Design

- Content

- Integrations

- DevOps

- Server management and security
</u>

###Deployment, Plugins and Design

What is quite nice about this task management structure, besides being essentially [MECE](https://en.wikipedia.org/wiki/MECE_principle) and easy to understand, is that at the beginning it can roughly be followed in order. In the first post, we covered Deployment of the Hello World site (in particular I used DigitalOcean's 1-click WordPress launcher to set up a Droplet VM which hosts samgould.net). The next step for me was to install a WordPress theme (which is [very easy](https://www.wpbeginner.com/beginners-guide/how-to-install-a-wordpress-theme/)), essentially a Plugin, and then to configure the Design options. While not the focus of this article, the basic actions I would cover here are (all found under Appearance &gt; Customise in WordPress):

<u>
- Define a tagline

- Upload a 512x512 favicon

- Upload a logo (150x150 suggested but you can also use a banner)

- Pick a nice colour and font scheme

- Define a sitewide header and footer (NB: it's possible to remove the theme copyright text by going into your site html root directory, which is `/var/www/your-site/` where $your-site may depend on your Apache virtual host config, and then going to the relevant theme file, which for me is `wp-content/themes/theme-name/includes/template-tags.php`, and editing the php code; however I'm not 100% sure if this is permitted by copyright and/or the theme license (GPL v3 for me) so I decided to be a good citizen and leave it in place. It is interesting to know that this is where the processing happens under-the-hood, though.)

- Create a public nickname for the admin user, to be used in the default WordPress template ("content posted by X"); or it [can be removed completely](https://www.wpbeginner.com/wp-themes/how-to-remove-author-name-from-wordpress-posts/).

- Set a site timezone (under Settings &gt; General).
</u>

###Content

You would probably then want to add some basic content to your site! I added stuff like:

<u>
- Basic homepage

- About Me / Bio page with links to socials

- Core functionality - a blog and list of freelance services for me; you might want an e-commerce store or something else
</u>

Another important piece of functionality is enabling people to contact you. Of course it is easy to write your email address or link to other socials, but it looks quite professional to be able to **receive email to you@yourdomain**. I discovered that, while it is easy to receive emails like this (very simple to [set up via your domain registrar](https://www.epik.com/support/how-to-use-email-forwarding/)), sending emails requires an SMTP server. This is commonly achieved via [paid](https://superuser.com/questions/208485/sending-email-from-domain-name-address) or [free tier](https://www.reddit.com/r/webhosting/comments/iu7ud0/sending_email_from_digital_ocean_and_linode/) plugins but could also be [self-hosted](https://www.reddit.com/r/selfhosted/comments/rfh15d/how_do_your_selfhosted_applications_send_emails/).

###Integrations

The only work I have done here is beginning to automate the way I share my content across different socials. I have started posting to [DEV.to](https://dev.to/samg7b9) and am using a [script I wrote which reformats WordPress markup](https://gist.github.com/samg7b5/2ebfc9e18cab74f5000919b9b76ae604) so it is ready to post. This is somewhat tangential and should not be considered a necessary step to setting up your website.

###DevOps

In the context of this article, by DevOps what I really mean is: "**how do I manage my code and other site assets in a way which is flexible for development and resilient to failure?**". In particular - version control and backups. I would also echo DigitalOcean's best practice advice and first [set up a non-root user](https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04#step-2-creating-a-new-user) for any work you do on the server. In the code below, we will call this user `_non_root_user_`.

####Version control

At this stage, all you really need is a Git repo to hold relevant files from your site codebase. For my WordPress site, this is simply [custom theme and plugin code](https://stackoverflow.com/questions/54595787/using-wordpress-with-git-which-files-should-i-ignore/57414776#57414776) (and at this stage I don't even have any custom plugins). As mentioned in the Design section above, the site root on your server may depend on your HTML server config; mine depends on [Apache virtual hosts](https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-20-04) and can be double checked by checking which sites are within `/etc/apache2/sites-enabled`. As a data scientist you (should) probably know how to set up a Git repo, but there were a few gotchas which I discovered along the way:

<u>
- `cd /var/www/your-site/`

- `git init`

- **gotcha #1**: `sudo chown -R non_root_user /var/www/your-site/` (explanation [here](https://stackoverflow.com/questions/72978485/git-submodule-update-failed-with-fatal-detected-dubious-ownership-in-repositor))
<u>
- **gotcha #2**: if you try to interact with the filesystem from outside the server (e.g. upload media files via web UI) then you will now get errors because the default `www-data` worker no longer owns the files. After you finish working with the git repo you can reset it with `chown -R www-data /var/www/your-site/`. _I'm not sure if there is a better way to handle this…_
</u>

- **gotcha #3**: `nano .gitignore` (this is a gotcha in the sense that it is crucially important you define (and save!) the correct gitignore. Do not commit passwords/secrets to your repo! In particular `wp-config.php` must be excluded. I used <a href="https://salferrarello.com/wordpress-gitignore/">Sal Ferrarello's "surgical" .gitignore</a> as my starting point. Make sure you populate and save the file.)

- `git add .`

- `git commit -m "First commit"`

- Create a repo, e.g. on github, and do `git remote add origin https://github.com/your-github-username/your-repo.git`

- `git branch -M main`

- `git push origin main`
<u>
- **gotcha #4**: although the command output says it requires a password, [it actually needs](https://stackoverflow.com/questions/68775869/message-support-for-password-authentication-was-removed-please-use-a-personal) a [Personal Access Token](https://github.com/settings/tokens).
</u>
</u>

Congrats, you have now synced your mutable (WordPress) site files to a Git repo. But this is not every component of the site - we also need to create a backup of our content and config. For that we will use phpMyAdmin (although [there are multiple ways](https://wordpress.org/support/article/backing-up-your-database/) to do this).

####Database backup

<h4>What is phpMyAdmin?</h4>

As WordPress [puts it](https://wordpress.org/support/article/phpmyadmin/):

"An administrator’s tool of sorts, phpMyAdmin is a PHP script meant for giving users the ability to interact with their MySQL databases. WordPress stores all of its information in the MySQL database and interacts with the database to generate information within your WordPress site. A “raw” view of the data, tables and fields stored in the MySQL database is accessible through phpMyAdmin."

<h4>How to use phpMyAdmin to backup a WordPress site</h4>

Step 1: [install phpMyAdmin](https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-20-04): `sudo apt install phpmyadmin`

<u>
- With an Apache server, you may need to run these additional commands: `sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf &amp;&amp; sudo a2enconf phpmyadmin.conf &amp;&amp; sudo service apache2 reload`. I only needed to run the apache2 reload for some reason.

- With an Nginx server, you may need to run these additional commands: `sudo ln -s /user/share/phpmyadmin /var/www/my-site/phpmyadmin &amp;&amp; nginx -s reload`
</u>

phpMyAdmin can then be accessed via _https://yourdomain/phpmyadmin_ (NB: see the 'Server management and security' section below for instructions on enabling https, or get it automatically from the DigitalOcean 1-click installer). You can log in with the MySQL root user created during the DigitalOcean 1-click deployment when our LAMP stack was configured (see the [first post](https://samgould.net/index.php/2022/11/02/introduction-to-web-development-for-data-scientists/)), or [create a new database user with appropriate permissions](https://devanswers.co/phpmyadmin-access-denied-for-user-root-localhost/).

Step 2: run a simple backup using phpMyAdmin. This is very simple to do - instructions can be found [here](https://wordpress.org/support/article/backing-up-your-database/#using-phpmyadmin).

Congrats, you have now backed up pretty much everything you need to be able to restore your (WordPress) site in case something breaks in production!

####What about CI/CD?

A pattern for deploying different codebase versions is [blue/green deployment](https://www.redhat.com/en/topics/devops/what-is-blue-green-deployment). This is possible to do, for example [using Apache virtual hosts](https://techannotation.wordpress.com/2020/01/28/blue-green-deployment-with-apache-web-server/), but in my opinion is overkill at this stage.

###Server management and security

One of the first, most important steps to securing your server is **implementing a firewall to block unwanted traffic**. The simplest configuration tool is the [Uncomplicated Firewall (UFW)](https://wiki.archlinux.org/title/Uncomplicated_Firewall) and the suggested configuration is to allow only SSH (port 22, rate limited), HTTP (port 80), and HTTPS (port 443) access.

With a firewall (i.e. with appropriately exposed ports), you can **further secure your website by enabling encrypted connections over HTTPS/SSL**. This protects user privacy, data and has additional benefits like preferential treatment by search engines. The simplest way to implement this protection is by using the <a href="https://certbot.eff.org/instructions?ws=apache&amp;os=ubuntufocal">Certbot tool</a>, which handles the certification and renewal/reminder processes.

We can block further unwanted traffic by implementing **DDoS prevention**. The DigitalOcean 1-click installer for WordPress uses two layers, namely [fail2ban](https://wiki.archlinux.org/title/fail2ban) (should be implemented on all architectures) and [disabling XML-RPC](https://kinsta.com/blog/xmlrpc-php/) (WordPress-specific).

Finally, we want to make sure that our **server stays updated to mitigate against any potential exploits**. When using a Cloud VM/VPS, your Cloud provider will most likely push messages into the SSH console so that upon login you can see if you need to run updates (`udo apt update &amp;&amp; sudo apt upgrade`). Since vulnerabilities should be patched in a timely manner, it is sometimes recommended to [run updates in an automated and unattended manner](https://www.cyberciti.biz/faq/set-up-automatic-unattended-updates-for-ubuntu-20-04/) (i.e. as soon as they are available).

###Conclusion

We have completed our first batch of activities from each section of my task management framework, taking us from a simple Hello World to a site which is version controlled, backed-up and secured from attackers. Congratulations!

_Disclaimer: I am not a security professional and you should DYOR. You are responsible for the security of your server/website._

_This content was originally posted on [samgould.net](https://samgould.net/index.php/2022/11/22/web-development-for-data-scientists-core-functionality-and-a-devsecops-foundation/)_