Ubuntu 22.04 Initial Server Setup

Ubuntu 22.04 Initial Server Setup

Last updated on

In this guide we will configure your new Ubuntu 22.04 server and implement some best practices.

Prerequisites

If you are accessing Ubuntu Server 22.04 remotely and don’t know how to access terminal via SSH, please see one of the following guides:

1. Create User with Superuser Privileges

If you already set up a superuser account during installation under your own name, you may skip this step.

The default root user is the administrative user in a Linux environment that has superuser privileges and you are discouraged from using it on a regular basis. For that reason, it is highly recommended that you set up an alternative account and assign it superuser privileges.

If you are logged in as root, you should see something like root@servername:~#  . The # sign here means you are the system administrator (root).

In this example we are going to use the adduser command to add a new user called conor.

adduser conor

You will be prompted to enter a new password. Generate a password and keep it safe.

Once you’ve entered your new password, you will be asked to enter contact details. You don’t have to fill these in, just press ENTER for defaults.

Once the new user is created, give it superuser privileges using the usermod command. The -aG parameter means append to Group, and the name of the superuser group is sudo.

usermod -aG sudo conor

We can now switch to our new account conor using the su command (substitute user).

sudo su - conor

Enter the password you created previously if prompted.

To run a command as administrator (user "root"), use "sudo ".
See "man sudo_root" for details.

conor@devanswers:~$

Great, we are now logged in as conor. From now on, you should only log into Ubuntu 22.04 using this new user.

2. Set up SSH Key-Based Authentication (optional)

You may skip this step but it is recommend for added security.

As well as offering additional security, SSH key authentication can be more convenient than the more traditional password authentication.

3. Set Up Firewall

It’s always a good idea to set up a firewall to make sure only connections to certain services are allowed. The default firewall configuration tool for Ubuntu 22.04 is ufw. It provides a user friendly way to create an IPv4 or IPv6 host-based firewall.

If you are connected via SSH, allow OpenSSH as a firewall rule first so you don’t get locked out.

sudo ufw allow OpenSSH

If successful, you will see “Rules updated”.

Press y and ENTER if prompted with a warning “Command may disrupt existing ssh connections.”.

sudo ufw enable

To check the status of the firewall, run:

sudo ufw status

Here we can see the OpenSSH rule we just added.

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

4. Check the Timezone

To check if the server time and timezone are correct for your region, run:

date

If the date, time or timezone are incorrect, please see article:

5. Create Swap Space

Why would you need swap space? If your Ubuntu 22.04 server ever runs out of physical memory during heavy load, some critical services such as MySQL can crash. It’s important to have some swap space where memory can expand to if really necessary.

6. Configure a Web Server

Now that you’ve completed the Ubuntu 22.04 Initial Server Setup, you may wish to set up a web server next.

You should decide whether you want to run a LAMP Stack (Linux/Apache/MySQL/PHP) or a LEMP Stack (Linux/Nginx/MySQL/PHP).

Apache is the most popular web server and has been around the longest whereas Nginx is newer but is catching up. As of 2018, Apache is used on 47% of web servers and Nginx is not far behind with 37%. By 2020 if trends continue, Nginx will be the most popular.

Bear in mind that if you go with Nginx there are quite a few differences in how Virtual Hosts are set up compared to Apache. Also, Nginx does not interpret .htaccess files the way Apache does. If you’re used to working with Apache-based servers, go with that. Otherwise, why not give Nginx a try and learn something new?

Apache Guides

We have one single guide for installing a LAMP stack or if you prefer you can do them separately for Apache, MySQL and PHP.

You may also want to install phpMyAdmin for administering your MySQL databases.

To set up a free SSL cert for your domain:

You may want to install and configure an FTP server or configure SFTP.

Nginx Guides

We also have one single guide for installing a LEMP stack or separate guides for Apache, MySQL and PHP.

You may also want to install phpMyAdmin for administering your MySQL database.

To set up a free SSL cert for your domain:

You may want to install and configure an FTP server or configure SFTP.

7. Web Server Backups

If you’re planning on running a web server on Ubuntu 22.04, it’s important to make frequent automated backups of your web document root and databases should you ever accidentally alter data or suffer a hack. We have two articles here to help you configure automated backups.

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

Leave a reply

Your email address will not be published. Required fields are marked *