DEV Community

Cover image for Setting Up Multiple Static Sites on One Apache Server
Hritik Raj
Hritik Raj

Posted on

Setting Up Multiple Static Sites on One Apache Server

Sometimes it’s not about orchestrating a massive Kubernetes cluster or optimizing a serverless function. Sometimes, you just need to get a couple of static websites online. It’s a foundational skill, and mastering the command line to do it quickly is a must.

Today, we're walking through a classic scenario: deploying two static sites from a jump host to a fresh app server using nothing but the command line. No control panels, no GUIs, just pure shell.

The Mission

Here's the setup:

  • A Jump Host: This is our staging area. It holds the website files.
  • An App Server: A clean CentOS machine that will host the sites.
  • The Goods: Two website directories, /media and /games, currently sitting on the jump host.
  • The Goal: Serve the media site at http://appserver:6300/media/ and the games site at http://appserver:6300/games/. Notice the custom port—we're not using the default port 80.

Let's get to it.


Step 1: The Great File Migration (scp)

First, we need to get the files from our jump host to the app server. The best tool for this job is scp (secure copy), which works over SSH.

Run this command from your jump host:

# scp -r <source_1> <source_2> <user>@<destination_server>:<path>
scp -r /home/thor/media /home/thor/games tony@stapp02:/tmp
Enter fullscreen mode Exit fullscreen mode

Why this works:

  • scp is the command-line equivalent of a secure drag-and-drop.
  • The -r flag stands for "recursive," which is essential for copying entire directories.
  • We're copying both the media and games directories to the /tmp directory on our app server. /tmp is a great temporary holding area for files you need to process or move into their final positions.

Step 2: Installing the Workhorse - Apache (yum)

Now, SSH into your app server (stapp02 in our case). It’s a clean machine, so the first thing we need is a web server. On CentOS/RHEL, the package for Apache is httpd.

sudo yum install -y httpd
Enter fullscreen mode Exit fullscreen mode

Breaking it down:

  • sudo: We need root privileges to install software.
  • yum install: The command to install packages on CentOS/RHEL systems.
  • -y: This flag automatically answers "yes" to any confirmation prompts, which is great for scripting and saving time.

Step 3: Evicting Port 80 (sed)

Our requirement is to serve content on port 6300, not the default HTTP port 80. We need to edit Apache's main configuration file, /etc/httpd/conf/httpd.conf. While you could use a text editor like vim or nano, the fastest way to make a simple substitution is with sed (stream editor).

sudo sed -i 's/Listen 80/Listen 6300/g' /etc/httpd/conf/httpd.conf
Enter fullscreen mode Exit fullscreen mode

This one-liner is powerful:

  • sed: The stream editor utility.
  • -i: This means "in-place," telling sed to modify the file directly.
  • 's/Listen 80/Listen 6300/g': This is the substitution command.
    • s means substitute.
    • Listen 80 is the pattern we're searching for.
    • Listen 6300 is what we'll replace it with.
    • g means "global," so it replaces all instances in the file (though there's usually only one Listen directive).

Step 4: Setting Up House (mv)

Our website files are still sitting in /tmp. We need to move them to Apache's default document root, which is /var/www/html.

sudo mv /tmp/media /var/www/html/
sudo mv /tmp/games /var/www/html/
Enter fullscreen mode Exit fullscreen mode

This is a straightforward mv (move) command. By moving these directories into /var/www/html, Apache will automatically serve them as subdirectories of the main site.


Step 5: Flipping the 'Open' Sign (systemctl)

We've installed, configured, and moved our files. The last step is to actually run the web server. We'll use systemctl to manage the httpd service.

# Start the service right now
sudo systemctl start httpd

# Enable the service to start automatically on boot
sudo systemctl enable httpd
Enter fullscreen mode Exit fullscreen mode

It's a best practice to run both commands. start gets the server running immediately, and enable ensures it comes back online after a reboot.


The Moment of Truth: Verification (curl)

Is it working? The quickest way to check from the command line is with curl.

# Check the media site
curl http://localhost:6300/media/

# Check the games site
curl http://localhost:6300/games/
Enter fullscreen mode Exit fullscreen mode

If everything went well, curl will print the HTML content of the index.html file from each directory right in your terminal. Success!

And that's it. A complete, multi-site deployment in just a handful of commands. It's not always the flashiest task, but it's the bedrock of web hosting. Happy hosting!

Top comments (0)