How to create and enable website using Apache webserver

 

  1. Install apache using command
    #apt update
    #apt install apache2

     

  2. Here apache service will be stopped and will be disabled on start-up. Hence we need to start the service and enable auto-start.

    #systemctl start apache2
    #systemctl enable apache2

    If it gives “abort execution” error for enabling the service, use below command

    # update-rc.d apache2 remove
    # update-rc.d apache2 defaults
    # update-rc.d apache2 enable

  3. In order to add a website, we need to create a directory where website related files will be stored. For testing you can simply create a html file named index.html with text ‘Testing is successful’. Create a directory in /var/www/ path

    #mkdir /var/www/website                                                 \\ Reference 1
    #cd /var/www/website                                                       \\ Reference 2
    #vim index.html
    #echo ‘Testing is successful’ > index.html
    #chown www-data. /var/www/website/*                     \\ Reference 3

  4. Now, we have to create a virtual host configuration in /etc/apache2/sites-available/ path. Be careful with the syntax as it will keep apache service in stopped state while restarting if syntax is wrong.

    #cd /etc/apache2/sites-available/
    #vim (your_website).conf                                    \\ e.g. # vim mydomain.com.conf

    Add following lines and replace text in the bracket with website URL

    <VirtualHost *:80>

ServerName (your_websiteURL)

ServerAlias www.(your_websiteURL)

DocumentRoot /var/www/website/

ErrorLog ${APACHE_LOG_DIR}/ (your_websiteURL).log

</VirtualHost>

 

 

e.g.

<VirtualHost *:80>

ServerName mydomain.com

ServerAlias www.mydomain.com

DocumentRoot /var/www/website/

ErrorLog ${APACHE_LOG_DIR}/ mydomain.com.log

</VirtualHost>

 

  1. Save the file. The website is not activated yet. Hence we need to enable the site and reload the apache.
    #
     cd /etc/apache2/sites-available/
    # a2ensite (your_website).conf                          \\ e.g a2ensite mydomain.com.conf
    # /etc/init.d/apache2 reload    or     # systemctl reload apache2

     

  2. You can now search the website from the browser.

How to install multiple wordpress websites using Apache

 

WordPress is widely used as it is very easy to create and manage websites with wordpress, even for a non-technical person. You can refer WordPress installation guide in our KB. Let us explore apache related aspects of wordpress. Note that, it is taken for granted that adequate PHP package and MySQL db server is already installed along with Apache.

 

  1. To install wordpress, go to the website directory (in our case website and website2) and download WordPress tarball. Extract the tarball in the folder.

    #cd /var/www/website/
    # curl -O https://wordpress.org/latest.tar.gz
    #tar xzvf latest.tar.gz

    #cd /var/www/website2/

# curl -O https://wordpress.org/latest.tar.gz

#tar xzvf latest.tar.gz

after this command, a new folder named wordpress will be created in the website folders. We have to give proper permission to the folder.

# chown -R www-data:www-data /var/www/website/wordpress
#
 chown -R www-data:www-data /var/www/website2/wordpress

 

  1. After editing the wordpress config file with correct database username, password and database name, repeat step 4 from Section 1. Here, domain names and Document Root will be changed in virtual host configuration files. E.g

    #vim wordpresweb.com.conf

    <VirtualHost *:80>

ServerName wordpresweb.com

ServerAlias www.wordpresweb.com

DocumentRoot /var/www/website/

ErrorLog ${APACHE_LOG_DIR}/wordpresweb.com.log

</VirtualHost>

 

#vim wordpressnew.com.conf

<VirtualHost *:80>

ServerName wordpressnew.com

ServerAlias www.wordpressnew.com
DocumentRoot /var/www/website2/

ErrorLog ${APACHE_LOG_DIR}/wordpressnew.com.log

</VirtualHost>

 

 

  1. As, we have created two new config files, we need to enable them.

    # a2ensite wordpresweb.com.conf
    # a2ensite wordpressnew.com.conf
    # /etc/init.d/apache2 reload    or     # systemctl reload apache2