转载: 网站搭建:从0到1搭建网站

<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
                    <div class="markdown_views" deep="5">
                <h3 id="1-网站基本概念"><a name="t0"></a>1 网站基本概念</h3>

<p>网站说白了就是把一些东西放到哪里供其他人访问,并提供了一些可以交互的接口的地方。如果要实现这样一个地方首先,需要一个能放东西的地方这就是服务器。其次,我们要找到这个地方,然而有很多这样的地方,所以我们就需要给很多这样的地方标号,然后按照标号来找到这些地方,并访问。所以就有了IP地址,每个服务器都有一个唯一的IP地址,这样我们可以通过IP地址找到服务器。最后,IP地址都是一堆数字太难记了,有那么多服务器(相应的就有那么多IP地址),根据一串数字来找到服务器对于我们来说太麻烦了,根本记不住。如果有个名字就好了,比如百度的服务器可以用www.baidu.com来记那就好多了。所以就有了域名。为了我们能够用域名来找到服务器,我们需要把各个域名与服务器的IP地址映射起来来。每次当我们用域名来访问服务器的时候,就需要把域名转换为IP地址(这个过程就是DNS(Domain Name System)服务),然后通过IP地址找到服务器。</p>

<p>实际上这些概念实现上很复杂,分别在互联网七层协议中的不同层实现。它们之间层与层之间相互依赖。实际情况也比较复杂,比如从域名到IP的映射之间可以多个域名映射到同一个IP,这就是虚拟主机的概念。不过这些都是后话了。接下来对各个概念尽心详细的解释。</p>



<h5 id="11-服务器概念">1.1 服务器概念</h5>

<p>服务器(server),是提供计算机服务的设备。由于服务器需要响应服务请求,并进行处理,因此一般来说服务器应具备承担服务并且保证服务的能力。</p>

<p>服务器的构成包括处理器、硬盘、内存、系统总线等,和通用的计算机架构类似,但是由于需要提供高可靠的服务,因此在处理能力、稳定性、可靠性、安全性、可扩展性、可管理性等昂面要求较高。</p>

<p>在网络环境下,根据服务器提供的服务类型不同,分为文件服务器,数据库服务器,应用程序服务器,WEB服务器等。</p>

<p>总的来说服务器就是能够提供服务的机器,这取决于机器上所安装的软件(服务软件),比如:Web服务器提供Web服务(网站访问),就需要Web服务软件,如Apache、tomact等。</p>



<h5 id="12-ip的概念">1.2 IP的概念</h5>

<p>IP:Internet Protocol,网络之间互联协议。网络之间互联的协议也就是计算机网络相互连接进行通信而设计的协议。在因特网中,它是能链接到网上的所有计算机网络实现相互通信的一套规则,规定了计算机在因特网上进行通信时应当遵守的规则。任何厂家生产的计算机系统,只要遵守了IP协议就可以与因特网互联互通。IP地址具有唯一性。</p>



<h5 id="13-域名">1.3 域名</h5>

<p>域名(Domain Name),是由一串用点分隔的名字构成(www.baidu.com)的Internet上某一台计算机或计算机组成的名称,用于在数据串数时标识计算机的电子方位(有时也指地理位置,地理上的域名,指代有行政自主权的一个地方区域)。域名时一个IP地址上的“面具”。一个域名的目的是便于记忆和沟通的一组服务器的地址(网站,电子邮件,FTP等)。域名作为力所能及难忘的互联网参与者的名称。</p>



<h5 id="14-dns">1.4 DNS</h5>

<p>DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够是用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串。通过主机名,最终得到该主机名对应的IP地址的过程叫作域名解析(或主机名解析)。</p>



<h5 id="15-端口">1.5 端口</h5>

<p>端口(Port),可以认为设备与外界通讯交流的出口。主要为了解决,一台服务器上有很服务程序我们访问一台服务器的时候如何判断我们要要访问那种服务程序的问题。比如网页服务(http访问)的端口都为80。端口可分为虚拟端口和物理端口,其中虚拟端口指计算机内部或交换机路由器内的端口,不可见。例如计算机中的80端口、21端口、23端口等。物理端口又称为接口,是可见端口。</p>



<h3 id="2-当我们在浏览器中输入一个网址后发生了什么"><a name="t1"></a>2 当我们在浏览器中输入一个网址后发生了什么</h3>

<p>前面说过域名只是IP地址的”面具”,所以当我们在浏览器中输入了域名后,首先要做的是解析域名,找到域名所对应的IP地址。这个时候浏览器会先访问本地DNS服务。说白了就是在本地电脑里保存了一个文件(windows中是hosts),该文件中记录了域名与IP地址的映射,如果你输入的域名在该文件中,则浏览器会直接访问该域名对应的IP地址所对应的服务器。如果本地DNS文件中没有该域名,则浏览器会访问DNS服务器(DNS服务器的IP地址就几个,且比较固定,比如114.114.114.114),在DNS服务器上查询该域名所对应的IP。 <br data-filtered="filtered">
查到IP后,浏览器则访问该IP所指向的服务器,然后向服务器提出请求,服务器回应后,发送相应的内容到本机,显示在浏览器中。</p>

<p>DNS解析域名的过程和访问IP的过程中还有很多机制,这里不详细说。</p>



<h3 id="3-静态网页与动态网页"><a name="t2"></a>3 静态网页与动态网页</h3>



<h5 id="31-静态网页及其访问过程">3.1 静态网页及其访问过程</h5>

<p>称为普通网页。静态网页是指编写完成后,内容就不会在变化的,不管何人何时访问,显示的内容都是一样的。如果要修改网页的内容就必须修改其源代码,然后重新上传到服务器上。一般指.html、.htm、.xml文件等。可以直接打开用浏览其查看的。其访问过程如下图(以Apache为例):</p>

<p><img src="https://img-blog.csdn.net/20180601214210313?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>



<h5 id="32-动态网页及其访问过程">3.2 动态网页及其访问过程</h5>

<p>动态网页指在网页文件中除了HTML标记外,还包括一些实现特定功能的程序代码,这些程序代码使得浏览器与服务器之间可以进行交互,即服务器端可以根据客户端的不同请求动态产生网页内容。动态网页的后缀名通常根据所用的程序设计语言的不同而不同,一般为.asp、.aspx、.cgi、.php、.perl、.jsp等。动态网页可以根据不同的时间,不同的浏览器者显示不同的信息。其访问流程如下(以php为例):</p>

<p><img src="https://img-blog.csdn.net/20180601214220885?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>



<h3 id="4-服务器环境搭建"><a name="t3"></a>4 服务器环境搭建</h3>

<p>因为没有购买服务器,也没有注册域名。我们计划在本地搭建一个服务器环境,把我们自己本地计算机作为一个服务器。通过本地DNS重映射域名到我们本机的IP地址(127.0.0.1),这样和实际搭建服务器没有大的区别,唯一的区别是做这些操作水在另一个IP地址的计算机上进行的,域名也是经过注册(开通DNS服务的),在工信部经过备案的。</p>

<p>这里我们选择Apache2.4服务器软件,php7语言,Mysql5.7数据库。</p>



<h5 id="41-软件下载">4.1 软件下载</h5>

<p>Apache2.4,文件名:httpd-2.4.33-win64-VC15.zip。官方没有提供windows编译版本,这里是在pachelounge社区下载的:</p>

<p><a href="https://www.apachelounge.com/download/" target="_blank">https://www.apachelounge.com/download/</a></p>

<p>php7.2.6,文件名:VC15 x64 Thread Safe:</p>

<p><a href="https://windows.php.net/download/" target="_blank">https://windows.php.net/download/</a></p>

<p>MySQL5.7.22,文件名:mysql-5.7.22-winx64(下载需要注册账号):</p>

<p><a href="https://dev.mysql.com/downloads/mysql/5.7.html#downloads" target="_blank">https://dev.mysql.com/downloads/mysql/5.7.html#downloads</a></p>



<h5 id="42-apache安装">4.2 Apache安装</h5>

<p>下载并解压文件,这里为方便管理,把Apache、php、mysql放在同一个文件夹下了。这里目录为“D:\Server\Apache”。</p>



<h6 id="421-apache目录解释">4.2.1 Apache目录解释</h6>

<p><img src="https://img-blog.csdn.net/20180601211252758?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>



<h6 id="422-安装">4.2.2 安装</h6>

<p>以管理员身份启动cmd,然后进入Apache目录,调用一下命令:</p>



<pre class="prettyprint" name="code"><code class="language-cpp hljs  has-numbering">httpd -k install -n apache
net start apache
httpd -n <span class="hljs-string">"apache"</span> -t</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>

<p>以上命令分别是安装apache服务,并命名为“apache”;启动apache服务;如果启动不成功,第三行则可以查看原因。启动成功。第三行则起测试作用测试作用。运行结果如下图:</p>

<p><img src="https://img-blog.csdn.net/20180601211518421?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>

<p>另外,卸载Apache服务方法,是以管理员身份运行cmd,然后输入:sc delete Apache().</p>

<p>Apache(),表示你安装的Apache服务名字,有可能是Apache,也可能是Apache2.4,是可以在安装的时候指定的。</p>

<h6 id="423-配置apache">4.2.3 配置Apache</h6>

<p>在”D:\Server\Apache\conf”目录下有个名为”httpd.conf”的文件,用记事本打开。并修改一些内容,如下:</p>

<p>1、服务器根目录,ServerRoot</p>

<p>修改前:ServerRoot “c:/Apache24”</p>

<p>修改后:ServerRoot “D:/Server/Apache”,这个目录是Apache的安装目录,根据自己的目录做相应的修改。</p>

<p>2、服务名称 <br data-filtered="filtered">
修改前:#ServerName www.example.com:80 <br data-filtered="filtered">
修改后:ServerName localhost:80 <br data-filtered="filtered">
3、Apache默认网站目录</p>

<p>修改前:DocumentRoot “c:/Apache24/htdocs”</p>

<p>修改后:DocumentRoot “D:/Documents/MyProjects/Web/”,这个目录是自己网页文件的放置目录,就是网站文件都在该文件夹下。是自己定义的。</p>

<p>4、修改根目录选项</p>

<p>修改前:</p>



<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering"><Directory <span class="hljs-string">"c:/Apache24/htdocs"</span>>

    Options Indexes FollowSymLinks

    AllowOverride None

    <span class="hljs-keyword">Require</span> all granted

</Directory></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre>

<p>修改后:</p>



<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering"><Directory <span class="hljs-string">"D:/Documents/MyProjects/Web/"</span>>

    Options Indexes FollowSymLinks

    AllowOverride None

    <span class="hljs-keyword">Require</span> all granted

</Directory></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre>



<h6 id="424修改本地dns映射">4.2.4修改本地DNS映射</h6>

<p>在文件夹”C:\Windows\System32\drivers\etc”下面有个名为hosts的文件,用记事本打开,并添加如下一行:</p>



<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering"><span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span> localhost</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li></ul></pre>

<p>这里是告诉浏览器,把localhost这个域名映射到IP地址为127.0.0.1的服务器,而127.0.0.1指向的服务器就是本机。</p>



<h6 id="425-启动apache并测试">4.2.5 启动Apache,并测试</h6>

<p>在Apache网站根目录里放入html文件,这里为”D:/Documents/MyProjects/Web/”,就是在4.2.3中修改的目录。</p>

<p>注意,apache默认打开的文件是index.html。所以我们在”D:/Documents/MyProjects/Web/”中创建名为index.html的文件,并编辑以下内容:</p>



<pre class="prettyprint" name="code"><code class="language-html hljs  has-numbering"><span class="hljs-doctype"><!DOCTYPE html></span>
<span class="hljs-tag"><<span class="hljs-title">html</span> <span class="hljs-attribute">lang</span>=<span class="hljs-value">"en"</span>></span>

<span class="hljs-tag"><<span class="hljs-title">body</span>></span>

    Hello world !
<span class="hljs-tag"></<span class="hljs-title">body</span>></span>
<span class="hljs-tag"></<span class="hljs-title">html</span>></span></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre>

<p>启动Apache,在”D:\Server\Apache\bin”下有个名为ApacheMonitor.exe的程序,可以通过它来管理Apache的启动、关闭与重启。双击,启动后在任务栏右下角有个图标,左击然后启动Apache服务。</p>

<p>在浏览器地址栏输入:”<a href="http://localhost/" target="_blank">http://localhost/</a>“结果如下:</p>

<p><img src="https://img-blog.csdn.net/20180601211835186?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>



<h5 id="43-php安装">4.3 php安装</h5>

<p>下载并解压文件,这里放的目录为:“D:\Server\php”,php目录介绍如下图:</p>

<p><img src="https://img-blog.csdn.net/20180601211905582?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>



<h5 id="44-mysql安装">4.4 mysql安装</h5>

<p>mysql安装要注意有两个目录,一个是mysql本身的安装目录,另一个是mysql中数据存放的目录。为方便管理这两个目录放在了同一个目录下:</p>

<p>mysql的安装目录:”D:\Server\mysql” <br data-filtered="filtered">
mysql的数据存放目录:“D:\Server\mysql\data”  </p>

<p>1、把文件解压到”D:\Server\mysql”  ,然后在该文件夹中创建data目录。mysql目录介绍如下图:</p>

<p><img src="https://img-blog.csdn.net/201806012119524?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>

<p>2、把“D:\Server\mysql\bin”,添加到环境变量,为了方便在cmd中调用mysql命令,否则每次调用mysql命令都要输入路径,或者先通过cmd进入该文件夹。</p>

<p>3、创建mysql配置文件 <br data-filtered="filtered">
在目录”D:\Server\mysql” 中创建”my.ini”文件,内容如下:</p>



<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering">[client]
port=<span class="hljs-number">3306</span>
<span class="hljs-keyword">default</span>-character-set=utf8
[mysqld]
port=<span class="hljs-number">3306</span>
character_set_server=utf8
basedir=D:/Server/mysql
datadir=D:/Server/mysql/data</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre>

<p>4、初始化数据库</p>



<pre class="prettyprint" name="code"><code class="language-shell hljs sql has-numbering">mysqld <span class="hljs-comment">--initialize</span>
mysqld -install MySQL
net <span class="hljs-operator"><span class="hljs-keyword">start</span> MySQL</span></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>

<p>其中命令依次为,初始化数据库,安装数据库服务,启动数据库服务</p>

<p>5、修改数据库密码 <br data-filtered="filtered">
在“D:\Server\mysql\data” 文件夹下有个名字为xxxxxx.err的文件夹,”xxxxxx”是计算机名。找到该文件并打开,找到”[Note] A temporary password is generated for root@localhost: “。改行后的是初始化数据库root账号的默认密码,我们就是要修改该密码:</p>



<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering">mysql -u root -p
Enter password: ***********
set password <span class="hljs-keyword">for</span> root@localhost = password(<span class="hljs-string">'新密码'</span>);</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>

<p>注意:修改数据库密码命令后面的’;’不能丢。如果想退出mysql,则输入quit,回车就可以了。 <br data-filtered="filtered">
如下图:</p>

<p><img src="https://img-blog.csdn.net/20180601212111794?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>



<h5 id="45-apache配置php">4.5 Apache配置php</h5>

<p>我们刚才只是把Apache、php、mysql安装了,它们之间兵们有什么关系,我们需要让他们协同起来一起工作。Apache需要调用php服务,php需要调用mysql服务。先Apache配置php。 <br data-filtered="filtered">
1、构造php配置文件 <br data-filtered="filtered">
在目录”D:\Server\php”中有个名为php.ini-development的文件,复制一份,并重命名为php.ini,放在该文件夹中。这个是php的配置文件。 <br data-filtered="filtered">
2、修改Apache配置文件 <br data-filtered="filtered">
打开httpd.conf文件,目录为”D:\Server\Apache\conf”,在里面添加如下几行:</p>



<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering">
PHPIniDir <span class="hljs-string">"D:/Server/php/"</span>
LoadModule php7_module <span class="hljs-string">"D:/Server/php/php7apache2_4.dll"</span>
AddType application/x-httpd-php .php .html .htm</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre>

<p>第一行是加载php配置文件,第二行是加载php中的一个动态链接库,第三行是把后缀名为.php、.html、.htm的文件交给apache来处理。</p>

<p>到此apache已经配置完php了,现在apache已经可以解析php文件了。在Apache网站根目录里创建一个名为index.php的文件,并编写以下内容:</p>



<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering"><span class="hljs-preprocessor"><?php</span>
phpinfo();
<span class="hljs-preprocessor">?></span></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>

<p>重启apache。注意:所有对apache配置后都需要重启apache后才能生效。并在浏览器地址栏输入“<a href="http://localhost/index.php" target="_blank">http://localhost/index.php</a>”,如下图:</p>

<p><img src="https://img-blog.csdn.net/20180601212251340?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>



<h5 id="46-php加载mysql">4.6 php加载MySQL</h5>

<p>用记事本打开php.ini文件,这里目录为”D:\Server\php”</p>

<p>添加如下几行:</p>



<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering">extension_dir = <span class="hljs-string">"D:/Server/php/ext"</span>
extension=php_mysqli.dll
extension=php_pdo_mysql.dll</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>

<p>第一行是设置php扩展模块的目录,第二行和第三行都是加载mysql的动态链接库。</p>

<p>到此,php已经可以链接数据库,并访问数据库了。在Apache网站根目录里创建一个名为index.php的文件,并编写以下内容:</p>



<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering"><span class="hljs-preprocessor"><?php</span>

<span class="hljs-variable">$host</span> = <span class="hljs-string">"127.0.0.1"</span>; <span class="hljs-comment">//mysql主机地址</span>
<span class="hljs-variable">$user</span> = <span class="hljs-string">"root"</span>; <span class="hljs-comment">//mysql 登录账户</span>
<span class="hljs-variable">$pwd</span> = <span class="hljs-string">"你自己的mysql密码"</span>; <span class="hljs-comment">//mysql登录密码</span>
<span class="hljs-variable">$conn</span> = <span class="hljs-keyword">new</span> mysqli(<span class="hljs-variable">$host</span>, <span class="hljs-variable">$user</span>, <span class="hljs-variable">$pwd</span>); <span class="hljs-comment">//连接数据库</span>
<span class="hljs-keyword">if</span> (!<span class="hljs-variable">$conn</span>) {<span class="hljs-comment">//判断</span>
    <span class="hljs-keyword">echo</span> <span class="hljs-string">'连接数据库失败: '</span> . mysql_error();
}
<span class="hljs-keyword">echo</span> <span class="hljs-string">"mysql 连接成功!"</span>;
mysqli_close(<span class="hljs-variable">$conn</span>); <span class="hljs-comment">// 关闭mysql连接</span>

<span class="hljs-preprocessor">?></span></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li></ul></pre>

<p>重启apache。注意:所有对apache配置后都需要重启apache后才能生效。并在浏览器地址栏输入“<a href="http://localhost/index.php" target="_blank">http://localhost/index.php</a>”,如下图:</p>

<p><img src="https://img-blog.csdn.net/20180601212417453?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>

<p>到此网站服务器环境已经配置完成了,只要在apache网站默认目录中放入网站的代码就可以了。</p>



<h3 id="5-虚拟主机设置"><a name="t4"></a>5 虚拟主机设置</h3>

<p>之前说过多个域名可以映射到同一个服务器,也就是说一个服务器是可以同时挂在多个网站的。只要域名解析到该服务器,apache会把域名映射到一个文件夹中,也就是说这里的一个域名所对应的只是服务器上的一个文件夹而已。这里可以想想,如果apache(或者其他的自己开发的web服务软件)不是把域名映射到一个文件夹,而是映射到一个IP的话,这应该就是我们购买服务器时候说的公网IP和内网IP中的内网IP了把。而安装web服务软件的把域名映射到新IP的那个服务器的IP应该就是公网IP了。言归正传,设置虚拟主机主要还是apache配置问题,步骤如下:</p>

<p>1、加载httpd-vhosts.conf文件</p>

<p>打开httpd.conf文件,并找到Virtual hosts一项如下图:</p>

<p><img src="https://img-blog.csdn.net/20180601212456501?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>

<p>把#Include conf/extra/httpd-vhosts.conf,前的”#”删除,表示加载httpd-vhosts.conf这个文件。</p>

<p>2、修改httpd-vhosts.conf文件 <br data-filtered="filtered">
打开httpd-vhosts.conf文件,目录在 “D:\Server\Apache\conf\extra”,把原来的内容删除掉,添加如下:</p>



<pre class="prettyprint" name="code"><code class="hljs apache has-numbering"><span class="hljs-tag"><VirtualHost *:80></span>
    <span class="hljs-keyword"><span class="hljs-common">DocumentRoot</span></span> <span class="hljs-string">"D:/Documents/MyProjects/Web"</span>
    <span class="hljs-keyword"><span class="hljs-common">ServerName</span></span> localhost
    <span class="hljs-tag"><Directory "D:/Documents/MyProjects/Web"></span>
    <span class="hljs-keyword"><span class="hljs-common">Options</span></span> Indexes FollowSymLinks
    <span class="hljs-keyword">AllowOverride</span> None
    <span class="hljs-keyword">Require</span> <span class="hljs-literal">all</span> granted
    <span class="hljs-tag"></Directory></span>
<span class="hljs-tag"></VirtualHost></span>

<span class="hljs-tag"><VirtualHost *:80></span>
    <span class="hljs-keyword"><span class="hljs-common">DocumentRoot</span></span> <span class="hljs-string">"D:/Documents/MyProjects/BlogWeb"</span>
    <span class="hljs-keyword"><span class="hljs-common">ServerName</span></span> www.denverBlog.com
    <span class="hljs-tag"><Directory "D:/Documents/MyProjects/BlogWeb"></span>
    <span class="hljs-keyword"><span class="hljs-common">Options</span></span> Indexes FollowSymLinks
    <span class="hljs-keyword">AllowOverride</span> None
    <span class="hljs-keyword">Require</span> <span class="hljs-literal">all</span> granted
    <span class="hljs-tag"></Directory></span>
<span class="hljs-tag"></VirtualHost></span></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li></ul></pre>

<p>其中第一个是apache的默认目录,如果加载了httpd-vhosts.conf文件必须则必须在httpd-vhosts.conf中添加默认目录的部分,否则默认目录不能访问。</p>

<p>第二个则是我们添加的另一个虚拟主机的目录,其中</p>

<p>DocumentRoot :表示该虚拟主机的文件目录;</p>

<p>ServerName:表示该虚拟主机的域名。 <br data-filtered="filtered">
</p>

<pre class="prettyprint" name="code"><code class="language-php hljs  has-numbering"><span class="hljs-preprocessor"><?php</span>
        <span class="hljs-keyword">echo</span> <span class="hljs-string">"Hellow world"</span>;

        <span class="hljs-variable">$host</span> = <span class="hljs-string">"127.0.0.1"</span>; <span class="hljs-comment">//mysql主机地址</span>
        <span class="hljs-variable">$user</span> = <span class="hljs-string">"root"</span>; <span class="hljs-comment">//mysql 登录账户</span>
        <span class="hljs-variable">$pwd</span> = <span class="hljs-string">"scu@hzq159786"</span>; <span class="hljs-comment">//mysql登录密码</span>
        <span class="hljs-variable">$conn</span> = <span class="hljs-keyword">new</span> mysqli(<span class="hljs-variable">$host</span>, <span class="hljs-variable">$user</span>, <span class="hljs-variable">$pwd</span>); <span class="hljs-comment">//连接数据库</span>
        <span class="hljs-keyword">if</span> (!<span class="hljs-variable">$conn</span>) {<span class="hljs-comment">//判断</span>
            <span class="hljs-keyword">echo</span> <span class="hljs-string">'连接数据库失败: '</span> . mysql_error();
        }
        <span class="hljs-keyword">echo</span> <span class="hljs-string">"mysql 连接成功!"</span>;
        mysqli_close(<span class="hljs-variable">$conn</span>); <span class="hljs-comment">// 关闭mysql连接</span>

        phpinfo();
<span class="hljs-preprocessor">?></span></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul></pre>

<p>在浏览器中输入<a href="http://www.denverblog.com/" target="_blank">http://www.denverblog.com/</a>,结果如下:</p>

<p><img src="https://img-blog.csdn.net/20180601212602147?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tvbmdsb25nZGFuZm8x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>            </div>
            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
                </div>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值