Skip to content

Instantly share code, notes, and snippets.

@OroArmor
Last active August 27, 2023 23:14
Show Gist options
  • Save OroArmor/0093803f4cfc9666d7948b8ce768c71f to your computer and use it in GitHub Desktop.
Save OroArmor/0093803f4cfc9666d7948b8ce768c71f to your computer and use it in GitHub Desktop.

How to setup a Maven Server

Introduction

The intention of this instruction manual is to teach moderate to advanced Java Developers who wish to publish their Java programming projects to a Maven server with the intent of allowing others to use these projects. Once a Java developer begins to make multiple projects, and often libraries, it becomes useful to share code with others. While some other solutions exist, this manual will cover a solution that leaves control of the code in the hands of the developer. This manual will guide programmers through the acquisition of a physical server with Linode, setup of a web server with nginx, distribution of the server with Google Domains and DNS, and SSL certification with certbot. The manual will also cover an optional process of testing your server with a publishing test.

While this guide will use Google Domains for domain purchase and DNS, many other DNS providers like CloudFlare may be used. In addition, this guide will use Git Bash as the terminal of choice.

Required Knowledge/Materials

  • What a terminal is.
  • A SSH client that is set up
  • Basic knowledge on:
    • nginx
    • DNS
    • Maven Servers

Warnings

This tutorial will not cover web security, only covering basic authentication. While the chance of an attack is unlikely, doing some research into proper web security and authentication is recommended. You could lose control of your server or webpage by using insecure passwords.

Hosting infrastructure on the web is not free, and the server will cost upwards of seventy-two dollars a year. This manual is not liable for any extra costs occurred.

Instructions

Setting up a Linode Server

  1. Create and login to your account on Linode. Go to cloud.linode.com. create an account.
  2. Click on the button “Create Linode.” This button will open to a screen for the creation settings. Make sure that “Distributions” is selected underneath “Linodes/Create.” There are five sections that you will modify. Figure 1, shown below, shows an example for the first three of the sections.
    1. Selecting a Distribution. If you have prior Linux experience, you should choose the Linux distribution you are most familiar with. For those without Linux experience, Ubuntu 21.10 (or latest at time of execution), is the best option.
    2. Selecting a Region. If you have a concentrated audience for your server, selecting a location close to that is optimal. Otherwise, select the location closest to you.
    3. Selecting a Plan. This step is extremely important as extra costs could occur. Select the “Shared CPU” option in the horizontal selection menu. By using a Shared CPU server, you can save about twenty-five dollars a month for features you do not need. Because nginx is not memory or CPU intensive, selecting this option makes the most sense. From the list of plans, select the “Nanode 1GB” plan. This plan comes with 25GB of storage. If you require more storage, follow the instructions in Appendix A on how to upgrade the storage.
    4. Adding a label. Create an identifiable name that describes the functionality of this Linode server.
    5. Adding a Root Password and SSH Key. Use a strong and secure password for the root user of the Linode server. Keep note of this password as future steps will need it; it will be referred to as the “root password.” In addition, upload and select your SSH key in the “SSH Keys” section. For the label, the name of the computer you are currently using is recommended. For the public key, you can find it in the .ssh folder in your home directory or user folder. The file is a text file and will end in .pub; copy the contents into the “SSH Public Key” text box.
  3. Finalize. Click “Create Linode” on the right of the screen. You have successfully created a Linode server on which to host your maven repository. You should now be on a page showing information about the server; this will be referred to as the “server information page” in future steps.

Setting up a Domain with Google Domains

  1. Purchase a domain. Navigate to domains.google.com/registrar/search.
    1. Search for your desired domain name. Google Domains will either show an exact match for your desired domain name or indicate that someone else has already registered the domain name. Below the exact match, there is a list of similar domain names to purchase. Select either the exact match or a similar name and click the cart button for the domain name. The selected name will be referred to as “domain name” in future steps.
    2. Purchase the domain. Click on the cart in the top right corner. Click the checkout button on the bottom. Follow the rest of the instructions provided by Google.
  2. Setup DNS records. After purchasing your new domain, select the “My Domains” tab on the left. Click on the domain name you just registered.
    1. Click on the “DNS” tab on the left. Continue by clicking on “Manage custom records.”
    2. Adding the DNS records. You will add two records, one for IPv4 and one for IPv6. Both records will require clicking on “Create new record” on the left of the center content. Figure 2 shows an example of how a filled-out DNS record looks. The values on the right are placeholders and should be filled out according to the following steps. Figure 3 also shows where you can find the IP Addresses on the information pages.
      1. Choosing your subdomain. Before adding the records, decide if you want your server to be on a subdomain. In most situations you will, as you will host an actual webpage on the domain name. This tutorial will use maven as the subdomain. Remember the full subdomain name for later, it will be referred to as the server URL.
      2. Setting up the IPv4 record. Going from left to right: enter the subdomain, then select “A” as the type, skip over the 3600, and then copy and paste the first IP address from the top of the server information page.
      3. Setting up the IPv6 record. Going from left to right: enter the subdomain, then select “AAAA” as the type, skip over the 3600, and then copy and paste the second IP address from the top of the server information page.
    3. Click Save.

Setting up nginx

  1. Connect to the Server with SSH. From the server information page, copy the “SSH Access” value. See Figure 3 for the location of the value. Open your terminal program and paste the command in. This will require you to type in the root password from eariler.
  2. Installing nginx. To get nginx, run the command sudo apt update and thensudo apt install nginx. These commands will ensure your server has the latest nginx version installed.
  3. Configuring nginx. nginx uses a config file to properly route incoming traffic. The file provided at gist.github.com/OroArmor/cffc0358b1d3f721791ef24707929176 is a basic template. Download and save this file to your computer. A more in-depth explanation of how the configuration file works is in #Appendix-B.
    1. Modifying the template. There are two placeholder values in the template: maven.example.com and Example.com’s publishers only. The first placeholder is for the server URL you configured in earlier steps. The second placeholder is the message that the server returns when an unauthorized user tries to publish to your server. Replace these values on the saved file on your computer.
      1. Customizing the storage location. Only more advanced Linux users or those wishing for more control over the server’s storage location should follow this step. Changing the /home/maven value on line 22 will change where the maven server saves the files published to it.
    2. Applying the template.
      1. Locating the configuration file. Run cd /etc/nginx to navigate to where the nginx configuration files exist.
      2. Replacing the default configuration file. Run rm nginx.conf && vim nginx.conf; this command will delete the default configuration file and open a file in the text editor Vim.
      3. Supplying your configuration file. Copy over the edited configuration file from the previous step, then paste it into Vim with Shift + Insert.
      4. Saving the configuration file. Press the escape key, then type :wq to exit the text editor.
  4. Create the storage folder. Run mkdir /home/maven; this command will create a new folder on the server where the server will store files. If you specified a custom storage location in Step 3.1.1 use that here instead of /home/maven.
  5. Restart nginx. Run nginx -s reload to restart nginx, which will allow you to test that your maven server is working. If you type in the server URL into a web browser, a page with the title “Index of /” should appear as shown in Figure 4.

Setting up Authentication

  1. Installing apache2-utils. Run sudo apt install apache2-utils. This command transitively installs htpasswd, a program that generates http authentication passwords.
  2. Generating the password file. Run htpasswd -c .htpasswd <user>, in the /etc/nginx/ directory, replacing <user> with the desired username for publishing. The terminal will then prompt you to input the password for this user twice.
  3. Restart nginx. Run nginx -s reload to restart nginx again.

Setting up SSL certification using cerbot

  1. Install Snap Core. Run sudo snap install core; sudo snap refresh core. This command will install the core module for a group of programs called Snap.
  2. Install certbot. Run sudo snap install --classic certbot. This command will install certbot, which is a program that manages SSL certifications for webservers.
  3. Attach certbot to nginx. Run sudo certbot --nginx, then follow input instructions for email and more. This command will modify the nginx.conf file from before.
  4. Restart nginx. Run nginx -s reload to restart nginx, which will allow you to test that SSL certification is working. If you type in the server URL into a web browser, you should notice a padlock to the left of the website URL.

Conclusion

Congratulations, you have successfully created a Maven Server using Linode, nginx, Google Domains, and Certbot. By first creating your server, then purchasing a domain and pointing it to your server with DNS records, followed by configuring a web server, adding authentication, and finally enabling SSL certification, you have also learned about how the web works. While there are no extensive tests in the main section of the manual, refer to Appendix C on a sample project to publish to your maven.

For use with Gradle projects read docs.gradle.org/current/userguide/publishing_maven.html and docs.gradle.org/current/samples/sample_publishing_credentials.html on how to setup publishing with credentials.

Appendix A – Adding more Storage

  1. Select the Storage Tab from the server information page.
  2. Click “Create Volume.” Figure 5 shows what the create volume screen looks like.
    1. Give your volume a label. For example, “maven-storage” could be valid
    2. Specify the size. The default size is 20GB. Increase or decrease to your needs.
    3. Click “Create Volume.”

Appendix B – nginx.conf Explanation

user root;												
worker_processes 1;										
pid /run/nginx.pid;										
include /etc/nginx/modules-enabled/*.conf;						
events {												
	worker_connections 768;								
}													
http {												
	include	   mime.types;								
	default_type  application/octet-stream;					
	sendfile		on;									
	keepalive_timeout  65;								
	server {											
		# The name of the server. This is the subdomain followed	
		#	by the domain name purchased.					
		Server_name  maven.example.com;						
		# The “/” is the URL path, in this case there is no path	
		Location / {									
			# “/home/maven” is the storage location for the files	
			#	on the maven server						
			root /home/maven;							
			client_max_body_size 0;						
			client_body_temp_path /tmp/client_temp;			
			dav_methods  PUT DELETE MKCOL COPY MOVE;			
			create_full_put_path  on;						
			dav_access  user:rw group:rw all:r;				
			autoindex on;								
			# Limits all operations expept the follow to 		
			# 	authenticated users only.					
			Limit_except GET PROPFIND OPTIONS HEAD {			
				# The failed authentication message			
				Auth_basic “Example.com’s publishers only”;		
				# The path to the authentication file.			
				auth_basic_user_file .htpasswd;				
			}										
		}											
		# What port to listen to. Do not change this value. 80 is	
		#	for http and 443 is https. certbot will change this.	
		listen 80;										
	}												
}

Appendix C – Publishing a Sample Project to your Server

  1. Clone the example project. Using git, clone https://github.com/OroArmor/example-gradle-publishing-project to your local machine. You will want to open a second terminal to do this. (This doesnt actually exist, it might in the future).
  2. Publish the example project. Run gradlew publish -Pusername=<username> -Ppassword=<password> -Purl=<url>. Replace the username and password with the values you provided for htpasswd, and the url as the server URL.
  3. Confirm the project was published. Navigating back to your maven server’s webpage, you should now see that there is an “com” link. This is the published example project.
  4. Removing the example project. Because the example project is not actual code, remove it by using the original console (the one with SSH access to the server) and run rm -rf /home/maven/com. This will successfully remove the example project and leave the maven in a clean state.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment