Practical | How to host a website on Linux for free

Move your little hand to make a fortune, give it a thumbs up!

Web server can be used to refer to both hardware and software, or both working together. For the purpose of this guide, we will focus on the software side and see how to host a website on a Linux machine.

A web server is a software program that receives and responds to client requests via the HTTP/HTTPS protocol. Its main purpose is to display website content, usually in the form of text, images and video.

Web servers can serve static or dynamic content. Static content, as the name suggests, is content that rarely changes and necessarily stays the same. The server sends the content back to the user's browser as-is.

Dynamic content is content that changes frequently or is constantly being updated. To serve dynamic content, a Web server must also work with a database server and a server-side scripting language.

This guide [1] will demonstrate how to set up an Apache web server to host a website on a Linux system for free.

rely

To follow this guide, make sure you have the following.

  • A dedicated public IP address can be obtained from your ISP.
  • A Linux box, which can be a Linux server installation of your preferred OS variant. In this guide, we will be using Debian 11.

You'll also need to install a LAMP server, which is an acronym for Linux, Apache, and MySQL (and can also be MariaDB).

How to Host a Website on a Linux Server

In this section, we continue our discussion of the main components of a web server.

What is Apache?

Apache is a popular free and open source cross-platform web server released under the Apache License 2.0. It is one of the most widely used web servers with nearly 32.2% web server market share.

To check the latest version of Apache available and whether it is installed on your server, run the following command:

apt-cache policy apache2 (On Debian-based OS)

From the output, you can see that the parameter Installed: (none) means it is not installed yet. You can also get information about the latest version available from the Debian/Ubuntu repositories, in this case 2.4.52.

alt

在现代 Red Hat 发行版上,您可以使用以下 dnf 命令检查 Apache 的可用性,如下所示。

dnf search httpd
alt

从上面的输出中,您可以看到 Apache httpd 包可供下载。如果您的系统上未安装 Apache,请使用“apt”或“dnf”包管理器来安装 Apache,如图所示。

在基于 Debian 的系统上:

$ sudo apt install apache2 -y   
$ sudo systemctl start apache2  
$ sudo systemctl enable apache2  
$ sudo systemctl status apache2
alt

在基于 Red Hat 的系统上:

# dnf install httpd -y   
# systemctl start httpd  
# systemctl enable httpd  
# systemctl status httpd
alt

什么是 MariaDB?

MariaDB 是 MySQL 的一个分支,是最流行的开源关系数据库管理系统之一。如今,它比 MySQL 更受欢迎,因为它具有更快的复制和执行查询速度以及安全性和大量的存储引擎。

要在基于 Debian 的系统上安装 MariaDB:

$ sudo apt install mariadb-server mariadb-client -y  
$ sudo systemctl start mariadb  
$ sudo systemctl enable mariadb  
$ sudo systemctl status mariadb  

以下输出显示 MariaDB 已安装并按预期运行。

alt

要在基于 RHEL 的系统上安装 MariaDB:

# dnf install mariadb-server -y  
# systemctl start mariadb  
# systemctl enable mariadb  
# systemctl status mariadb 
alt

什么是 PHP?

PHP 是 PHP 超文本预处理器的递归缩写,它是一种流行的通用脚本语言,主要用于 Web 开发。

要在基于 Debian 的系统上安装 PHP:

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install  ca-certificates apt-transport-https software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
$ sudo apt install php8.0 libapache2-mod-php8.0 

要在基于 RHEL 的系统上安装 PHP,您需要首先启用 EPEL 存储库。

$ sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm  [RHEL 9]
$ sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm  [RHEL 8]

接下来,启用 Remi 存储库,它在基于 RHEL 的系统上提供最新版本的 PHP。

$ sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm  [RHEL 8]
$ sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm  [RHEL 8]

在系统上启用 EPEL 和 Remi 存储库后,您可以如图所示安装 PHP。

# dnf module list php
# dnf module enable php:remi-8.0 -y 
# dnf install php php-cli php-common

安装所有组件后,您现在可以使用 WordPress CMS 构建网站,该软件使用户可以轻松开发和管理网站,而无需了解 HTML、CSS、PHP 和 Javascript 等网页设计语言。

WordPress 建站

为了进行演示,我们将在 Debian 11 和 RHEL 9 系统上安装 WordPress,这将提供一个示例网站,可以根据您的喜好进一步定制。

本节假设您已经安装了 LAMP 堆栈。

1. 安装附加 PHP 模块

要继续,请安装 WordPress 所需的其他 PHP 模块,如图所示。

要在基于 Debian 的系统上安装 PHP 模块:

$ sudo apt install php libapache2-mod-php php-pear php-cgi php-common php-mbstring php-zip php-net-socket php-gd php-mysql php-bcmath

要在基于 RHEL 的系统上安装 PHP 模块:

# dnf install php-gd php-soap php-intl php-mysqlnd php-pdo php-bcmath php-curl php-zip php-xmlrpc wget

2. 为 WordPress 创建数据库

WordPress 用 PHP 编写,是一个数据驱动的、免费的开源内容管理系统。数据库是 WordPress 的重要组成部分。

该数据库用于存储所有博客文章、页面、类别、评论、主题、插件以及 WordPress 配置文件。

要为 WordPress 创建数据库,请登录 MariaDB 数据库服务器:

$ sudo mysql -u root -p

接下来,创建数据库,如图所示

CREATE DATABASE wordpress_db;

接下来,创建一个数据库用户并将数据库上的所有权限分配给该用户。

GRANT ALL PRIVILEGES ON wordpress_db.* to wordpress_user@localhost identified by 'P@ssword321';

然后最后重新加载授权表以保存所做的更改并退出数据库。

FLUSH PRIVILEGES;
QUIT;

3. 下载WordPress

数据库就位后,继续使用 wget 命令下载最新的 WordPress tarball 文件。

$ wget https://wordpress.org/latest.tar.gz

下载后,使用 tar 命令解压缩压缩文件。

$ tar -xvzf latest.tar.gz

该命令将文件的内容提取到名为 wordpress 的文件夹中。将文件夹移动或复制到 Apache Web 服务器的文档根目录中。

$ sudo mv wordpress/ /var/www/html/

接下来,分配以下权限和所有权。

$ sudo chmod 755 -R /var/www/html/wordpress/
$ sudo chown -R www-data:www-data /var/www/html/wordpress/

4. 为 WordPress 创建 Apache 虚拟主机

术语虚拟主机是指在单个服务器上托管多个网站的做法。如果您打算在一台服务器上托管多个网站,则需要为每个网站创建一个虚拟主机。

在这种情况下,您需要为 WordPress 网站创建虚拟主机,如下所示。

$ sudo nano /etc/apache2/sites-available/wordpress.conf  [On Debian]
# vi /etc/httpd/conf/httpd.conf [On RHEL]

粘贴以下代码行来定义虚拟主机。对于 ServerName 指令,提供服务器的 IP 地址或完全限定域名,它应指向专用公共 IP 地址。

<VirtualHost *:80>
     ServerAdmin admin@your_domain.com
     DocumentRoot /var/www/html/wordpress
     ServerName 192.168.0.100

     <Directory /var/www/html/wordpress>
          Options FollowSymlinks
          AllowOverride All
          Require all granted
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log
     CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined

</VirtualHost>

保存更改并退出文件。

要连接到数据库,需要进行一些额外的修改。因此,导航到 wordpress 文件夹。

cd /var/www/html/wordpress/

接下来,使用 wp-config-sample.php 文件的内容更新 wp-config.php 文件。

$ cp wp-config-sample.php wp-config.php
$ sudo nano wp-config.php

接下来,使用数据库详细信息更新数据库名称、数据库用户名和密码指令。

接下来,在基于 Debian 的系统上启用新的 WordPress 站点,如下所示。

$ sudo ln -s /etc/apache2/sites-available/wordpress.conf /etc/apache2/sites-enabled/wordpress.conf
$ sudo a2ensite wordpress
$ sudo a2enmod rewrite
$ sudo a2dissite 000-default

要使更改生效,请重新启动 Apache。

$ sudo systemctl restart apache2   [On Debian]
# systemctl restart httpd  [On RHEL]

5. 在浏览器上完成 WordPress 设置

要完成设置,请浏览 Web 服务器的 IP 地址,如下所示:

http://server-ip

您应该会看到 WordPress 欢迎页面,如图所示。选择您的首选语言,然后单击“继续”。

alt

接下来,填写站点详细信息。

alt

然后单击“安装 WordPress”以完成 WordPress 设置。

alt

如果一切顺利,您将收到安装成功的确认信息。要登录,请单击“登录”按钮。

alt

如您所见,这将引导您进入 WordPress 仪表板。此时,您可以尝试使用各种主题来增强示例网站的外观。

alt

6. 使用端口转发访问 WordPress

由于您是通过家里的 Linux 系统或局域网 (LAN) 自托管 Web 服务器,因此下一步是让外部用户或 LAN(局域网)之外的用户可以访问它。这就是端口转发的用武之地。

端口转发,也称为端口映射,是一种允许外部设备通过 Internet 访问专用网络内的服务器或资源的技术。整个想法是从外部访问专用网络,否则这是不可能的,因为外部设备无法与内部 IP 地址通信。

在您的设置中,您需要转发 Web 服务器正在侦听的端口(在大多数情况下,对于 HTTP 流量是端口 80,对于 HTTPS 是端口 443)以及 Web 服务器的静态专用 IP 地址。

因此,登录您的路由器并前往端口转发部分。在我们的示例中,我们使用 DLink 路由器将 Web 服务器的端口(80 和 443)和私有 IP (192.168.0.100) 端口转发到 ISP 分配的专用 IP 公共 IP。

根据您的情况,指定 Web 服务器的端口和专用 IP 并保存更改。

alt

要保存更改,您可能需要重新启动路由器。所以,继续做吧。

正确执行端口转发后,您现在可以通过公共 IP 地址访问网络外部的 Web 服务器。

总结

在本指南中,我们演示了如何在 Linux 机器上使用 Apache 自行托管 Web 服务器。欢迎您对本指南提供反馈。

Reference

[1]

Source: https://www.tecmint.com/host-website-locally/

本文由 mdnice 多平台发布

Guess you like

Origin blog.csdn.net/swindler_ice/article/details/131605540