> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/servers-configurations-debian/apache-web-server.md).

# Apache Web Server

Apache is known for its flexibility and reliability. It is used to host a wide range of websites and applications. Apache runs on various operating systems (Linux, Unix, Windows) and supports multiple programming languages and technologies such as PHP, Perl, and Python.

***

## Configure Apache Web Server

* Install Apache web server in Debian Linux.

  ```bash
  apt install apache2*
  ```
* Useful Files
  * This is the main configuration file.

    ```bash
    /etc/apache2/apache2.conf
    ```
  * This is the path to the folder on your server where your web content is stored.

    ```bash
    /var/www/html/
    ```
* Owner and Group change.

  ```bash
  chown www-data:www-data /var/www/html/*
  ```

***

## Website Bind with IP

* Edit the configuration file.

  ```bash
  vim /etc/apache2/apache2.conf
  ```

  *Note: Add the following lines at the end of the file.*

  ```
    <Virtualhost Enter_Your_IP>
        Documentroot /path/to/directory
        Directoryindex index.html
    </Virtualhost>
  ```
* Start the service.

  ```bash
  systemctl restart apache2.service
  ```

***

## Website Bind with Port

* Edit the configuration file.

  ```bash
  vim /etc/apache2/apache2.conf
  ```

  *Note: Add the following lines at the end of the file.*

  ```
    <Virtualhost Enter_Your_IP:Enter_your_port_number>
        Documentroot /path/to/directory
        Directoryindex index.html
    </Virtualhost>
  ```
* Start the service.

  ```bash
  systemctl restart apache2.service
  ```

***

## Website Bind with Domain Name

* Create a local domain. Refer to the resource:[Local Domain Setup](https://github.com/Mr-Secure-Code/Linux_Server/tree/main/Debian/DNS)
* Edit the configuration file.

  ```bash
  vim /etc/apache2/apache2.conf
  ```

  *Note: Add the following lines at the end of the file.*

  ```
    <Virtualhost Enter_Your_Domain_IP>
        Servername zone.name
        Documentroot /path/to/directory
        Directoryindex index.html
        ServerAlias www.zone.name
    </Virtualhost>
  ```
* Start the service.

  ```bash
  systemctl restart apache2.service
  ```

***

## Website Bind with SSL

* Enable the SSL module.

  ```bash
  a2enmod ssl
  ```
* Edit the configuration file.

  ```bash
  vim /etc/apache2/apache2.conf
  ```

  *Note: Add the following lines at the end of the file.*

  ```
    <Virtualhost Enter_Your_Domain_IP>
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
        Documentroot /path/to/directory
        Directoryindex index.html
    </Virtualhost>
  ```
* Start the service.

  ```bash
  systemctl restart apache2.service
  ```

***

## Website Call with Users Home-Directory

* Enable the userdir module.

  ```bash
  a2enmod userdir
  ```
* Bind the website.

  ```bash
  vim /etc/apache2/apache2.conf
  ```

  *Add the following configuration:*

  ```
    <Virtualhost Enter_Binde_IP>
        Documentroot /home/user_name/public_html
        Directoryindex index.html
    </Virtualhost>
  ```
* Grant execute permission to the user.

  ```bash
  chmod 711 /home/user_name
  ```
* Create the public\_html directory in the user's home directory and place your website inside.

  ```bash
  mkdir public_html
  ```
* Start the service.

  ```bash
  systemctl restart apache2.service
  ```

***

## Wordpress Deploy in Apache

* PHP Configuration
  * Install PHP and related packages.

    ```bash
    apt install php php-common php-cli php-opcache php-gd php-curl php-mysql php-xml php-mbstring php-pear php-dev
    ```
  * Edit the PHP configuration file.

    ```bash
    vim /etc/php/{version}/cli/php.ini
    ```

    *Update or add the following settings:*

    ```
      memory_limit = 512M
      max_execution_time = 500
      max_input_vars = 10000
      upload_max_filesize = 2048M
      post_max_size = 2048M
      allow_url_fopen = On
    ```

    *Explanation of parameters:*

    ```
      - memory_limit: Maximum memory a PHP script can consume.
      - max_execution_time: Maximum time a PHP script can run.
      - max_input_vars: Maximum number of input variables a PHP script can process.
      - upload_max_filesize: Maximum size for file uploads.
      - post_max_size: Maximum size of HTTP POST data.
      - allow_url_fopen: Whether PHP can access remote files.
    ```
* MySQL Configuration
  * Refer to the following resources for MySQL setup:

    [MySQL Configure](https://downloads.mysql.com/docs/mysql-apt-repo-quick-guide-en.pdf)[MySQL Old Version Key Error](https://forums.mysql.com/read.php?11,723158,723158)
  * Download and configure the MySQL repository package.

    ```bash
    wget https://dev.mysql.com/get/mysql-apt-config_0.8.24-1_all.deb
    ```

    ```bash
    apt install ./mysql-apt-config_0.8.24-1_all.deb
    ```
  * Update repositories.

    ```bash
    apt update
    ```
  * Install MySQL Community Server.

    ```bash
    apt install mysql-community-server
    ```
  * Start and enable the MySQL service.

    ```bash
    systemctl start mysqld
    ```

    ```bash
    systemctl enable mysqld
    ```
* Configure phpMyAdmin
  * Download the phpMyAdmin package.

    ```bash
    wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip
    ```
  * Unzip the package.

    ```bash
    unzip phpMyAdmin-5.2.0-all-languages.zip
    ```
  * Copy the package contents to the web directory.

    ```bash
    cp -rv phpMyAdmin-5.2.0-all-languages /var/www/html/phpMyAdmin
    ```
  * Bind phpMyAdmin to your web server.

    ```bash
    vim /etc/apache2/apache2.conf
    ```

    *Add the following configuration:*

    ```
      <Virtualhost Enter_phpMyAdmin_Bind_IP>
          Documentroot /var/www/html/phpMyAdmin/
          Directoryindex index.php
      </Virtualhost>
    ```
  * Change folder ownership.

    ```bash
    chown -Rv www-data:www-data phpMyAdmin/
    ```
  * Restart the Apache service.

    ```bash
    systemctl restart apache2.service
    ```
* Configure Wordpress
  * Download the Wordpress package.

    ```bash
    wget https://wordpress.org/latest.tar.gz
    ```
  * Extract the package.

    ```bash
    tar -xvf latest.tar.gz
    ```
  * Copy Wordpress to the web directory.

    ```bash
    cp -rv wordpress/ /var/www/html/
    ```
  * Bind Wordpress to your web server.

    ```bash
    vim /etc/apache2/apache2.conf
    ```

    *Add the following configuration:*

    ```
      <Virtualhost Enter_Wordpress_Bind_IP>
          Documentroot /var/www/html/wordpress/
          Directoryindex index.php
      </Virtualhost>
    ```
  * Change folder ownership.

    ```bash
    chown -Rv www-data:www-data /var/www/html/wordpress/
    ```
  * Restart the Apache service.

    ```bash
    systemctl restart apache2.service
    ```
