Use Raspberry Pi to build your own web server (2) set up Apache and raspberry apache
1. Obtain the Server IP Address:
1 ifconfig
2. Change the local hosts file
If you want to enter a domain name in your browser to access your server, you need to change the hosts file in your computer.
Linux:
1 sudo vim /etc/hosts
Windows:
1 C:\Windows\System32\drivers\etc\hosts
Open the host file and add the following content:
--------------------------------------------------
192.168.2.100 www.yourname.com yourname.com
192.168.2.100 www.myname.com myname.com
--------------------------------------------------
The front is the IP address of your server and the back is the domain name of your website. In this way, enter www.yourname.com or www.myname.com in your browser to directly access the website on your server. You can use ping www.yourname.com to test
3. Create a website directory file
Next, we need to create the root directory and test page of our two websites under the apache root directory, so that we can enter the specified connection to access our different pages
1 sudo mkdir {/var/www/yourname.com,/var/www/myname.com}
Create pages and write content respectively.
1 sudo vim /var/www/yourname.com/index.html2 sudo vim /var/www/myname.com/index.html
4. Set Virtual Hosts
If you access yourname.com and myname.com in your browser at this time, you will find that the original it workspage is still opened, and the two index.html pages just created for different domain names are not accessed. to access different domain names, you can access different websites, the last and most important step is to set up apache Virtual Hosts.
The apache VM configuration file is in/etc/apache2/sites-available/. You can create a new vhosts. conf file by referring to the 000-default.conf file in this directory,
1 sudo vim /etc/apache2/sites-available/vhosts.conf
Enter the following content in the file:
------------------------------------------------------
<VirtualHost *: 80>
ServerName www.yourname.com
ServerAlias yourname.com
DocumentRoot/var/www/yourname.com
</VirtualHost>
<VirtualHost *: 80>
ServerName www.myname.com
ServerAlias myname.com
DocumentRoot/var/www/myname.com
</VirtualHost>
------------------------------------------------------
Each <VirtualHost>... </VirtualHost> tag specifies a virtual host. The host domain name is the domain name after ServerName, and the host root directory is the directory entered after DocumentRoot. ServerAlias is the alias of the website domain name. The link after accessing it is equivalent to the link after directly accessing ServerName.
Finally, we need to create a soft connection to the created vhost. conf file and put it in the/etc/apache2/sites-enabled/directory.
1 sudo ln -s /etc/apache2/sites-available/vhosts.conf /etc/apache2/sites-enabled/
5. Test
Now, all the settings are complete. Enter the corresponding website in the browser to open the website page in different directories. By the way, each time the configuration file is modified, restart apache to re-read the new settings. Otherwise, all the settings just made will be ignored.
1 sudo service apache2 restart
References:
<Linux Command Tutorial 13-apache2>