- Make sure you are on Mac OS or Linux, if your are on windows please use Git Bash instead of default terminal
And then connect to your ops server using ssh command with password or ssh key:
ssh root@SERVER_IP_ADDRESS
- Go to where your ktor server exists, create folder called "keys" and cd in there, then copy the absolute path of this folder then run this command:
ssh-keygen -m PEM -t rsa
and paste the following: "${theCopiedPath}/id_rsa" then enter, and enter again (no password)
create a folder in your ktor server project called "keys" or anything you prefer don't forgot to ignore it in .gitignore keys/ cut the keys to your this folder
- Copy the key to your server:
ssh-copy-id -i id_rsa <user>@<host>
- Now exit from ssh and login to your Ubuntu server via the new SSH key:
ssh -i id_rsa <user>@<host>
- Update dependencies: Important note: if you are on root user make sure sudo doesn't exists in the command, otherwise on distros like centos and debain will throw an error, but if you are not on root user, then please type sudo first example: "sudo apt update" and because there are commands require sudo permission (like open as admin in windows) you need to type sudo at the start of the command, if the command faield, it will tell you it need sudo permission, again you should really only use sudo if you are not on root user, apt require sudo, systemctl require sudo, the files we are editing in this tutorial also require sudo pemrission (sudo nano ...)
apt update (to update the package sources) apt upgrade -y (to update the packages) (-y is for auto accpet as yes) apt dist-upgrade (to load the new packages for the new version) apt autoremove (to remove packages that are no longer needed) apt clean (to clean the cache of the package sources)
- Install Java: I do prefer java 17 for now, I'm using ubuntu and the package name diffrent from package manager to another it depends on your distro
Please search more about that
In ubuntu:
apt install openjdk-17-jdk
or without the jdk (preferred):
apt install openjdk-17-jre-headless
or just (not recommend):
apt install default-jdk
- Add custom ssh algorithm (optional): Open /etc/ssh/sshd_config:
nano /etc/ssh/sshd_config
Put this string in there, save with Ctrl+S and exit with Ctrl+X (optional): KexAlgorithms [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
and then restart the sshd service:
systemctl restart sshd
- Install nginx:
apt install nginx
if you want auto configurations between certbot and nginx this plugin
apt install python3-certbot-nginx
Do it manually if python3-certbot-nginx plugin doesn't work
-
Configure nginx default configurations file: Copy the contents of nginxServerConfigurationsS and paste it into default nginx configurations using
-
Generate ssl certificate by running this command:
for auto configurations with nginx:
certbot certonly --nginx --domain example.com --cert-name example.com
manually (which is what will we do if python3-certbot-nginx plugin don't work):
certbot certonly --manual --preferred-challenges dns --domain example.com --cert-name example.com
If you by mistake enter wrong domain, you can delete it with the following:
certbot delete --cert-name wrong.example.com
then remove the default configurations:
rm /etc/nginx/sites-available/default
create it again using:
nano /etc/nginx/sites-available/default
copy the contents of nginxServerConfigurations.txt and paste it (ctrl + v) to the nano text editor
Replace the domain with yours in server_name in both server blocks, and in ssl_certificate and ssl_certificate_key
Then save using ctrl + x and then y (yes)
- Restart nginx:
In ubuntu and modern linux systems use:
systemctl restart nginx.service
in old ones:
service nginx restart
Or just restart your server
- Create ktor server service: To automataiclly start the ktor server when the the system start You need to create a linux service
copy the contents of ktorServerService.txt ( if your java path id diffrent than mine, please change it in ExecStart, if you install openjdk-17-jdk, then you are good to go )
nano /etc/systemd/system/ktor-server.service
paste it using ctrl + v and then ctrl + x and click y (yes)
- Configure gradle to deploy to ktor server
Go to your build.gradle.kts in ktor server
and use build.gradle.kts as template, look at it and find it what things to remove and add, edit, or just copy the things you need sync the project
- Deploy the server
now go to gradle tasks and run "cleanAndDeploy" every time you made a changes note: if you have a lot of users, you might want implement service unavaliable functionallity
example: intercept(ApplicationCallPipeline.Call) { val file = File(getUserDirectory(), ".locked") if (file.exists()) { call.respondJsonText(HttpStatusCode.ServiceUnavailable, "Sorry, the api is undergoing maintenance.") return@intercept } }
so in production server whwere you have a lot of users and payments and important things before you deploy, make sure to lock the server, wait for a few mintues and then deploy, then unlock the server again
- Launch the service:
systemctl start ktor-server
Create a symlink to automatically launch the service on boot up:
systemctl enable ktor-server
make sure you have no error by view the log:
systemctl status ktor-server
click q to exit
-
Others (optional): install mongodb and all other tools you need, or just use sepereated service like cloud mongodb for simpleitity (don't forgot to add the ip address of the server in network access) I might create another tutriol for that
-
Make sure app server folder: the app folder is created by default:
the 'app' should be the same as serverFolderName variable in build.gradle.kts
cd ~/app/
ls
if not, please review build.gradle.kts and re run the deploy task if the issue still exists
you can run those commands:
cd ~
mkdir app
As I said, if you change this folder name, make sure to do the same in the build.gradle.kts
- Create a folder for your server files (images, verifications, assets) You might headrd of .well-known which is a folder where you put some files which required for verify that you are owning the domain and the server for example for android app links and apple site link (universal links) and it's also required for ssl certificate but I choose to use dns instead in the command
go to your routing block and add the following:
val filesFolder = "/files/".getFileFromUserDirectory()
staticFiles("/", filesFolder)
staticResources("/", "static")
you need to have those extension functions somewhere in your project:
fun getUserDirectory(): String = File(object {}.javaClass.protectionDomain.codeSource.location.toURI().path).parent fun String.getFileFromUserDirectory(): File = File(getUserDirectory(), this)
don't forgot to deploy the project again, then run this command:
mkdir -p ~/app/files/
change 'app' with the one serverFolderName in build.gradle.kts and files what the one that you choose
then create any kind of files there and I hope it works for you
- Use cloudflare (Highley recommened) it might need another tutorial create a account in cloudflare.com Add website Chagne nameservers Wait for some time until you cloudflare verify the change Add dns records
A record with name "@" and value with your server Ip address Cname recored with name "www" and with "@" value
Wait for few hours
and you are good to go! I might need improve this tutorial and create video for it