Apache Server Installation in Linux
The Apache HTTP server is the most widely-used web server in the world. It provides many powerful features including dynamically loadable modules, robust media support, and extensive integration with other popular software.
Installing Apache
- Install httpd 🡺 yum install httpd
- Enable and start the service httpd
systemctl enable httpd
systemctl start httpd
- Enable https:
yum install mode_ssl
- Restart the service 🡺 systemctl restart httpd
Validation:
go to chrome and try running the server ip 🡺 http://192.168.11.136
Managing the Apache Process
Stop your web server 🡺 systemctl stop httpd
Start the web server when it is stopped 🡺 systemctl start httpd
Stop and then Start the service again 🡺 systemctl restart httpd
If you are simply making configuration changes, Apache can often reload without dropping connections 🡺 systemctl reload httpd
By default, Apache is configured to start automatically when the server boots. If this is not what you want, disable this behaviour by typing 🡺 systemctl disable httpd
To re-enable the service to start up at boot 🡺 systemctl enable httpd
Apache will now start automatically when the server boots again.
The default configuration for Apache will allow your server to host a single website. If you plan on hosting multiple domains on your server, you will need to configure virtual hosts on your Apache web server.
Setting Up Virtual Hosts:
Copy the developed (php, html) developer files in the path 🡺 /var/www/html/<dev file>
Dev files are pasted in above location as the above location is mentioned in the configuration files 🡺 /etc/httpd/conf(httpd.conf)
Name Based Virtual host in apache:
With the name based virtual hosting you can host several domains/websites on a single machine with a single IP. All domains on that server will be sharing a single IP. It’s easier to configure than IP based virtual hosting, you only need to configure DNS of the domain to map it with its correct IP address and then configure Apache to recognize it with the domain names.
Example Setup:
- OS — CentOS 6.5
- Application — Apache Web Server
- IP Address — 192.168.0.100
- IP Address — 192.168.0.101
- Domain — www.example1.com
- Domain — www.example2.com
- Install httpd 🡺 yum install httpd
- Setup Name Based Virtual Host:
- Create a directory in the document root (/var/www/html) — where you will keep all your website’s files.
mkdir /var/www/html/example1.com/
mkdir /var/www/html/example2.com/
- To set up Name based virtual hosting you must need to tell Apache to which IP you will be using to receive the Apache requests for all the websites or domain names. We can do this with NameVirtualHost directive. Open Apache main configuration file with VI editor.
vi /etc/httpd/conf/httpd.conf
- Search for NameVirtualHost and uncomment this line by removing the # sign in front of it. Next add the IP with possible in which you want to receive Apache requests. After the changes, your file should look like this:
NameVirtualHost 192.168.0.100:80
- Add the following two virtual directives at the bottom of the file. Save and close the file.
<VirtualHost 192.168.0.100:80>
ServerAdmin webmaster@example1.com
DocumentRoot /var/www/html/example1.com
ServerName www.example1.com
ErrorLog logs/www.example1.com-error_log
CustomLog logs/www.example1.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@example2.com
DocumentRoot /var/www/html/example2.com
ServerName www.example2.com
ErrorLog logs/www.example2.com-error_log
CustomLog logs/www.example2.com-access_log common
</VirtualHost>
- You are free to add as many directives you want to add in your domains virtual host section. When you are done with changes in httpd.conf file, please check the syntax of files with following command.
httpd -t
- It is recommended to check the syntax of the file after making some changes and before restarting the Web server because if any syntax goes wrong Apache will refuse to work with some errors and eventually affect your existing web server go down for a while. If syntax is OK. Please restart your Web server and add it to chkconfig to make your web server start in runlevel 3 and 5 at the boot time only.
service httpd restart
chkconfig — level 35 httpd on
- Now it’s time to create a test page called index.html add some content to the file so we will have something to check it, when the IP calls the virtual host.
vi /var/www/html/example1.com/index.html
<html>
<head>
<title>www.example1.com</title>
</head>
<body>
<h1>Hello, Welcome to www.example1.com.</h1>
</body>
</html>
vi /var/www/html/example2.com/index.html
<html>
<head>
<title>www.example2.com</title>
</head>
<body>
<h1>Hello, Welcome to www.example2.com.</h1>
</body>
</html>
- Once you’re done with it, you can test the setup by accessing both the domains in a browser.