设置VirtualBox上的Ubuntu Server 来Host网站

       首先说明一下,这里提到的Ubuntu server是安装在Windows 10上运行的VirtualBox上面的。Ubuntu server上host的网站,其访问可以分为在host machine上(也就是这里提到的Windows 10)以及在外网的访问。

web server 安装和基本设置:

        这里的Ubuntu server是20.04 LTS (Focal Fossa): 64-bit PC (AMD64) Server,具体的安装请参考我之前的介绍“Windows上安装VirtualBox运行Ubuntu Server及SSH登录”。

      web server的选择和安装有不少组合,这里选择比较流行的LAMP。LAMP不是一个软件,而是一个组合:Linux, Apache HTTP Server, MySQL/MariaDB, and PHP。这里已经有了Ubuntu server,接下来介绍一下其他的软件安装,这里选的database是MySQL。

 1.登录到Ubuntu server,检查和安装更新

      sudo apt update

      sudo apt upgrade

 2.安装Apache MySQL PHP

      sudo apt-get install apache2

      sudo apt-get install mysql-server  mysql-client

      sudo apt-get install php php-common php-mysql php-gd php-cli php-pear

      sudo apt install libapache2-mod-php

 3.基本设置—MySQL

     a.使用mysql_secure_installation,根据提示,进行选择设置,建议选择是。

          sudo mysql_secure_installation

     b.启动database server

          sudo /etc/init.d/mysql start

     c.编辑基本配置

          sudo vi /etc/mysql/my.cnf

         可以添加类似如下基本设置:


[mysqld]

max_allowed_packet = 1M

thread_stack = 128K

max_connections = 75

table_open_cache = 32M

key_buffer_size = 32M


      d.重启mysql

           sudo systemctl restart mysql

  4. 基本设置— Apache

       a.调整防火墙,确保Port打开(这里使用的是默认port)

            sudo ufw allow 80/tcp

            sudo ufw allow 443/tcp

            sudo ufw reload

            sudo ufw app list

            sudo ufw app allow ‘Apache’

            sudo ufw status

        b.启动web server

            sudo systemctl status apache2

            sudo systemctl start apache2

            sudo systemctl status apache2

   5.本地host machine连接

        完成上面的步骤后,就可以在host machine(这里是Windows 10)做测试。打开浏览器,输入ubuntu server的IP地址,比如,http://192.168.68.108 (请替换IP)就可以看到Apache2 Ubuntu Default page: 

    6.  在其他PC或者手机上连接

         为了使网页可以被外部设备访问,需要设置Ubuntu server的network和路由器的Port Forwarding。 这里可参考“Windows上安装VirtualBox运行Ubuntu Server及SSH登录”。基本方法如下:

        PC(outside) —> Router Port —> Windows 10 Port —> Ubuntu server Port

这里对应的Ubuntu server Port,默认的为80,亦可以更改。这里简单其间,我们就使用80。在VM VirtualBox Manager窗口,打开 Ubuntu server的network设置:Setting—>Network->Adapter 1->Advanced-> Port Forwarding,来设置Virtual machine的Port Forwarding, 添加一个新的Port Forwarding Rule, 比如

         Namehtml  ProtocolTCP  Host Port2024  Guest Port 80

这里Host IP和Guest IP可以为空。完成这一步,可以实现在host machine上访问。如果只是在host machine访问,这一步也可以省略,因为host machine会通过ubuntu server上默认的port 80来访问。

       路由器的Port Forwarding设置,不同的路由器各有不同,基本原则是一致的。比如路由器的ip是1.2.3.4,设置的port为2024,host machine (windows 10) IP为5.6.7.8,设置的port是2024,那么就可以通过http://1.2.3.4:2024 来访问网页。这时候就可以看到看到Apache2 Ubuntu Default page。

设计自己的网站

     有了网站的访问之后,接下来我们看如何添加一个自己的网站。

1. 可选择关闭默认的Apache的虚拟host

      sudo a2dissite *default

   -----------------------

    Site 000-default disabled.

    To activate the new configuration, you need to run:

    systemctl reload apache2

    ------------------------

2. 编辑网页

      默认的Ubuntu 允许访问的文件在/var/www,及public_html

cd /var/www/html

     创建网页文件

sudo mkdir -p mywebsite.com/{public_html,log}

    添加网页文件,比如index.html

————————————————-———

<!DOCTYPE html>

<html>

<head>

    Hellow World!

</head>

<body>

<p>Hello World!</p>

</body>

</html>

——————————————————————-

    创建虚拟host文件

        sudo vi /etc/apache2/sites-available/mywebsite.com.conf


    # domain: mywebsite.com

    # public: /var/www/html/mywebsite.com/public_html/

      # Admin email, Server Name (domain name), and any aliases

      ServerAdmin [email protected]

      ServerName  mywebsite.com

      ServerAlias www.mywebsite.com

      # Index file and Document Root (where the public files are located)

      DirectoryIndex index.html index.php

      DocumentRoot /var/www/html/mywebsite.com/public_html

      # Log file locations

      LogLevel warn

      ErrorLog  /var/www/html/mywebsite.com/log/error.log

      CustomLog /var/www/html/mywebsite.com/log/access.log combined


3. 打开mywebsite

    sudo a2ensite mywebsite.com.conf

    ------------

    Enabling site mywebsite.com.

    To activate the new configuration, you need to run:

    systemctl reload apache2

   -------------

    sudo systemctl reload apache2

         到这里,就可以通过上面介绍的访问方法,通过IP地址的相应的Port信息来访问在Ubuntu server上的网站了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章