Home >Backend Development >PHP Tutorial >Set up a free dedicated web server: a step-by-step guide

Set up a free dedicated web server: a step-by-step guide

王林
王林Original
2023-09-04 20:05:011354browse

Behind all great websites is a great server. In this tutorial, I'll show you how to set up a dedicated web server (using Apache, MySQL, and PHP) using an old computer at home and some free software. p>


1. Quick Overview

In this tutorial, our goal is to accomplish a few things:

  • We will install the Ubuntu Server operating system. I usually use Ubuntu because it's easy to use and

    Simple management. There is also a sizable and extremely active community behind it, which makes

    Support Breeze.

  • We will install the OpenSSH server. This allows you to manage your server from a remote computer.
  • The LAMP (Linux, Apache, MySQL and PHP) stack will be installed. This provides the backbone to run

    your web page. Apache is the industry-standard web server on Unix-based operating systems; this is what most web hosts do

    use (NETTUTS is using it now!), that's what we're going to use.

  • We will install a firewall to protect your server from unauthorized access.

In order to follow this tutorial, you will need a few items:

  • The computer used as the server. It doesn't need to be powerful; as long as it's not ancient, it should work fine. please

    Don't do this on a desktop computer; Ubuntu will completely wipe your computer.

  • A CD burner and a blank CD. These are so you can burn Ubuntu to a disk so you can install it.
  • time. Let’s be honest, this process is very time consuming, especially when you run into problems. Try setting aside an afternoon

    Follow this tutorial.

You may ask why you would want to have your own web server. There are several reasons, some of them are: You can do your own testing

The basics of your website; with just a few modifications, you can host your own website; and, as you go, you'll learn a lot about Linux/Unix.

With that being said, let’s get started!


2.Download Ubuntu Server

First, we need a CD containing Ubuntu. Point your web browser to http://www.ubuntu.com/,

Then click "Download" from the left menu. You will now see a box with two tabs: "Desktop" and

"Server Edition". Click the Server Version tab and select Ubuntu 8.04 LTS. Next, select the download location from

Drop down box. Finally, click on the "Start Download" button.

设置免费专用 Web 服务器:分步指南

Now you need to burn the ISO (the file you downloaded) to a blank CD. If you don't know how to do this you can use

An excellent guide is at https://help.ubuntu.com/community/BurningIsoHowto


3.Install Ubuntu server

Now that you have downloaded and burned the ISO, let's install Ubuntu on your server. Place the disk in the drive and boot from the CD. In most modern computers, this happens by default if the disk is in the drive when you open it. If not, then you need to press a key on your keyboard when opening it. For my laptop it's F12 and for my server it's F2. It just depends on your computer. You can find it by looking at the text on the screen when you turn on your computer during the BIOS. You'll see something like "Press [KEY] to change boot order." Press this key and select your CD drive.

Are you still with me? OK Now that you have booted Ubuntu, you should see the following screen:

设置免费专用 Web 服务器:分步指南

Select your language and press Enter. Now you will see this screen:

设置免费专用 Web 服务器:分步指南

Select "Install Ubuntu Server" and we're good to go!

The installer will now ask you if you want it to detect your keyboard layout. Personally, I always choose no because

Selecting the standard US keyboard from the list is faster than letting the installer detect it. Any option is fine,

Just follow the on-screen instructions.

Once you have done this, you will now see a bunch of loading screens that say things like "Detect CD-ROM drive".

These should go through quickly and without issue. However, during these screens the installer will attempt to automatically configure your

Network settings. For the most part, this will work without complaint. But if it doesn't work for you, just follow

On-screen instructions make it work.

After doing all this, it will ask you for your hostname. You can usually set it to any value; I always set it to

My "webserver".

设置免费专用 Web 服务器:分步指南

You will now be asked to set the time zone for your clock. For me, it's the Pacific Ocean. Select one that applies

you.

Now, the system will detect more hardware and you will be prompted to "Partition the disk". Select "Boot-Use Entire"

disk".

设置免费专用 Web 服务器:分步指南

You now need to select the disk you want to partition. For most setups, only one disk is available; however,

for more specialized systems, more options will be available here. Choose the one that applies to you.

设置免费专用 Web 服务器:分步指南

It will ask you if you want to write the changes to disk. Select Yes and press Enter. The installer will now continue

Format the drive and set up partitions.

Now a miracle happened. The system will start the installation. When this happens, go get a cup of coffee. This can be taken anywhere

From 10 minutes to an hour. It just depends on your system. Sometimes it may look like it's frozen; don't worry,

But in fact, it's not. Just let it do its thing. But if it stays on an object for more than an hour, then yes, it will be frozen.

设置免费专用 Web 服务器:分步指南

Now that the system has been installed, you need to set up the account you want to log in to. First, give full play to

Name it and click Continue.

设置免费专用 Web 服务器:分步指南

Now give it your username. It usually just sets it to your name,

But you can change it. One name you cannot use is "root".

设置免费专用 Web 服务器:分步指南

You will now be asked to provide your password. You must choose a strong password or your server will not

It must be safe. I recommend at least a mix of numbers, lowercase letters, and uppercase letters. However, for my server

I use symbols as well as a mixture of the above symbols. Don't use a password shorter than 7 characters.

设置免费专用 Web 服务器:分步指南

Then, re-enter your password to verify that the password you entered is correct.

The system will now try to configure the "Package Manager" (we'll learn what that is soon). Provide your

Proxy information, or leave it blank if you are not using a proxy and select Continue.

设置免费专用 Web 服务器:分步指南

The system will now scan multiple servers for updates and configuration settings.

设置免费专用 Web 服务器:分步指南

Once completed, you will see several options to install the server software. Now, listen carefully.

Select the OpenSSH server and press the space bar instead of the enter key. If you press Enter, the installation will continue without installing the OpenSSH server.

You can also install "LAMP Server" but I have no experience with this option so we will install it using a different way

Order later.

设置免费专用 Web 服务器:分步指南

The system will now install the software you selected along with other system components.

设置免费专用 Web 服务器:分步指南

Finally, the installation will be completed. Remove the CD and press Enter. The computer will restart. If all goes well, you will

The screen that appears looks similar to the following:

设置免费专用 Web 服务器:分步指南

Congratulations! You've just finished the hardest part. Ubuntu is now installed, it's time to turn this computer into a

Network Server.


4.Update your new server

Before continuing, we need to make sure your server is up to date. To do this you need to log in. First, enter your username

(the one you selected earlier), press Enter, and then type your password. When you enter your password you will notice that there is nothing

It seems to be happening. Don't worry, that's how it's designed to work. After entering the password, press Enter,

If all goes well, your screen should look like this:

设置免费专用 Web 服务器:分步指南

Now, enter:

sudo aptitude update && sudo aptitude dist-upgrade

It will ask you for your password and you won't see anything when you enter it. Once this is done it will ask you if

You want to continue. Type "y" and press Enter. Your screen will look similar to the following:

设置免费专用 Web 服务器:分步指南

Your system will now download and install all the latest updates. This will take a while depending on your internet connection. after

It is complete and your computer will need to restart. To do this, type:

sudo shutdown -r now

and let it restart. Your server is now fully updated.


Quick Note on "Sudo"

Now, you may have noticed that all the commands you enter start with "sudo". This is because they need

Administrator rights, that's what "sudo" does. It runs the command as administrator (i.e. "shutdown") to make it work

Appropriately. That's why it asks you to enter your password. However, after you type "sudo" once and enter your password,

You do not need to enter your password again for five minutes. Not all commands require sudo, only commands that modify some part of the system do.

Have you mastered these? very good.


5.Install Apache, MySQL and PHP

Now it's time to install some programs. In order to access your website from the internet we need to install a web server (Apache). In addition to the web server, we will

A database server (MySQL) and a server-side language (PHP) are also required so that we can run popular applications like WordPress. so,

let's start!

Installing programs on Ubuntu is very different from installing programs on Windows or Windows

OS X, Ubuntu will download and install the program for you with a simple command. This is because Ubuntu has a software called

Package Manager, which manages almost all programs on the system. All we have to do is tell the package manager

(called "aptitude") We want it to install Apache, MySQL, and PHP. To do this, type the following command:

sudo aptitude install apache2 php5-mysql libapache2-mod-php5 mysql-server

Then press Enter. Aptitude will download and install the programs you specify. It will also download and install anything

Dependencies.

设置免费专用 Web 服务器:分步指南

During the installation process, MySQL will ask you to enter the root password. You can set it to any value, just make sure it's long and safe.

Whatever you do, don't leave this blank.

设置免费专用 Web 服务器:分步指南

When everything is complete, you now have a fully operational web server. To test it, first find the server's IP by typing:

ifconfig | grep inet

设置免费专用 Web 服务器:分步指南

Usually the first IP returned. In my case it's 192.168.177.129. Now that you know the IP, open a web browser and point to it

to your server IP. If you see the "It works!" message, then congratulations, it worked.

设置免费专用 Web 服务器:分步指南

But, we're not done yet. We don't want Apache or PHP to leak any information about themselves because it's not needed

Executed by your users, may pose a security risk. First, back up the original Apache configuration file:

sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak

Now open the configuration file:

sudo nano /etc/apache2/apache2.conf

Scroll down (down arrow) to where it says "ServerTokens Full" and change it to "ServerTokens Prod"

设置免费专用 Web 服务器:分步指南

Now, scroll down further and change "ServerSignature On" to "ServerSignature Off"

设置免费专用 Web 服务器:分步指南

Finally, press Control-O, then Control-X. This will save the file and exit the text editor.

Now, we need to do the same thing with PHP. First, back up the original PHP configuration file:

sudo cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini.bak

Open configuration file:

sudo nano /etc/php5/apache2/php.ini

将“expose_php = 开”更改为“expose_php = 关”

设置免费专用 Web 服务器:分步指南

再次按 Control-O,然后按 Control-X。现在配置文件已更新,重新启动 Apache:

sudo /etc/init.d/apache2 restart

您已完成 Apache、MySQL 和 PHP 的设置。


6.安装防火墙

我们现在将通过安装 Shorewall(一种命令行防火墙)来进一步锁定我们的服务器。安装它:

sudo aptitude install shorewall

默认情况下,Shorewall 安装时没有任何规则,允许完全访问。然而,这不是我们想要的行为。

相反,我们将阻止除端口 80 (HTTP) 和端口 22 (SSH) 之外的所有连接。首先复制配置

文件到 Shorewall 目录:

sudo cp /usr/share/doc/shorewall-common/examples/one-interface/* /etc/shorewall/

现在,打开“规则”文件:

sudo nano /etc/shorewall/rules

在“#LAST LINE”上方添加这些行

HTTP/ACCEPT	net		$FW
SSH/ACCEPT	net		$FW

设置免费专用 Web 服务器:分步指南

然后按 Control-O 和 Control-X。您的防火墙现已配置为仅接受 HTTP 和 SSH 流量。我们需要做的最后一件事

要做的就是告诉 Shorewall 在启动时启动。因此,打开主 Shorewall 配置文件:

sudo nano /etc/shorewall/shorewall.conf

向下滚动到“STARTUP_ENABLED=No”并将其设置为“STARTUP_ENABLED=Yes”

设置免费专用 Web 服务器:分步指南

按 Control-O 和 Control-X。现在,打开 Shorewall 默认配置文件:

sudo nano /etc/default/shorewall

并将“startup=0”更改为“startup=1”。按 Control-O 和 Control-X。最后,启动防火墙:

sudo /etc/init.d/shorewall start

恭喜!您的防火墙现已设置并保护您的服务器。


7. 将您的网站添加到您的网络服务器

现在您已完成所有设置,您可能想向其中添加一个网站。默认情况下,Apache 提供的所有文件

互联网上的地址位于“/var/www/”。但是,您无法写入此文件夹。让我们做到这一点,以便您可以:

sudo usermod -g www-data [YOUR USERNAME]
sudo chown -R www-data:www-data /var/www
sudo chmod -R 775 /var/www

发生的情况是,您将自己添加到“www-data”组,并使网站文件夹对“www-data”成员可写

组。

现在,您将使用 SFTP(不要与 FTPS 混淆)登录服务器。一些支持 SFTP 的客户端有:

WinSCP(Windows,免费),FileZilla(Windows,Linux,OS X,免费),

Cyber​​duck(OS X,免费),以及我个人最喜欢的 Transmit

(OS X,30 美元)

使用您的用户名和密码连接到您的服务器,如果您的客户端支持,则使用默认路径“/var/www”(如果不支持,则使用默认路径)

登录后只需浏览到 /var/www ):(传输如图)

设置免费专用 Web 服务器:分步指南

您现在可以将文件添加到此文件夹 (/var/www),当您使用您的浏览器浏览该文件夹时,它们将显示在您的服务器上

网络浏览器。

现在,您可能想知道为什么我们使用 SFTP 而不是 FTP。主要是因为 SFTP 已内置于 OpenSSH(您安装的)

较早)。然而,它也比 FTP 安全得多,并且使恶意用户很难(如果不是不可能)获得访问权限

您的登录凭据。


8. 让您的服务器可访问 Internet

如今,大多数现代家庭网络都位于路由器后面。因此,如果没有

,您的网络服务器将不会对互联网可见。 一点工作。由于我没有可用的所有路由器进行测试,因此我只能为您提供该领域的一般指导。

有两种方法可以将服务器开放到互联网:DMZ 或端口转发。您会注意到的主要区别是,对于 DMZ,您的服务器使用

我们之前安装的防火墙是为了保护自身。但是,通过端口转发,您的服务器将受到路由器防火墙的保护。

但是,在我们继续之前,您需要为您的服务器提供一个静态 LAN 地址。为此,请登录您的路由器,然后查找某些内容

类似于“静态 IP”或“静态路由”。为服务器提供静态 LAN 地址后,您可以执行接下来的部分。请记住,

Google 是您的朋友。

要转发,有一个很棒的网站,PortForward.com,虽然丑陋,但可以

帮助您完成几乎所有路由器的工作。您要转发的端口是 22 和 80。

要创建 DMZ,您需要登录路由器并查找“DMZ 设置”之类的内容。找到它后,将您的服务器添加到

DMZ,然后您就可以了。同样,Google 在这种情况下会很有帮助。

Now, find your public IP and voila! You can access your server from anywhere as long

Because your IP won't change.


9.Remotely manage your server

In addition to allowing you to upload files, OpenSSH also allows you to log into your server from anywhere, as long as you know its IP. For Windows you would

An SSH client is required. I recommend putty. For OS X, SSH is

Installed. Just open a terminal and type "ssh you@yourip". For Putty, select SSH and enter your IP, username, and password

When it asks. You'll notice that after logging in, it looks exactly like the screen on the server:

设置免费专用 Web 服务器:分步指南

You can do anything from here that you would actually do sitting on the server. To log out of the server, just type "exit" and press Enter.


10. That’s it!

You now have a fully functional web server. It's a great testing ground, even suitable for hosting websites with fairly low traffic. There's obviously a lot more to learn, but hopefully you already have some understanding of how a web server works. If you want to learn more about the topics I cover, here are some great guides:

install software
  • Setting up Apache, MySQL and PHP
  • Coastal Wall Configuration Guide
  • How to forward
  • Also, be sure to check out the thousands of useful PHP scripts on Envato Market. Everything from forms and shopping carts to database abstractions are sure to help you in your next phase of work as a developer.

设置免费专用 Web 服务器:分步指南

The above is the detailed content of Set up a free dedicated web server: a step-by-step guide. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn