DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Arvind Kumar Rawat
Arvind Kumar Rawat

Posted on

Install apache2 on ubuntu and configure user home as webroot

Step 1: Install apache2 server on ubuntu server

sudo apt update
sudo apt install -y apache2
Enter fullscreen mode Exit fullscreen mode

Step 2: Add user to the ubuntu server.

sudo adduser <username>
Enter fullscreen mode Exit fullscreen mode

Step 3: add below line at /etc/apache2/apache2.conf

<Directory /home/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
Enter fullscreen mode Exit fullscreen mode

Example file

DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>
<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<Directory /home/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
Enter fullscreen mode Exit fullscreen mode

Step 4: Update the default virtual host of the apache2 /etc/apache2/sites-available/000-default.conf.

Update the DocumentRoot /home/<username>/

Step 5: Restart the apache2 server.

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Additional steps to enable basic authentication, here your username: admin and password: gotechnies.

htpasswd -cbs /etc/global.htpasswd admin gotechnies
Enter fullscreen mode Exit fullscreen mode

Update the virtual host file /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/arvind/

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory "/home/username">
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/global.htpasswd 
        Require valid-user
    </Directory>
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Restart the apache2 server.

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Here is a post you might want to check out:

Regex for lazy developers

regex for lazy devs

Sorry for the callout πŸ˜†