3.Configuring the Apache webserver
1. Change the Server Name and Port:
File to Edit: httpd.conf
or apache2.conf
Purpose: To set the server's hostname and port.
Steps:
Open the Apache configuration file. On Ubuntu/Debian, it's typically
/etc/apache2/apache2.conf
. On CentOS/RHEL, it's usually/etc/httpd/conf/httpd.conf
.Look for the
ServerName
directive and set it to your desired hostname:
If you want to change the default HTTP port (80), modify the
Listen
directive:
2. Create a Virtual Host:
File to Edit: Create a new configuration file in the sites-available
directory (on Debian/Ubuntu) or /etc/httpd/conf.d
(on CentOS/RHEL).
Purpose: To host multiple websites or applications on the same Apache server.
Steps:
Create a new configuration file for your virtual host, e.g.,
mywebsite.conf
:
For Ubuntu/Debian:
For CentOS/RHEL:
Add a configuration block for your virtual host, specifying the server name, document root, error log, and custom log:
Save the file and exit the text editor.
Enable the virtual host:
For Ubuntu/Debian:
For CentOS/RHEL, if you placed the configuration file in /etc/httpd/conf.d
, it should be automatically enabled.
3. Secure the Web Server with SSL/TLS:
File to Edit: /etc/apache2/sites-available/default-ssl.conf
(on Debian/Ubuntu) or /etc/httpd/conf.d/ssl.conf
(on CentOS/RHEL).
Purpose: To enable HTTPS for secure connections.
Steps:
Ensure you have SSL/TLS certificates installed on your server. You can obtain certificates from a Certificate Authority or use Let's Encrypt.
Edit the SSL virtual host configuration file:
For Ubuntu/Debian:
For CentOS/RHEL:
Update the
SSLCertificateFile
andSSLCertificateKeyFile
directives with the paths to your certificate and private key files:
Save the file and exit the text editor.
4. Implement Access Control:
File to Edit: Virtual host configuration file (e.g., mywebsite.conf
)
Purpose: To control access to specific directories or resources.
Steps:
Open the virtual host configuration file you created earlier.
Add directives to specify access control. For example, to restrict access to a directory:
This example denies access to the /var/www/html/yourwebsite/restricted
directory for all users.
Save the file and exit the text editor.
5. Enable .htaccess Overrides (Optional):
File to Edit: Virtual host configuration file (e.g., mywebsite.conf
)
Purpose: To allow per-directory configuration using .htaccess
files.
Steps:
Open the virtual host configuration file.
Add the
AllowOverride
directive within a<Directory>
block to specify which directives can be overridden by.htaccess
files:
This example allows all directives to be overridden using .htaccess
files in the /var/www/html/yourwebsite
directory.
Save the file and exit the text editor.
6. Restart Apache:
After making configuration changes, restart the Apache web server to apply the changes:
For Ubuntu/Debian:
For CentOS/RHEL:
These steps should help you configure Apache for various common tasks. Remember to adapt the configurations to your specific use case and requirements.
Last updated