With Apache, you can use virtual hosts to direct http traffic for a given domain name to a particular directory (i.e. the root directory of the website for the domain in the request). This feature is commonly used to host multiple websites, but it’s suggested to using it for every website on your server including the first.
Install the Apache web server
If you haven’t have Apache installed on you server, please refer to this passage to install Apache first: CentOS7安装LAMP环境. And then return back to do the following operations.
Set up the virtual host
# create the virtual directories of your domain mkdir -p /var/www/example.com/public_html # change the ownership to the Apache group # this allows Apache modify files in our web directories chown -R apache:apache /var/www/example.com/public_html # change the directory's permissions so they can be read from the internet chmod -R 755 /var/www
Create content for the website
Upload your files you prepared to the public_html folder you created in the last section.
Configure your virtual host directories
We’re going to copy a configuration usually used in Ubuntu/Debian and create two directories: one to store the virtual host files (sites-available) and the other to hold symbolic links to virtual hosts that will be published (sites-enabled).
Create sites-available and sites-enabled directories
mkdir /etc/httpd/sites-available mkdir /etc/httpd/sites-enabled
Edit your Apache configuration file
Edit the main configuration file (httpd.conf) so that Apache will look for virtual hosts in the sites-enabled directory.
# open your config file vi /etc/httpd/conf/httpd.conf # add this line at the very end # this way, we're telling Apache to look for additional config files # + in the sites-enabled directory IncludeOptional sites-enabled/*.conf # save and close the file :wq!
Create virtual host file
We’re going to build it from a new file in your sites-available directory.
1. Create a new config file:
vi /etc/httpd/sites-available/example.com.conf
2. Paste the code bellow in, replacing your own domain for example.com:
<VirtualHost *:80> ServerAdmin webmaster@example.com ServerName www.example.com ServerAlias example.com DocumentRoot /var/www/example.com/public_html ErrorLog /var/www/example.com/error.log CustomLog /var/www/example.com/requests.log combined </VirtualHost>
The lines ErrorLog and CustomLog are not required to set up your virtual host, but we’ve included them, in case you do want to tell Apache where to keep error and request logs for your site.
3. Save and close the file.
:wq!
4. Enable your virtual host with a sym link to the sites-enabled directory.
ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
5. Restart Apache
service httpd restart
Reference:
- Configure Apache Virtual Hosts – CentOS 7: https://hk.godaddy.com/help/configure-apache-virtual-hosts-centos-7-17338
最新评论
大哥资深网民啊,01年我还在念小学。。
看着有点难过。。。
嘿嘿,谢谢老哥,也祝老哥事业蒸蒸日上。
我是你唯一的药学类友情链接网站。 作为一个80后的过来人祝福你,生活越来越好。
这篇文章,我们中学那会老师课堂上念给我们听的。
哈哈哈哈哈,没想到啊, 我有手抄版
嗯,是的
好心办坏事多了去啦
哈哈,是的,我15年末来上海写代码了,一晃三年多过去了,好快。
今天看QQ好友的时候突然看到了你的名字,想起几年前在药品国际注册群挺活跃/厉害的你,现在不见踪影了。就搜了一下,没想到你现在转行去写代码了... (刚才打漏了一句话...)