详解Ubuntu/CentOS下Apache多站点配置

  • Post category:Linux

以下是“详解Ubuntu/CentOS下Apache多站点配置”的完整使用攻略,包含两个示例说明。

步骤

  1. 安装Apache服务器。在终端中输入以下命令:

bash
sudo apt-get install apache2

或者

bash
sudo yum install httpd

  1. 创建虚拟主机配置文件。在终端中输入以下命令:

bash
sudo nano /etc/apache2/sites-available/example.com.conf

或者

bash
sudo nano /etc/httpd/conf.d/example.com.conf

将“example.com”替换为您要托管的域名。

  1. 在打开的文件中,输入以下内容:

apache
<VirtualHost *:80>
ServerAdmin webmaster.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

将“example.com”替换为您要托管的域名,并将“/var/www/example.com/public_html”替换为您的网站文件路径。

  1. 启用虚拟主机。在终端中输入以下命令:

bash
sudo a2ensite example.com.conf

或者

bash
sudo systemctl enable httpd

  1. 重启Apache服务器。在终端中输入以下命令:

bash
sudo service apache2 restart

或者

bash
sudo systemctl restart httpd

  1. 在Web浏览器中输入您的域名,您将看到您的网站。

http://example.com/

  1. 重复步骤2-6,为每个要托管的域名创建一个虚拟主机配置文件。

示例1:在Ubuntu上托管多个态网站

  1. 在“/var/www/”目录中创建一个名为“example1.com”的目录。在终端中输入以下命令:

bash
sudo mkdir /var/www/example1.com

  1. 在“example1.com”目录中创建一个名为“.html”的文件。在终端中输入以下命令:

bash
sudo nano /var/www/example1.com/index.html

  1. 在打开的文件中,输入以下内容:

“`html



Example 1title><br /> </head><br /> <body></p> <h1>Welcome to Example 1!</h1> <p> </body><br /> </html></p> <p>“`</p> <ol> <li>保存并关闭文件。重复步骤1-3,为每个要托管的域名创建一个目录和一个index.html文件5. 创建虚拟主机配置文件。在终端中输入以下命令:</li> </ol> <p><code>bash<br /> sudo nano /etc/apache2/sites-available/example1.com.conf</code></p> <ol> <li>在打开的文件中,输入以下内容:</li> </ol> <p><code>apache<br /> <VirtualHost *:80><br /> ServerAdmin webmaster@example1.com<br /> ServerName example1.com<br /> ServerAlias www.example1.com<br /> DocumentRoot /var/www/example1.com<br /> ErrorLog ${APACHE_LOG_DIR}/error.log<br /> CustomLog ${APACHE_LOG_DIR}/access.log combined<br /> </VirtualHost></code></p> <ol> <li>启用虚拟主机。在终端中输入以下命令:</li> </ol> <p><code>bash<br /> sudo a2ensite example1.com.conf</code></p> <ol> <li>重启Apache服务器。在终端中输入以下命令:</li> </ol> <p><code>bash<br /> sudo service apache2 restart</code></p> <ol> <li>在Web浏览器中输入您的域名,将看到您的网站。</li> </ol> <p><code>http://example1.com/</code></p> <p>重复步骤5-9,为每个要托管的域名创建一个虚拟主机配置文件。</p> <h2>示例2:在CentOS上托管多个动态网站</h2> <ol> <li>安装PHP。在终端中输入以下命令:</li> </ol> <p><code>bash<br /> sudo yum install php</code></p> <ol> <li>在“/var/www/”目录中创建一个名为“example2.com”的目录。在终端中输入以下命令:</li> </ol> <p><code>bash<br /> sudo mkdir /var/www/example2.com</code></p> <ol> <li>在“example2.com”目录中创建一个名为“.php”的文件。在终端中输入以下命令:</li> </ol> <p><code>bash<br /> sudo nano /var/www/example2.com/index.php</code></p> <ol> <li>在打开的文件中,输入以下内容:</li> </ol> <p>“`php<br /> <?php echo "Welcome to Example 2!"; ?></p> <p>“`</p> <ol> <li> <p>保存并关闭文件。重复步骤2-4,为每个要托管的域名创建一个目录和一个index.php文件。</p> </li> <li> <p>创建虚拟主机配置文件。在终端中输入以下命令:</p> </li> </ol> <p><code>bash<br /> sudo nano /etc/httpd/conf.d/example2.com.conf</code></p> <ol> <li>在打开文件中,输入以下内容:</li> </ol> <p><code>apache<br /> <VirtualHost *:80><br /> ServerAdmin webmaster@example2.com<br /> ServerName example2.com<br /> ServerAlias www.example2.com<br /> DocumentRoot /var/www/example2.com<br /> ErrorLog /var/log/httpd/example2.com-error.log<br /> CustomLog /var/log/httpd/example2.com-access.log combined<br /> <Directory /var/www/example2.com><br /> Options Indexes FollowSymLinks MultiViews<br /> AllowOverride All<br /> Order allow,deny<br /> allow from all<br /> </Directory><br /> </VirtualHost></code></p> <ol> <li>启用虚拟主机。在终端中输入以下命令:</li> </ol> <p><code>bash<br /> sudo systemctl restart httpd</code></p> <ol> <li>重启Apache服务器。在终端中输入以下命令:</li> </ol> <p><code>bash<br /> sudo systemctl restart httpd</code></p> <ol> <li> <p>在Web浏览器中输入您的域名,您将看到您的网站。</p> <p><code>http://example2.com/</code></p> <p>重复步骤6-10,为每个要托管的域名创建一个虚拟主机配置文件。</p> </li> </ol> <p>希望这些步骤和示例能帮助您在Ubuntu/CentOS下配置Apache多站点,并在Apache服务器上托管静态和动态网站。</p> </div><!-- .entry --> <div class="post-tags clr"> <span class="owp-tag-text">Tags: </span><a href="https://www.bbsmap.com/tag/apache/" rel="tag">apache</a><span class="owp-sep">,</span> <a href="https://www.bbsmap.com/tag/linux/" rel="tag">Linux</a></div> <nav class="navigation post-navigation" aria-label="Read more articles"> <h2 class="screen-reader-text">Read more articles</h2> <div class="nav-links"><div class="nav-previous"><a href="https://www.bbsmap.com/slnjoongwthfnnr/" rel="prev"><span class="title"><i class=" fas fa-long-arrow-alt-left" aria-hidden="true" role="img"></i> Previous Post</span><span class="post-title">linux系统下ubuntu重启apache服务命令</span></a></div><div class="nav-next"><a href="https://www.bbsmap.com/yvrvavrwkloyroy/" rel="next"><span class="title"><i class=" fas fa-long-arrow-alt-right" aria-hidden="true" role="img"></i> 在下一篇文章</span><span class="post-title">Linux下Apache HTTP Server 2.4.26安装教程</span></a></div></div> </nav> <section id="related-posts" class="clr"> <h3 class="theme-heading related-posts-title"> <span class="text">你可能也喜欢</span> </h3> <div class="oceanwp-row clr"> <article class="related-post clr col span_1_of_3 col-1 post-68588 post type-post status-publish format-standard hentry category-linux tag-linux entry"> <h3 class="related-post-title"> <a href="https://www.bbsmap.com/dszyxpkfglorebe/" rel="bookmark">centos6.5中用yum方式安装php5.4与apache2.2的步骤</a> </h3><!-- .related-post-title --> <time class="published" datetime="2023-05-14T09:37:18+08:00"><i class=" icon-clock" aria-hidden="true" role="img"></i>2023年5月14日</time> </article><!-- .related-post --> <article class="related-post clr col span_1_of_3 col-2 post-67542 post type-post status-publish format-standard hentry category-linux tag-linux entry"> <h3 class="related-post-title"> <a href="https://www.bbsmap.com/tkfohijkkgfiwak/" rel="bookmark">比较详细的基于CentOS的WWW服务器架设指南第1/2页</a> </h3><!-- .related-post-title --> <time class="published" datetime="2023-05-14T07:02:14+08:00"><i class=" icon-clock" aria-hidden="true" role="img"></i>2023年5月14日</time> </article><!-- .related-post --> <article class="related-post clr col span_1_of_3 col-3 post-68326 post type-post status-publish format-standard hentry category-linux tag-linux entry"> <h3 class="related-post-title"> <a href="https://www.bbsmap.com/buvadzmqwafpgnh/" rel="bookmark">在CentOS上安装搭建PHP+Apache+Mysql的服务器环境</a> </h3><!-- .related-post-title --> <time class="published" datetime="2023-05-14T09:01:43+08:00"><i class=" icon-clock" aria-hidden="true" role="img"></i>2023年5月14日</time> </article><!-- .related-post --> </div><!-- .oceanwp-row --> </section><!-- .related-posts --> </article> </div><!-- #content --> </div><!-- #primary --> <aside id="right-sidebar" class="sidebar-container widget-area sidebar-primary" itemscope="itemscope" itemtype="https://schema.org/WPSideBar" role="complementary" aria-label="Primary Sidebar"> <div id="right-sidebar-inner" class="clr"> <div id="block-3" class="sidebar-box widget_block clr"> <div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow"> <h2 class="wp-block-heading">近期文章</h2> <ul class="wp-block-latest-posts__list wp-block-latest-posts"><li><a class="wp-block-latest-posts__post-title" href="https://www.bbsmap.com/drihpsscgkicleq/">Python预测分词的实现</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://www.bbsmap.com/najzdhhlvinkpnn/">python利用sklearn包编写决策树源代码</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://www.bbsmap.com/gvvcemuhsfkiaed/">使用matplotlib的pyplot模块绘图的实现示例</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://www.bbsmap.com/ofxcbnyetcijpkv/">Java及python正则表达式详解</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://www.bbsmap.com/imftbtkagnpabjv/">python递归计算N!的方法</a></li> </ul></div></div> </div> </div><!-- #sidebar-inner --> </aside><!-- #right-sidebar --> </div><!-- #content-wrap --> </main><!-- #main --> <footer id="footer" class="site-footer" itemscope="itemscope" itemtype="https://schema.org/WPFooter" role="contentinfo"> <div id="footer-inner" class="clr"> <div id="footer-bottom" class="clr"> <div id="footer-bottom-inner" class="container clr"> <div id="footer-bottom-menu" class="navigation clr"> <div class="menu-%e9%a1%b5%e8%84%9a%e8%8f%9c%e5%8d%95-container"><ul id="menu-%e9%a1%b5%e8%84%9a%e8%8f%9c%e5%8d%95" class="menu"><li id="menu-item-2978" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2978"><a href="https://www.bbsmap.com/category/cloud/">云计算</a></li> <li id="menu-item-2979" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2979"><a href="https://www.bbsmap.com/category/ai/">人工智能</a></li> <li id="menu-item-2980" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2980"><a href="https://www.bbsmap.com/category/algorithm/">算法与数据结构</a></li> </ul></div> </div><!-- #footer-bottom-menu --> <div id="copyright" class="clr" role="contentinfo"> Copyright @2023 <a href='https://www.bbsmap.com/'>BBSMAP</a> 版权所有 </div><!-- #copyright --> </div><!-- #footer-bottom-inner --> </div><!-- #footer-bottom --> </div><!-- #footer-inner --> </footer><!-- #footer --> </div><!-- #wrap --> </div><!-- #outer-wrap --> <a aria-label="Scroll to the top of the page" href="#" id="scroll-top" class="scroll-top-right"><i class=" fa fa-angle-up" aria-hidden="true" role="img"></i></a> <script src="https://www.bbsmap.com/wp-includes/js/imagesloaded.min.js?ver=5.0.0" id="imagesloaded-js"></script> <script id="oceanwp-main-js-extra"> var oceanwpLocalize = {"nonce":"3d12940f4b","isRTL":"","menuSearchStyle":"drop_down","mobileMenuSearchStyle":"disabled","sidrSource":null,"sidrDisplace":"1","sidrSide":"left","sidrDropdownTarget":"link","verticalHeaderTarget":"link","customScrollOffset":"0","customSelects":".woocommerce-ordering .orderby, #dropdown_product_cat, .widget_categories select, .widget_archive select, .single-product .variations_form .variations select"}; </script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/theme.min.js?ver=3.5.3" id="oceanwp-main-js"></script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/drop-down-mobile-menu.min.js?ver=3.5.3" id="oceanwp-drop-down-mobile-menu-js"></script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/drop-down-search.min.js?ver=3.5.3" id="oceanwp-drop-down-search-js"></script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/vendors/magnific-popup.min.js?ver=3.5.3" id="ow-magnific-popup-js"></script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/ow-lightbox.min.js?ver=3.5.3" id="oceanwp-lightbox-js"></script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/vendors/flickity.pkgd.min.js?ver=3.5.3" id="ow-flickity-js"></script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/ow-slider.min.js?ver=3.5.3" id="oceanwp-slider-js"></script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/scroll-effect.min.js?ver=3.5.3" id="oceanwp-scroll-effect-js"></script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/scroll-top.min.js?ver=3.5.3" id="oceanwp-scroll-top-js"></script> <script src="https://www.bbsmap.com/wp-content/themes/oceanwp/assets/js/select.min.js?ver=3.5.3" id="oceanwp-select-js"></script> </body> </html>