Mojolicious app released on Sakura rental server standard
I created a Hello World application, ran it on my machine, and viewed it in a browser. But in the end, you'll want to use the internet to show others the apps you've created.
There are several steps to publish a website on the Internet, but this time I would like to use Sakura's rental server standard as the easiest method.
Sakura Rental Server Standard is a service for publishing websites on the Internet. Called a shared rental server, you can run and view files created using CGI.
The Hello World application can actually be implemented as a CGI as well. Therefore, you can publish the application created on your own machine using Sakura Rental Server Standard.
Apply for Sakura Rental Server Standard
You can apply for Sakura Rental Server Standard from this page. It can be used for about 500 yen a month, and the disk capacity is 100GB, which is quite large, so it is a good deal. Also, as I will explain later, SSH can be used to execute commands on the server.
Please follow the instructions on this site to proceed.
Connect to the server using SSH
When Sakura's rental server standard becomes available, let's proceed with the procedure immediately. First of all, I would like to try connecting to the server using SSH.
SSH is a secure communication protocol for executing commands to a server. In the rest of this work, you'll need to implement commands when you change to a different version of Perl or change CGI permissions.
To do this, you need to connect to the server using SSH. If you have Mac OS X, the ssh command is already available. If you open the terminal, you can use the ssh command, so no preparation is required.
For Windows, install an SSH client called Teraterm. With Teraterm, you can use SSH to execute commands on the server.
Installing Tera Term on Windows
Download and install Tera Term from the following site.
After the installation is complete, start Tera Term. You should see a window called "New Connection".
In the Host part, enter the host name you received when you applied for Sakura Standard.
xxxxx.sakura.ne.jp
The user name is entered in place of xxxxx.
The "Service" part specifies SSH.SSH
Press the "OK" button to open a window called SSH Authentication.
Enter the user name and password received by Sakura Standard in the "User name" and "Passphrase" parts.
You can connect to the server by clicking OK.
Connecting to a server on Mac OS X
The ssh command is available by default on Mac OS X, so open a terminal and run the SSH command.
ssh yyy@xxx.sakura.ne.jp
Enter your user name in the xxx field. For the xxx part, enter the corresponding part of the given host name.
You will be asked for a password, so enter the password you received when you applied for Sakura Standard.
You can connect to the server.
Change the version of Perl to use
To use Mojolicious, you need Perl 5.10.1 or higher. Sakura Standard makes it easy to switch between Perl versions.
SSH uses commands to perform various operations. First, let's view the file with the Perl version specified. You can execute the command by typing the following command and pressing Enter.
cat .perl.version
Note that there is a space after cat. Keep in mind that you press Enter whenever you run a command.
Next, since Perl 5.14 is available in Sakura Standard as of 2015, rewrite this setting.
echo "5.14"> .perl.version
If you want to restore it, you can restore it with the following command.
echo "5.8"> .perl.version
Let's check the version of Perl with the following command.
perl -v
If you see Perl 5.14 as shown below, you're successful.
This is perl 5, version 20, subversion 1 (v5.20.1) built for amd64-freebsd
Install Mojolicious
To install Mojolicious, it's easy to use a tool called cpanm that installs Perl modules. Download cpanm and install Mojolicious.
Download cpanm
First of all, download cpanm using the command curl.
curl -LOk http://xrl.us/cpanm
Check if cpanm has been downloaded using the command to display the file list.
ls
Make sure you see a file called cpanm.
Install Mojolicious
Next, let's install Mojolicious using cpanm.
perl cpanm Mojolicious
The following screen will be displayed.
->Workingon Mojolicious Fetching http://www.cpan.org/authors/id/S/SR/SRI/Mojolicious-6.08.tar.gz ... OK Configuring Mojolicious-6.08 ... OK Building and testing Mojolicious-6.08 ... OK Successfully installed Mojolicious-6.08 1 distribution installed
Let's make sure that Mojolicious is installed. The command perldoc is a command to view Perl documentation, but if you have Mojolicious installed, you can view the documentation.
perldoc Mojolicious
If you have Mojolicious installed, you'll see the documentation. Press "q" to finish viewing the document.
q q
Mojolicious application fix
In order to work with Sakura Standard, you need to modify the "webapp.pl" script created earlier.
Add Shebang
Below is the script I created earlier.
use Mojolicious::Lite; get'/' => sub { my $self = shift; $self->render(text =>'Hello World'); };; app->start;
Add the following line to the beginning of this script. This is called a shebang and is an instruction to run CGI using Perl.
#! / usr / bin / env perl
Also, add the following two lines to read the directory where you installed Mojolicious as the path of the library.
use FindBin; use lib "$FindBin::Bin / ../ perl5 / lib / perl5";
The script looks like this:
#! / usr / bin / env perl use FindBin; use lib "$FindBin::Bin / ../ perl5 / lib / perl5"; use Mojolicious::Lite; get'/' => sub { my $self = shift; $self->render(text =>'Hello World'); };; app->start;
(Reference) FindBin
Even when developing in your own environment, you can leave it as it is.
Rename file
When effective with Sakura Standard, the extension of the file name must be ".cgi".
Change "webapp.pl" to "webapp.cgi".Upload Mojolicious application
After installing Mojolicious, the next step is to upload the Hello World application you created.
In the case of Windows, in the case of Windows, if it is Windows 7 or later, the FTP software is integrated in Explorer, so use this. If you are using Windows 7 or earlier, you can upload files by using software called FFFTP.
For Mac OS X, FTP software is installed by default, so use it.
For Windows (explaining for Windows 7 or later)
From Explorer, use FTP to connect to the server. Anything is fine, just double-click the folder to launch Explorer.
Empty address bar at the topYou can enter the URL yourself by clicking the white part. Enter the following here.
ftp: yyy@xxx.sakura.ne.jp
yyy@xxx.sakura.ne.jp is the same as the one used when connecting to SSH.
Then you can connect to the server via FTP. You will be asked for your user name and password, so enter them.
When you connect, you will find a folder called www. Place the files you want to publish on the Internet in this folder.
I think I saved the Hello World application as "webapp.cgi", but save it in the www folder.
This completes the file upload.
For Mac OS X
The Finder is for viewing files, but you can use it to make an FTP connection to the server.
To make an FTP connection using the Finder, select Go> Connect to Server from the Finder menu.
Then enter the server you want to connect to as follows:
ftp: yyy@xxx.sakura.ne.jp
yyy@xxx.sakura.ne.jp is the same as the one used when connecting to SSH.
Then you can connect to the server via FTP. You will be asked for your user name and password, so enter them.
When you connect, you will find a folder called www. Place the files you want to publish on the Internet in this folder.
I think I saved the Hello World application as "webapp.cgi", but save it in the www folder.
This completes the file upload.
Preparation for running the application
Some preparation is required for the application to work.
Set file permissions to 755
In order to execute a CGI file, the permission of the CGI file must be set to 755. Remember here as a spell.
First, connect to the server using SSH using the procedure described earlier. For Windows, use Tera Term, and for Mac OS X, use the ssh command from the terminal.
And first, change to the directory where webapp.cgi exists. Use the "cd" command to change directories.
cd ~ / www
"~" Is a symbol that represents your home directory. Navigate to the "www" folder in your home directory.
Next, change the permissions of the "webapp.cgi" file to "755".
chmod 755 webapp.cgi
For Windows, set the line feed code to "LF"
In the case of Windows, the line feed code is "CR + LF" by default. In order to work with Sakura Standard, it is necessary to convert the line feed code to "LF".
Use the following command to unify the line feed code to "LF".
perl -i.org -pe's / \ r\n /\n / g'webapp.cgi
It's a hassle to do this every time. If you use Windows text editing software such as Hidemaru or Sakura Editor, it is convenient because you can unify it to "LF" in advance.
Check if the Perl script is correct
If the Perl script is incorrect, the CGI will fail to execute, so check if the CGI is correct.
perl -c webapp.cgi
You can use the command "perl -c" to verify that the Perl script syntax is correct. If you see the following, the Perl script is written correctly.
webapp.cgi syntax OK
Access the Mojolicious application
Let's access the Mojolicious application now. Try entering the URL from your browser.
http://xxx.sakura.ne.jp/webapp.cgi
If "Hello World" is displayed, it is successful. Now that you've published it on the internet, everyone can see your Mojolicious application. congratulation!