To run a java website you will need a java hosting or VPS or dedicated server
If you have a static ip and fast internet you can use your computer or laptop like a hosting for your java website only you need is to install ubuntu on it.
In this totorial we will talk about how to run java website on VPS , you can get on for 10$ a mounth, .
1) Get a VPS, with minimal ram - 2GB and SSD - 15-20GB sapce.
2) Install ubuntu on it, if you have a chose to install ubuntu desktop than do it, if not ubuntu server will be good to.
3) Install mysql-server by using comand line
3.1) First, update the apt package index by typing:
sudo apt update
3.2) Install the MySQL -server package with the following command:
sudo apt install mysql-server
3.3) Once the installation is completed, the MySQL service will start automatically. To check whether the MySQL server is running, type:
sudo systemctl status mysql
3.4) MySQL server package comes with a script called mysql_secure_installation that can perform several security-related operations.
sudo mysql_secure_installation
3.4.1) If you will need to create multiple tables and multiple userse you may install phpmyadmin or MySQL Workbench to add or edit databases or users from interface
3.5) In this metod we will add a table and user from ubuntu comand line
Login to mysql
sudo mysql -u root
3.6) Create new mysql table
CREATE DATABASE your_table_name
3.7) Check the list of tables
show databases;
3.8) Create new mysql user
CREATE USER 'your_user_naeme'@'localhost' IDENTIFIED BY 'your_strong_pasword';
3.9) Flush privilegise
FLUSH PRIVILEGES;
3.10) Check the list of users
SELECT user,authentication_string,plugin,host FROM mysql.user;
3.11) Add privilegies to user
GRANT ALL PRIVILEGES ON your_table_name.* to your_user_naeme@localhost;
3.12) In case you want to change the password to your user
ALTER USER 'your_user_name'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';
3.13) Exit from mysql
exit;
4) To conect a domain with our java website we will need to use nginx as server
4.1) Install nginx
sudo apt update sudo apt install nginx
4.2) Adjusting the Firewall
sudo ufw app list
You should get a listing of the application profiles:
Output
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
4.3) Add Nginx to you Firewall
sudo ufw allow 'Nginx HTTP'
4.4) Check nginx status
systemctl status nginx
if all good you will see something like this:
nginx.service - nginx - high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Mon 2020-09-21 15:50:32 EDT; 1 day 17h ago
Docs: http://nginx.org/en/docs/
Process: 90282 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exi>
Main PID: 90291 (nginx)
Tasks: 2 (limit: 4657)
Memory: 16.3M
CGroup: /system.slice/nginx.service
├─90291 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.>
└─90292 nginx: worker process
4.5) Ensure NGINX is running and and enabled to start automatically on reboot:
sudo systemctl start nginx
sudo systemctl enable nginx
if everythink it's working enter in the browser your server ip and you will see a message like
5) Point the domain to our VPS
5.1) Go to your domain settings
5.2) Find where you can edit the DNS
5.3) Point your domain to your VPS by add "A" record from DNS
1) A record
Host Name: leave empty
Destination IPv4 Address:your vps IP address
TTL : 7200
2) A record
Host Name: *
Destination IPv4 Address: your vps IP address
TTL : 7200
6) Wait until the domainis will be connected, will take from 1hour to 24 hour
when the domain will be connected, by accessing the domain name from the browser you should seee a messege like this.
7) Install Java
7.1) First check if java is installed
java -version
If you will see the next result "openjdk version "1.8.0_242"
than go to point 8
7.2) Update repositories
sudo apt-get update
7.3) Install JDK
sudo apt-get install openjdk-8-jdk
7.4) Verify the version of the JDK:
java -version
8) Upload your jar file in /var/www/yourdomain.com/public_html/, if this path won't exist than you have to create it
9) Make sure your jar is working corectly you can run your jar in comand line, how to setup your application.property you can check it here
java -jar /var/www/yourdomain.com/public_html/myjarfilename.jar --spring.config.location=/var/www/yourdomain.com/public_html/config/application.properties
10) Open your browser on your VPS and enter the url http://localhost:8889/, if you done everything correct than you will see the wdsedbsite working
11) Make your website live
11.1) some time when you install nginx all the config file for new domain you have to put in /etc/nginx/conf.d/ so in this case we will create a file call yourwebsite.com.conf by using comand line, if you open the directory /etc/nginx/ and see this two folders sites-available and sites-enabled than skip this point go to point 11.2
sudo nano /etc/nginx/conf.d/yourwebsite.com.conf
After add this text (change text "yourwebsite.com" with your real domain name)
server {
listen 80;
listen [::]:80;
server_name yourwebsite.com www.yourwebsite.com;
location / {
proxy_pass http://localhost:8889/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
By save changes press:
CTRL+O and pres ENTER
By exit from file editing press: CTRL+X
11.2)When open the directory /etc/nginx/ and see this two folders sites-available and sites-enabled than fallow this steps
Create a file call: yourwebsite.com.conf by using comand line
sudo nano /etc/nginx/sites-available/yourwebsite.com.conf
After add this text (change text "yourwebsite.com" with your real domain name)
server {
listen 80;
listen [::]:80;
server_name yourwebsite.com www.yourwebsite.com;
location / {
proxy_pass http://localhost:8889/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
By save changes press:
CTRL+O and pres ENTER
By exit from file editing press: CTRL+X
11.3) make a copy in folder sites-enabled
sudo ln -s /etc/nginx/sites-available/yourwebsite.com.conf /etc/nginx/sites-enabled/yourwebsite.com.conf
11.5) Check if you didn't done any error by enter this comand
sudo nginx -t
11.5) If all good restart nginx
sudo systemctl restart nginx
12) After all good restart ubuntu by enter this comand line
sudo reboot
13) Set the Spring Boot application as a service to start on reboot:
13.1) create new file in system
sudo nano /etc/systemd/system/javaspringboot
.service
and paste this text
[Unit]
Description=Spring Boot HelloWorld
After=syslog.target
After=network.target[Service]
User=username
Type=simple
[Service]
ExecStart=/usr/bin/java -jar /var/www/yourdomain.com/public_html/myjarfilename.jar --spring.config.location=/var/www/yourdomain.com/public_html/config/application.properties
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=javaspringboot
[Install]
WantedBy=multi-user.target
After press: CTRL+O and pres ENTER
After press: CTRL+X
13.2) make the service start automaticly when ubuntu restart
sudo systemctl enable javaspringboot
13.3) Start the service
sudo systemctl start javaspringboot
13.4) Check the status is active
sudo systemctl status javaspringboot
to exit from check status press : CTRL + Z
14) All done!!!