Install Nginx and Set Up a Separate Exercise Directory
🎯 Introduction
Start by preparing an exercise directory you can manage yourself. This article covers installing the software, creating directories, and preparing a web page. You will write the configuration that runs it in the next article.
🧰 Install the Required Tools
Run these commands one line at a time in an Ubuntu terminal. Do not type a $ before them:
sudo apt update
sudo apt install nginx curl vim openssl
nginx -v
apt is Ubuntu's software installation tool. update refreshes the list of available packages, and install installs them. sudo runs a command with administrator privileges; password input usually shows no characters. nginx -v prints the installed version. Record the actual output; you do not need to downgrade to match a patch version.
curl sends requests, vim edits text in the terminal, and openssl generates the exercise certificate in the HTTPS chapter.
The package may automatically start a system Nginx service using /etc/nginx/nginx.conf and port 80. This course starts a separate instance using your home directory and port 8080. A working system service does not verify the course instance. See the official Ubuntu installation instructions for package installation behavior.
🛠 Create the Directories and Your First Page
$HOME is your current user's home directory. mkdir -p creates directories; each / in a path separates directory levels:
mkdir -p "$HOME/nginx-course/conf/servers"
mkdir -p "$HOME/nginx-course/html/assets"
mkdir -p "$HOME/nginx-course/logs"
mkdir -p "$HOME/nginx-course/temp/client"
mkdir -p "$HOME/nginx-course/temp/proxy"
mkdir -p "$HOME/nginx-course/temp/fastcgi" "$HOME/nginx-course/temp/uwsgi" "$HOME/nginx-course/temp/scgi"
vim "$HOME/nginx-course/html/index.html"
The last command opens the file in Vim. If it does not exist yet, Vim opens an empty editing area. Vim starts in normal mode, where keys perform editing operations. Press lowercase i to enter insert mode, then type or paste the following content.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Study Notes</title>
</head>
<body>
<h1>My Study Notes</h1>
<p>This is my first web page served by Nginx.</p>
</body>
</html>
After pasting, press Esc to return to normal mode. Type :wq (including the colon), then press ↩ Return on a Mac keyboard (Enter on other keyboards). w writes the file and q quits. When successful, you return to the terminal. Use the same steps to save and close other files in Vim throughout the course.
To discard your changes after opening a file, press Esc, type :q!, and press ↩ Return. Vim quits without saving your changes. This page is named index.html; the next article configures it as the directory index.
| Directory | Purpose |
|---|---|
conf | Configuration files, with individual sites in servers |
html | Public web pages that browsers may read |
logs | Runtime logs and the instance's process ID file |
temp | Temporary storage for request bodies, proxy responses, and other data |
Keep passwords, certificate private keys, and configuration files out of html, because this directory will later be accessible over HTTP.
🧱 Give the Exercise Instance a Short Command
Repeatedly typing long paths is error-prone. Paste this entire Bash function into your current terminal:
ngx() {
nginx -e stderr -p "$HOME/nginx-course/" -c conf/nginx.conf "$@"
}
The function makes ngx a shorthand command. -p sets this instance's path prefix, -c selects the configuration file, and -e stderr sends even errors that occur before reading the configuration to the terminal. "$@" passes any additional arguments to Nginx unchanged. See the official command-line options.
Every use of ngx below refers to this function. Define it again whenever you open a new terminal. It cannot start the instance yet, because you will create conf/nginx.conf in the next article.
Run ngx as your regular user during the exercises. Switching between running it with and without sudo can leave log and process ID files that your user cannot write to.
❓ Questions and Answers
Q1: What if I see nginx: command not found?
The current terminal cannot find the Nginx executable. Confirm that you are working inside the Ubuntu environment where you installed it, then check whether installation succeeded.
Q2: Does seeing a welcome page at localhost mean setup is complete?
That is probably the system instance on port 80. Verification URLs for the course instance explicitly use http://127.0.0.1:8080.
🧪 Exercise
Run this command to confirm that the file exists and has the expected contents:
cat "$HOME/nginx-course/html/index.html"
cat prints a file's contents. You should see “My Study Notes” and all the HTML tags. If the file is missing, check where you saved it before continuing.
🎉 Your exercise environment is ready. Next, give Nginx a configuration to read!
