How to Set Up the public_html Directory on Raspberry Pi with Apache

Ben
Ben
@benjislab

Hosting a personal website or web pages on your Raspberry Pi is a great way to utilize its capabilities as a low-cost web server. By configuring the public_html directory, you can host individual user web pages, allowing each user to have their own personal website. This guide will walk you through the steps to set up the public_html directory on your Raspberry Pi using Apache.

Prerequisites

Before starting, ensure you have the following:

  • Raspberry Pi with Raspberry Pi OS installed
  • Apache Web Server installed
  • Basic knowledge of using the command line
  • Access to the Raspberry Pi via SSH or directly connected peripherals

Step 1: Install Apache on Raspberry Pi

If you haven’t installed Apache on your Raspberry Pi yet, follow these steps to install it.

  1. Update your system: Open a terminal on your Raspberry Pi or SSH into it, then update your package list:

    sudo apt update
    sudo apt upgrade -y
    
  2. Install Apache: Install the Apache web server by running:

    sudo apt install apache2 -y
    
  3. Verify the Apache Installation: After installation, you can verify that Apache is running by navigating to your Raspberry Pi’s IP address in a web browser. You should see the Apache default welcome page.

    http://<your-raspberry-pi-ip-address>
    

Step 2: Enable User Directories in Apache

Apache can be configured to serve content from a public_html directory within each user’s home directory. To enable this feature, follow these steps:

  1. Enable the userdir module: Apache has a module called userdir that needs to be enabled to use the public_html directory.

    sudo a2enmod userdir
    
  2. Restart Apache: After enabling the module, restart Apache to apply the changes:

    sudo systemctl restart apache2
    

Step 3: Set Up the public_html Directory

Each user on the Raspberry Pi can have their own public_html directory, where they can place HTML files to be served by Apache.

  1. Create the public_html Directory: Switch to the user you want to create the directory for (or create a new user if needed):

    su - <username>
    

    Replace <username> with the actual username.

    Create the public_html directory inside the user's home directory:

    mkdir ~/public_html
    
  2. Set Permissions: Ensure that the directory and its contents are readable by Apache:

    chmod 755 ~/public_html
    

    The 755 permission setting allows the user to read, write, and execute, while others can only read and execute.

Step 4: Create a Test HTML File

To verify that your setup is working, create a simple HTML file in the public_html directory.

  1. Create an index.html file: Inside the public_html directory, create a file named index.html:

    nano ~/public_html/index.html
    

    Add the following content to the file:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>My Raspberry Pi Website</title>
    </head>
    <body>
        <h1>Welcome to My Personal Web Page</h1>
        <p>This page is served from the public_html directory on my Raspberry Pi.</p>
    </body>
    </html>
    

    Save the file and exit the text editor.

Step 5: Access the Web Page

Now, you can access the web page served from the public_html directory in your web browser.

  1. Open your browser: Navigate to the following URL:

    http://<your-raspberry-pi-ip-address>/~<username>/
    

    Replace <your-raspberry-pi-ip-address> with the IP address of your Raspberry Pi and <username> with the username you used to create the public_html directory.

    You should see the "Welcome to My Personal Web Page" message, confirming that your public_html setup is working.

Step 6: Customize and Expand Your Web Pages

With the public_html directory set up, you can now:

  • Create more HTML files: Add additional pages and link them together to create a full website.
  • Use CSS and JavaScript: Enhance your web pages with styles and interactivity.
  • Host multiple user websites: Each user on the Raspberry Pi can have their own public_html directory, allowing multiple personal websites.

Conclusion

Setting up the public_html directory on your Raspberry Pi with Apache is a straightforward way to host personal websites or web pages. This configuration allows each user on the system to have their own space to develop and display content, making your Raspberry Pi a versatile and accessible web server. Whether you're using it for personal projects, education, or experimentation, this setup provides a solid foundation for web development on the Raspberry Pi.