Web system development environment construction with Sakura VPS + Ubuntu 18 + Perl

A summary of the procedure for creating a Web system development environment with Sakura VPS + Ubuntu 18 + Perl.

Sakura VPS application

Apply for Sakura VPS.

Sakura VPS Price / Specification List

Install Ubuntu 18 on Sakura VPS

Let's install Ubuntu 18 on Sakura VPS. The installation procedure was tried on August 16, 2020.

Log in from Sakura VPS Control Panel.

Click Add New Server in the upper right.

Select a zone. Ishikari Zone 1. Tokyo second zone. Osaka 3rd zone.

It's a good idea to choose a zone that is cheaper and closer to where you will be servicing.

Plan selection. Choose from 512, 1G and 2G first. Experience shows that having about 100G of storage will not be a problem later, but if you are starting first, we recommend the plan with the lowest cost.

If you want to increase the storage, use the storage change option to increase the storage. The fee is only for the first time. It's about the risk of poor storage performance, so decide whether to speed up or take up capacity.

Proceed to the next.

The name of the server is an optional item, so leave it as it is.

The description of the server is also an optional item, so leave it as it is.

The OS to be installed is Ubuntu 18.04 amd64 (64bit).

The admin user name is fixed and is "ubuntu".

Enter the New Administrative User Password and the New Administrative User Password (re-entered for confirmation). This is effectively the root password.

The startup script is not used.

By default, the packet filter setting is only for SSH port 22. This will change later.

To register the public key, select Do not register.

Proceed to the next.

Select a payment pattern. You can choose monthly payment or annual lump sum payment.

Select a payment method. You can choose credit card, bank transfer or invoice payment.

Leave the coupon blank. If you have a coupon and want to use it, please enter it.

Proceed to the next.

The application confirmation screen will appear. Select I Agree and click Apply.

Now you can use Ubuntu 18 with Sakura VPS.

Packet Filtering-Firewall Settings

Open the ports needed for web system development. This procedure was tried on August 16, 2020.

Sakura VPS login.

Click on the server in the left sidebar.

From the server list, click the created server.

Click "Packet Filter" on the far right of the tab.

Click "To Packet Filter Settings".

Click "Add release port".

Open HTTP and HTTPS ports

Click "Web" in "Use of Filter". HTTP port 80 and HTTPS port 443 are released.

Click "Save settings". The setting is completed.

Repeat the same work.

"Packet filter" "To packet filter setting"

Open SSH port

SSH's default port 22 is a very security-risk port.

Add your own port for SSH. Avoid choosing well-known ports and ports used by web applications.

If you can't choose by yourself, choose one port in the "30000" range (up to 32767). I will write an example.

  • 30012
  • 31163
  • 32130

In Sakura VPS, packet filtering seems to require the port value to be a number in the range 1-32767.

Click "Add open port".

Click "Custom" in "Filter Usage".

Select "TCP" as the protocol.

Enter the SSH port number selected above in "Port number".

Click "Save settings". The setting is completed.

Repeat the same work.

"Packet filter" "To packet filter setting"

Open port for Mojolicious development server

Let's open the port for the Mojolicious development server. Mojolicious is one of Perl's web frameworks for real-time web and cloud servers.

The port for the Mojolicious development server is

3000

is.

Click "Add open port".

Click "Custom" in "Filter Usage".

Select "TCP" as the protocol.

Enter "3000" in "Port number".

Click "Save settings". The setting is completed.

If you have multiple developers, open another port from the packet filter as needed (3001, 3002, etc.).

Repeat the same work.

"Packet filter" "To packet filter setting"

Open port for Mojolicious production server

Let's open the port for the Mojolicious production server. This is used for testing.

The port for the Mojolicious production server is

8080

is.

Click "Add open port".

Click "Custom" in "Filter Usage".

Select "TCP" as the protocol.

Enter "8080" in "Port number".

Click "Save settings". The setting is completed.

If you have multiple developers, open another port from the packet filter as needed (3001, 3002, etc.).

Working from the web console

I will explain the work from the Web console.

After applying for the VPS service, the server operation starts with a command from the Web console.

In this article, the web console is a CUI terminal that can be used from a web browser.

The minimum thing you need to do is create a user for your web application.

Once you've created a user for your web application, you'll be able to access your Ubuntu server from Windows or Mac OS using a tool called an SSH client.

You can execute all commands from the web console, but for efficiency, it is practical to install and use SSH client and software that can edit files on the server on SSH on Windows or MacOS.

Display the web console

Let's display the web console.

Log in to Sakura VPS.

Click the target server.

Click "Console" at the top of the site.

Click "Serial Console (β version)". The serial console will start. The serial console is one of the web consoles. We will execute commands using the serial console.

Once the serial console boots up, press Enter once.

You will be able to enter the login user as follows:

tk2-250-35845 login:

Type ubuntu and press Enter.

ubuntu

You will be asked for your password as follows.

Password: Password:

Enter your initial password and press Enter. If you can log in, you are successful. It will be displayed as follows.

ubuntu @tk2-250-35845: ~ $

In this state, you can execute Linux commands.

Add user for web application

Let's add the user "myapp" for the web application. This name can be arbitrary, but in the following steps, we will use "myapp" for explanation.

Add user

Use the useradd command to add a user. The "-m" option creates a home directory.

Assuming that the sudo command is a command to be executed with administrator privileges.

#Add user (home directory available, use bash for shell)
sudo useradd -m myapp -s / bin / bash

When you execute the sudo command, you will be asked for the password, so enter the initial password.

Password setting

Let's set a password for the user "myapp" for the web application. "Myapp" will be added to the sudo group to give it administrator privileges, so it is recommended to keep the password secure in length and complexity.

The password can be set with the passwd command.

sudo passwd myapp

You will be asked to enter the password and re-enter it for confirmation, so set it.

Join sudo group

Register the application user "myapp" in the sudo group using the gpasswd command. Give the "myapp" user administrator privileges.

sudo gpasswd -a myapp sudo

Using SSH client

Let's connect to the server with SSH from the computer so that you can execute commands and edit files.

Command execution on SSH client

In the case of Windows, TeraTerm is a tool for connecting to an SSH server with an SSH client and executing commands.

PuTTY, so if you like.

For Mac, you can use the ssh command from the terminal.

Use SCP to edit files on the server

You can edit files on the server using a protocol built on SSH called SCP.

WinSCP is the SCP client for Windows.

There is software on your Mac that can replace WinSCP on Windows, so install it.

SSH server settings

Use an SSH client to connect to the server with SSH. By default, the SSH port number is 22.

To increase the security of the server, let's change to the SSH port that was additionally released by the packet filter of Sakura VPS earlier.

Time zone setting

Let's set the time zone to "Asia / Tokyo". You can set and check the time zone with timedatectl.

#Set the time zone to "Asia / Tokyo"
sudo timedatectl set-timezone Asia / Tokyo

Check the time zone.

#Check the time zone
timedatectl

Package installation

Install the packages you need for Perl web system development.

apt updates

The apt command is a package installation command, but if apt itself is old, the package download may fail, so Update apt.

sudo apt update

Compiler Linker

Installation of the compiler / linker. Install make, Install gcc, Install g ++. Required for compiling Perl modules.

We recommend that you follow the steps for installing with apt line by line. Because I have a lot of experience going forward without skipping steps or making sure they are installed.

sudo apt install -y make
sudo apt install -y gcc
sudo apt install -y g ++

Text editor

Ubuntu's default vi has limited functionality, so let's do a more convenient install vim.

sudo apt install -y vim

HTTP client

Install curl, which is an HTTP client that is often used in the procedure.

sudo apt install -y curl

SSL communication

Install libssl-dev for SSL communication.

sudo apt install -y libssl-dev

Image processing

Install libpng-dev, Install libgif-dev for image processing , Install libjpeg-dev.

sudo apt install -y libpng-dev
sudo apt install -y libgif-dev
sudo apt install -y libjpeg-dev

Compression / decompression

Install zip for compression / decompression.

sudo apt install -y zip

Network

Let's install net-tools so that ifconfig can be used. The ip command is recommended for Linux, but many server administrators want to use it.

sudo apt install -y net-tools

Repository management

Let's install git for repository management.

sudo apt install -y git

Perl

Install the tools and modules you will often use for your system Perl. Install perl-doc package, Install libio-socket-ssl-perl , Install libreadline-dev, Install libterm-readline-gnu-perl to hold.

sudo apt install -y perl-doc
sudo apt install -y libio-socket-ssl-perl
sudo apt install -y libreadline-dev
sudo apt install -y libterm-readline-gnu-perl

SSL certificate

Install certbot for automatic issuance of SSL certificate.

sudo apt install -y certbot

Creating a directory for data

Let's create a directory for the data for the web application to store the data. Set it so that not only "myapp" users but also other development users can write.

Create a data directory with the mkdir command for "/ datadrive".

sudo mkdir / datadrive

Next, allow the web application user "myapp" and other development users to write to this directory for data.

I have extracted only the part related to the "myapp" user in the above procedure.

sudo groupadd myapp-group
sudo gpasswd -a myapp myapp-group
sudo chgrp -R myapp-group / datadrive
sudo chmod 775 / datadrive
sudo chmod g + s / datadrive

# Verification
ls -l / | grep datadrive

This completes the data directory.

Redis server

Redis server installation and Redis server startup confirmation Redis server startup confirmation Do / a>.

Redis is primarily used for user authentication and session management.

You can also use Redis' Pub / Sub feature to support real-time webs such as push notifications, real-time updates, and real-time chat.

Redis server installation

Install the Redsi server.

sudo apt install -y redis-server

The installation of Redis should be finished soon, but it takes a long time to start because the Redis server fails internally. Are you retrying? Let's wait for a while.

Confirm Redis server startup

Check the startup of the Redsi server.

sudo systemctl status redis-server

The default setting on the Redis server seems to fail to boot if IPv6 is not enabled. IPv6 is not set by default on Ubuntu 18 of Sakura's VPS.

Edit Redis configuration file

Edit the Redis configuration file with the vi editor. Make a backup in case you make a mistake.

sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.bak
sudo vi /etc/redis/redis.conf

The following

bind 127.0.0.1::1

Changed to

bind 127.0.0.1

Start and check Redis

Start and check Redis.

sudo systemctl start redis-server
sudo systemctl status redis-server

Database server MariaDB

Install and check the startup of MariaDB, which is a database server.

Install database server and related libraries

Install the database server MariaDB and related libraries. Install mariadb-client, Install mariadb-server, Install libmysqlclient-dev, Install libmariadb-dev.

sudo apt install -y mariadb-client
sudo apt install -y mariadb-server
sudo apt install -y libmariadbclient-dev
sudo apt install -y libmariadb-dev

Confirm database server startup

Make sure MariaDB is running.

sudo systemctl status mariadb

Connect to MariaDB as root user

First of all, let's connect to MariaDB as the MariaDB root user.

To connect as the root user, run the mysql command with sudo. The user name is root.

sudo mysql -uroot

To end the connection to MariaDB, type quit and press Enter.

quit

Creating a database

Here, create a database for your web application. The database name should be myappdb. Changing the database name requires some ingenuity, but you can change the database name later.

create database myappdb;

Display the database list to see the database you created.

show databases;

Creating an application user

Create a user "myapp" for the application.

#Myapp Create user
create user myapp;

Setting permissions and passwords

Give the myapp user select, update, insert, delete permissions and set a password.

The password is the string that follows "identified by". Please set this part yourself.

#myapp Give user select, update, insert, delete permissions
grant insert, update, delete, select on myappdb. * to'myapp' @'localhost' identified by'eifjutab &';

When installing DBD::mysql with Perl, you need to have connection permission to the test database, so set it.

create database test;
grant all privileges on test. * to'myapp' @'localhost' identified by's3kr1t';

Connection confirmation by application user

Make sure you can disconnect and connect to MariaDB with the newly created "myapp" user.

#Connect as root user
mysql -umyapp -p

For a detailed explanation of MariaDB initial settings, refer to MariaDB initial settings and user creation.

Building a Perl Web development environment

Install Perl in the user environment to build the Perl Web development environment, and Mojolicious.

Perl installation

Here, install "perl-5.20.3" which has as few Perl troubles as possible.

If you prefer a version that has official security fix support, install the latest version of Perl or an earlier version of Perl.

Extract the necessary parts to install Perl in the user environment with perlbrew from the following procedure. If you want to do it with plenv, please refer to the procedure below.

The Perl installation is very time consuming as it is built from source code and further tested. I think it will take about 10 to 20 minutes, so let's drink tea.

Install # perlbrew
wget -O --http://install.perlbrew.pl | bash

Write # perlbrew settings to ~ / .bashrc
echo "source ~ / perl5 / perlbrew / etc / bashrc" >> ~ / .bashrc

Reload # ~ / .bashrc
source ~ / .bashrc

#Install Perl
perlbrew install perl-5.20.3

#Switch to installed Perl
perlbrew switch perl-5.20.3

# Perl version check
perl -v

# installation of cpanm
perlbrew install-cpanm

Launching a web application

Next, install Mojolicious and start the web application.

Please execute the contents of the following articles. Follow the steps other than the last part, "Connecting to a web application with a reverse pixie from Apache".

Allow batch programs to find the library path

Let the batch program find the path to the web application's library. Be prepared to create a batch program in addition to your web application.

Let's add it to "~ / .bashrc".

vi ~ / .bashrc

Add the following contents.

export PERL5LIB = / home / myapp / webapp / lib

Get domain name and set DNS server

Get the domain and associate the domain name with the server IP address. The domain name is required to publish the website on the Internet and to set up the mail sending server.

The IP address of the server for Sakura VPS is described in "IPv4", "Network Information", and "Address" on the VPS control panel.

Please set only the A record in the following articles.

The examples from here in this article use "build-sakuravps-ubuntu18.perlclub.net" as the domain name.

Creating an email address and setting up a DNS server

Let's create an email address and set up a DNS server.

cron

Check the startup of cron, which is a resident application for executing periodic programs.

Confirmation of cron startup

Make sure Apache is running by running "sudo systemctl status cron" in the following article.

Crontab settings

Initialize crontab.

Write cron settings in crontab. You can have a crontab for each user. If you edit the crontab as a user named myapp, it becomes myapp's crontab.

Use the "-e" option to edit the crontab.

crontab -e

The editor will start.

One of the pitfalls of cron is that Perl installed in the user environment is not used. Set the PATH environment variable to resolve this.

env | grep -P'^ PATH'

Set the displayed value as follows.

PATH = / home / myapp / perl5 / perlbrew / bin: / home / myapp / perl5 / perlbrew / perls / perl-5.16.2 / bin: / usr / kerberos / bin: / usr / local / bin: / bin: / usr / bin: / home / myapp / bin

You will receive an email if an error occurs in the automatic execution of cron.Let's set it to.

MAILTO = kimoto_test@perlclub.net

Let's set the batch program to read the path of the library of the web application.

PERL5LIB = / home / myapp / webapp / lib

Save and exit.

Apache

Install and configure Apache, the web server. Set up SSL so that you can connect via HTTPS. Set up a virtual host and reverse proxy to connect to Mojolicious applications.

It is assumed that the domain to be used is correctly linked to the IP address of the server in the acquisition of the domain name and the setting of the DNS server.

Apache installation

Install Apache.

sudo apt install -y apache2

Apache startup confirmation

Check Apache startup.

sudo systemctl status apache2

Apache module

Enable the Apache module required for reverse proxy settings, SSL settings, and WebSocket settings.

mod_ssl, mod_rewrite, mod_headers, mod_proxy, mod_proxy_http, proxy_wstunnel.

Enable # mod_ssl
sudo a2enmod ssl

Enable # mod_rewrite
sudo a2enmod rewrite

Enable # mod_headers
sudo a2enmod headers

Enable # mod_proxy
sudo a2enmod proxy

# mod_proxy_http
sudo a2enmod proxy_http

# mod_proxy_wstunnel
sudo a2enmod proxy_wstunnel

Restart Apache.

sudo systemctl restart apache2

Check the status of Apache.

sudo systemctl status apache2

Virtual host and reverse proxy settings

Set up a virtual host and reverse proxy to forward HTTP connections to Apache to your Mojolicious application. Extract the necessary parts from the following procedure.

Place the configuration file in the following directory.

/ etc / apache2 / sites-available

The directory of this configuration file is explained in detail on the following page.

vi command.

cd / etc / apache2 / sites-available
sudo vi build-sakuravps-ubuntu18.perlclub.net.conf

The contents of the virtual host and reverse proxy configuration files. It is a setting of the reverse proxy corresponding to WebSocket and HTTPS. The SSL certificate specifies the self-SSL certificate that is prepared by default. This will later be replaced with a formal SSL certificate.

<VirtualHost *: 80>
  ServerName build-sakuravps-ubuntu18.perlclub.net

  RewriteEngine on
  RewriteRule (. *)? $Https: //%{HTTP_HOST}%{REQUEST_URI} [L, R = 301]
</VirtualHost>

<VirtualHost *: 443>
  ServerName build-sakuravps-ubuntu18.perlclub.net

  <Proxy *>
    Require all granted
  </Proxy>

  ProxyRequests Off
  ProxyPreserveHost On
  ProxyPass / websocket ws: // localhost: 8080 / websocket
  ProxyPass / http: // localhost: 8080 / keepalive = On
  ProxyPassReverse / http: // localhost: 8080 /
  RequestHeader set X-Forwarded-Proto "https"

  SSLEngine on
  SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
  SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>

Enable configuration file

After completing the settings, enable the configuration file.

sudo a2ensite build-sakuravps-ubuntu18.perlclub.net.conf

Reloading the configuration file

Reload the Apache configuration file. Before reloading, let's test the Apache config file.

sudo apachectl -t

Restart Apache.

sudo systemctl reload apache2

If you can display the website in the following domain, it is successful. The SSL warning will be corrected after this by setting the official SSL certificate.

https://build-sakuravps-ubuntu18.perlclub.net

Get SSL certificate

To receive the request over HTTPS, Let's Encrypt is used to issue an SSL certificate.

Let's Encrypt authenticates the SSL certificate by HTTP request, so it launches a web application.

#Launch web application
hypnotoad webapp.pl

This article assumes that the domain name is obtained as "build-sakuravps-ubuntu18.perlclub.net" and that the correspondence between the domain name and the IP address in the DNS server has been described. Please rewrite the corresponding part as appropriate.

This article assumes that you have completed the work of creating an email address and can send and receive with the email address "kimoto_test@perlclub.net". Please rewrite the corresponding part as appropriate.

To get the SSL certificate with Let's Encrypt, execute the certbot command.

sudo certbot certonly --agree-tos --non-interactive -d build-sakuravps-ubuntu18.perlclub.net --webroot -w /home/myapp/webapp/public --email kimoto_test@perlclub.net

If the following message is included, it is successful.

Congratulations! Your certificate and chain have been saved at:

The SSL certificate will be created in the following directory. From the top, the SSL certificate file, the SSL certificate private key file, and the intermediate certificate file.

/etc/letsencrypt/live/build-sakuravps-ubuntu18.perlclub.net/cert.pem
/etc/letsencrypt/live/build-sakuravps-ubuntu18.perlclub.net/privkey.pem
/etc/letsencrypt/live/build-sakuravps-ubuntu18.perlclub.net/chain.pem

Let's see it in action with the ls command.

sudo ls -l /etc/letsencrypt/live/build-sakuravps-ubuntu18.perlclub.net

Use the "certbot renew" command to renew the SSL certificate with Let's Encrypt. All the SSL certificates obtained will be checked for renewal, and if the renewal period is within one month, the certificate will be renewed.

sudo certbot renew -q --no-self-upgrade --post-hook "systemctl reload apache2"

If this command is set to automatic update of cron, it will be updated automatically. I will explain the cron settings later.

Add SSL certificate to Apache config file

Add the SSL certificate to the Apache config file.

First, create an SSL basic configuration file. This is intended to be used in common later when the number of web applications increases.

vi editor.

cd / etc / apache2
sudo vi conf-available/ssl-basic.conf

Add the following content.

SSLEngine on
SSLProtocol all -SSLv2 -SSLv3
SSLCipherSuite ECDHE-ECDSA-CHACHA20-POLY1305: ECDHE-RSA-CHACHA20-POLY1305: ECDHE-ECDSA-AES128-GCM-SHA256: ECDHE-RSA-AES128-GCM-SHA256: ECDHE-ECDSA-AES256-GCM-SHA384 -AES256-GCM-SHA384: DHE-RSA-AES128-GCM-SHA256: DHE-RSA-AES256-GCM-SHA384: ECDHE-ECDSA-AES128-SHA256: ECDHE-RSA-AES128-SHA256: ECDHE-ECDSA-AES128-SHA : ECDHE-RSA-AES256-SHA384: ECDHE-RSA-AES128-SHA: ECDHE-ECDSA-AES256-SHA384: ECDHE-ECDSA-AES256-SHA: ECDHE-RSA-AES256-SHA: DHE-RSA-AES128-SHA256: DHE -RSA-AES128-SHA: DHE-RSA-AES256-SHA256: DHE-RSA-AES256-SHA: ECDHE-ECDSA-DES-CBC3-SHA: ECDHE-RSA-DES-CBC3-SHA: EDH-RSA-DES-CBC3 -SHA: AES128-GCM-SHA256: AES256-GCM-SHA384: AES128-SHA256: AES256-SHA256: AES128-SHA: AES256-SHA: DES-CBC3-SHA :! DSS
SSLHonorCipherOrder on

Then edit the web application's configuration file. Change the description part of the self SSL certificate to the SSL certificate created by Let's Encrypt.

cd / etc / apache2 / sites-available
sudo vi build-sakuravps-ubuntu18.perlclub.net.conf

This is the setting part of the original SSL certificate.

  SSLEngine on
  SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
  SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

Please rewrite as follows.

<VirtualHost *: 80>
  ServerName build-sakuravps-ubuntu18.perlclub.net

  RewriteEngine on
  RewriteRule (. *)? $Https: //%{HTTP_HOST}%{REQUEST_URI} [L, R = 301]
</VirtualHost>

<VirtualHost *: 443>
  ServerName build-sakuravps-ubuntu18.perlclub.net

  <Proxy *>
    Require all granted
  </Proxy>

  ProxyRequests Off
  ProxyPreserveHost On
  ProxyPass / websocket ws: // localhost: 8080 / websocket
  ProxyPass / http: // localhost: 8080 / keepalive = On
  ProxyPassReverse / http: // localhost: 8080 /
  RequestHeader set X-Forwarded-Proto "https"
  
  #SSL certificate
  Include conf-available/ssl-basic.conf
  SSLCertificateFile /etc/letsencrypt/live/build-sakuravps-ubuntu18.perlclub.net/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/build-sakuravps-ubuntu18.perlclub.net/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/build-sakuravps-ubuntu18.perlclub.net/chain.pem
</VirtualHost>

Test if the Apache configuration file is correct.

sudo apachectl -t

After updating the SSL certificate, restart Apache with the systemctl command to reload the SSL certificate.

sudo systemctl reload apache2

It's OK if you visit the website and the SSL certificate is valid.

Automation of SSL certificate renewal with Let's Encrypt

Manually updating the SSL certificate with Let's Encrypt is tedious. Let's set it to update automatically once a week.

If it is once a day, if the certbot is wrong, it may issue a large number of SSL certificates and you may not be able to obtain the SSL certificate for a while, so please stop it. But the risk is avoided.

If it is once a month, I feel that it is easier to check if it is wrong if you increase the frequency a little more.

So, here, I will update it once a week at 9 o'clock on Monday.

To modify the root crontab, add "sudo" and start crontab.

sudo crontab -e

Set the contents of crontab as follows. For the e-mail address, specify the e-mail address that you can receive.

MAILTO = kimoto_test@perlclub.net

0 9 * * 1 certbot renew -q --no-self-upgrade --post-hook "systemctl reload apache2"

Systemd management of web applications

Let's register the Mojolicious application with systemd so that the Mojolicious application will also start automatically when the OP is restarted.

Here, it is assumed that you have installed Perl 5.20.3 using perlbrew. It is assumed that the Mojolicious application was also created with the name "Webapp" in the above procedure.

systemd configuration file

Register with systemd with the service name "webapp". Create a new file as follows.

sudo vi /lib/systemd/system/webapp.service

vi Open the command and write the following content.

[Unit]
Description = My Mojolicious application
After = network.target

[Service]
Type = forking
PIDFile = / home / myapp / webapp / script / hypnotoad.pid
ExecStart = / home / myapp / perl5 / perlbrew / perls / perl-5.20.3 / bin / hypnotoad / home / myapp / webapp / script / webapp
ExecReload = / home / myapp / perl5 / perlbrew / perls / perl-5.20.3 / bin / hypnotoad / home / myapp / webapp / script / webapp
ExecStop = / home / myapp / perl5 / perlbrew / perls / perl-5.20.3 / bin / hypnotoad / home / myapp / webapp / script / webapp --stop
KillMode = process
User = myapp
Group = myapp-group
Environment = MY_SERVER_ENV = my_dev

[Install]
WantedBy = multi-user.target

Service activation

Let's activate the service.

sudo systemctl enable webapp

If the Mojolicious application is already running, stop it.

/home/myapp/perl5/perlbrew/perls/perl-5.20.3/bin/hypnotoad /home/myapp/webapp/script/webapp --stop

Start your Mojolicious application under systemd's control.

sudo systemctl start webapp

Let's check if the Mojolicious application is running.

sudo systemctl status webapp

Check if the Mojolicious application can be restarted.

sudo systemctl reload webapp

Let's see if the Mojolicious application can be stopped.

sudo systemctl stop webapp

Let's launch the Mojolicious application.

sudo systemctl start webapp

Restart the OS

Do Restart the OS and see if the Mojolicious application is automatically restarted.

sudo reboot

Send email

Allows you to send mail with the sendmail command. Install postfix to be used as an email sending server. Set the cloud mail service SendGrid to work with postfix.

Postfix installation

Mail sending server Install postfix. If you install postfix, which functions as an email sending server, you can use the sendmail command to send emails.

sudo apt install -y postfix

When you execute the installation command, a GUI-like screen will appear.

Press the right side of the keyboard and Ok will illuminate. Press Enter.

Select "No configuration" with the up and down keys. Press the right key to recommend Ok, so press Enter.

postfix will be installed.

Apply for SendGrid

From the SendGrid top page, enter your email address to apply.

SendGrid application

Your account will be issued by the next business day.

Regarding the cooperation between SendGrid and postfix, the one explained in Cloud mail delivery SendGrid and Postfix cooperation is written below only for the necessary parts. increase.

Get the API key

Get your API key. Log in to the SendGrid management screen. Click "Settings" in the left sidebar. Click "API keys".

Click "Create API Key" in the upper right.

Enter any name in "API Key name". (Example) perlgenki.

Change "API Key Permissions" to "Full Access".

The created API key is displayed. It will only be displayed once, so copy and paste it and save it somewhere. If you turn off the screen, delete it and recreate it.

Edit postfix configuration file

Edit the postfix configuration file with the vi command.

sudo vi /etc/postfix/main.cf

Describe the following contents. No other settings are required, just this is OK.

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash: / etc / postfix / sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]: 587

Now create the password file described in the settings above.

sudo vi / etc / postfix / sasl_passwd

Specify the API key obtained by SendGrid. Replace the API key part with the obtained API key.

[smtp.sendgrid.net]: 587 apikey: API key

Then make sure that this file is restricted to read and write only by the root account, and use the postmap command to update the hashtable for Postfix to use this new file:

sudo chmod 600 / etc / postfix / sasl_passwd
sudo postmap / etc / postfix / sasl_passwd

Finally, start Postfix with systemctl.

# Start-up
sudo systemctl start postfix

#If you want to restart, use the following
sudo systemctl restart postfix

Check the status of Postfix.

sudo systemctl status postfix

Send an email with the sendmail command

Let's send an email with the sendmail command. Please set [Email sender] and [Email destination] to an email address that can be sent.

echo Hello | sendmail -f [mail source] [mail destination]

In sendmail, the standard input is the body.

-f specifies the mail sender with from. If there is no sender of the email, the email is often rejected, so I have specified it.

After that, specify the destination of the email. When testing, both the destination and the source can be the same.

# Sending example
echo Hello | sendmail -f kimoto_test@perlclub.net kimoto_test@perlclub.net

It is OK if you can send an email.

Completed !!!

The construction of the Web system development environment is completed with Sakura VPS + Ubuntu 18 + Perl. Thank you for your hard work.

Associated Information