Ubuntu Logo

Ubuntu System Services

This post about systemd has been published from a draft, so it’s not been proofed for publication. I find it useful, so I would like it readily available on-line. If you find anything wrong with it, please

Ubuntu, like many GNU/Linux distributions, uses systemd. systemd is a system used by the OS tor manage process, known as services. We can add our own services to systemd, and I’ll show how to do that in this post.

Services are useful, for example, if you start and run server. If we start that server as a user, the process running the server will be killed when the user logs out. If we want the server to run continuously – which is usually the case with servers – this is a problem.

We can be keep the service running by abandoning the process before logout, but it’s a hassle. In this set up, we would need to login, start the server, then abandoning processes. Instead, we can use systemd services and have the server started automatically (or on demand) and it runs in the background.

Systemd Unit Files

Our unit files go in the Systemd system directory.

/etc/systemd/system

Each service should have a name of the following form:

[name].service

Here’s an example of the format of the file.

[Unit]
Description=My Node.js Service

[Service]
WorkingDirectory=/home/jason/my-app
Restart=on-failure
ExecStart=/home/jason/my-app/dist/index.js

# Note: Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'.
# Note: when 'ExecStart' is used, 'Type' defaults to 'simple'.

Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV='production'
Environment=ORACLE_HOME=/usr/lib/oracle/18.3/client64
Environment=LD_LIBRARY_PATH=/usr/lib/oracle/18.3/client64/lib

[Install]
WantedBy=multi-user.target

For more information on Service files, see the man page.

man systemd.service

Update Systemd Services

 sudo systemctl daemon-reload 
 
sudo systemctl enable my-webapp.service

Created symlink /etc/systemd/system/multi-user.target.wants/event-tracker-api.service → /etc/systemd/system/event-tracker-api.service.

 
sudo systemctl start my-webapp
sudo systemctl stop my-app
sudo systemctl status my-webapp

Command:

sudo service [name] {start,stop,restart,status}
Maven Logo

Maven: Setting Compiler Version

I recently started a new JavaFX project. I decided to use Maven because the tool is great for building Java projects in a standard way that conforms with best practices. Unfortunately, I’m not the most well versed in the tool. I’ve used it a few times, and “it just worked”.

This evening I got the following errors when attempting to compile my project:

Source option 5 is no longer supported. Use 7 or later.
Target option 5 is no longer supported. Use 7 or later.

I didn’t know what it meant, so I went to the Web. A search result later, I learned this had to do with the compiler. So I added the following directive to my pom.xml file.

    <properties>
        <maven.compiler.source>13</maven.compiler.source>
        <maven.compiler.target>13</maven.compiler.target>
    </properties>

I’m using openjdk-13, so I told Maven that my source code (compiler.source) would be written in Java 13; and that my output code (compiler.target) should run on the Java 13 JVM.

If we were to compile directly on the command line, we could use -source and -target flags reach the same end, but I believe Maven leverages a javax.tools.JavaCompiler object that enables invocation of the Java compiler from within programs. From what I can tell, it’s the Maven Compiler Plugin which uses such an object – hence, the maven.compiler.* directives.

Mounting Windows File Shares in Docker

A network graph on top of an image of shipping containers

I recently had the need to mount a Windows network file share location within a Docker container. Thus, I tested the mounting of the share on my local machine. It worked, so I figured I’d simply set up the container to mimic the steps I took to mount the share on the host machine. Unfortunately, it wasn’t as straightforward as that. However, I was able to achieve the mount, and this post explains the steps I took to achieve the mount.

Continue reading

Redirect Apache Requests to Applications

When requests come into the Apache Web server, they are routed by default to the files in the Document Root. Sometimes we want to redirect those requests elsewhere. We can do this with the Rewrite module.

Add the Rewrite directives to the Apache config file, an .htaccess file, or do what I usually do and add it to the Virtual Host file.

Rewrite for SPAs

To direct requests to Single Page Web-apps, such as those that are build with React and Vue.js. This is what works for me.

<Directory "/var/www/my-app/dist">
Options Indexes FollowSymLinks
order allow,deny
allow from all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</Directory>

PHP Applications

Often when using Apache to host a PHP Web application, it’s desirable to handle incoming requests with PHP. By default, Apache will try to handle requests. But, we can use the rewrite module to send requests to specified PHP scripts.

<Directory "/path/to/webroot">
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?/$1 [L]
</Directory>