freebsd 9.1.1
介绍 (Introduction)
A FAMP stack, which is similar to a LAMP stack on Linux, is a group of open source software that is typically installed together to enable a FreeBSD server to host dynamic websites and web apps. FAMP is an acronym that stands for FreeBSD (operating system), Apache (web server), MySQL (database server), and PHP (to process dynamic PHP content).
FAMP堆栈类似于Linux上的LAMP堆栈,是一组开源软件,通常会安装在一起以使FreeBSD服务器能够托管动态网站和Web应用程序。 援外物资项目是代表适用于F reeBSD(操作系统),A帕什(Web服务器),M ySQL(数据库服务器)和P HP(处理动态PHP内容)的首字母缩写。
In this guide, we’ll get a FAMP stack installed on a FreeBSD 12.0 cloud server using pkg
, the FreeBSD package manager.
在本指南中,我们将使用FreeBSD软件包管理器pkg
将FAMP堆栈安装在FreeBSD 12.0云服务器上。
先决条件 (Prerequisites)
Before you begin this guide you’ll need the following:
在开始本指南之前,您需要满足以下条件:
- Access to a user with root privileges (or allowed by using sudo) in order to make configuration changes. 具有root特权(或使用sudo允许)访问用户,以进行配置更改。
A firewall configured using this tutorial on Recommended Steps For New FreeBSD 12.0 Servers. Ensure you open ports
80
and443
as part of your setup.使用本教程在“新FreeBSD 12.0服务器的推荐步骤”上配置的防火墙。 确保在安装过程中打开端口
80
和443
。Familiarity with the CLI (Command Line Interface) is recommended. FreeBSD’s vi editor has almost identical syntax as vim.
第1步-安装Apache (Step 1 — Installing Apache)
The Apache web server is currently the most popular web server in the world, which makes it a great choice for hosting a website.
Apache Web服务器是当前世界上最受欢迎的Web服务器,这使其成为托管网站的绝佳选择。
You can install Apache using FreeBSD’s package manager, pkg
. A package manager allows you to install most software pain-free from a repository maintained by FreeBSD. You can learn more about how to use pkg
here.
您可以使用FreeBSD的软件包管理器pkg
安装Apache。 软件包管理器允许您从FreeBSD维护的存储库中轻松安装大多数软件。 您可以在此处了解有关如何使用pkg
更多信息。
To install Apache 2.4 using pkg
, use this command:
要使用pkg
安装Apache 2.4 ,请使用以下命令:
sudo pkg install apache24
sudo pkg安装apache 24
Enter y
at the confirmation prompt to install Apache and its dependencies.
在确认提示时输入y
以安装Apache及其依赖项。
To enable Apache as a service, add apache24_enable="YES"
to the /etc/rc.conf
file. You’ll use the sysrc
command to do just that:
要启用Apache作为服务,请将apache 24 _enable="YES"
到/etc/rc.conf
文件。 您将使用sysrc
命令执行此操作:
sudo sysrc apache24_enable="YES"
sudo sysrc apache 24 _enable =“是”
Now start Apache:
现在启动Apache:
sudo service apache24 start
sudo服务apache 24开始
To check that Apache has started you can run the following command:
要检查Apache是否已启动,可以运行以下命令:
sudo service apache24 status
sudo服务apache 24状态
As a result you’ll see something similar to:
结果,您将看到类似于以下内容:
Output
apache24 is running as pid 20815.
You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser. See the note under the next heading to find out what your public IP address is, if you do not have this information already:
您可以立即进行抽查,以通过在Web浏览器中访问服务器的公用IP地址来验证一切是否按计划进行。 如果您还没有公共IP地址,请查看下一个标题下的注释,以了解您的公共IP地址:
http://your_server_IP_address/
You will see the default FreeBSD Apache web page, which is there for testing purposes. You’ll see: It Works!, which indicates that your web server is correctly installed.
您将看到默认的FreeBSD Apache网页,该网页用于测试。 您会看到:有效! ,表示您的Web服务器已正确安装。
如何查找服务器的公共IP地址 (How To find Your Server’s Public IP Address)
If you do not know what your server’s public IP address is, there are a number of ways that you can find it. Usually, this is the address you use to connect to your server through SSH.
如果您不知道服务器的公共IP地址是什么,可以通过多种方法找到它。 通常,这是您用于通过SSH连接到服务器的地址。
If you are using DigitalOcean, you may look in the Control Panel for your server’s IP address. You may also use the DigitalOcean Metadata service, from the server itself, with this command: curl -w "\n" http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address
.
如果使用的是DigitalOcean,则可以在“控制面板”中查找服务器的IP地址。 您也可以通过以下命令从服务器本身使用DigitalOcean Metadata服务: curl -w "\n" http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address
。
A more universal way to look up the IP address is to use the ifconfig
command, on the server itself. The ifconfig
command will print out information about your network interfaces. In order to narrow down the output to only the server’s public IP address, use this command (note that the highlighted part is the name of the network interface, and may vary):
查找IP地址的一种更通用的方法是在服务器本身上使用ifconfig
命令。 ifconfig
命令将打印出有关您的网络接口的信息。 为了将输出范围缩小到仅服务器的公共IP地址,请使用以下命令(请注意,突出显示的部分是网络接口的名称,并且可能有所不同):
ifconfig vtnet0 | grep "inet " | awk '{ print $2; exit }'
ifconfig vtnet0 | grep“ inet” | awk'{print $ 2; 出口 }'
You could also use curl
to contact an outside party, like icanhazip, to tell you how it sees your server. This is done by asking a specific server what your IP address is:
您还可以使用curl
与外界联系,例如icanhazip ,以告诉您它如何看待服务器。 通过询问特定的服务器您的IP地址是什么来完成此操作:
- curl http://icanhazip.com 卷曲http://icanhazip.com
Now that you have the public IP address, you may use it in your web browser’s address bar to access your web server.
现在您有了公共IP地址,可以在Web浏览器的地址栏中使用它来访问Web服务器。
第2步-安装MySQL (Step 2 — Installing MySQL)
Now that you have your web server up and running, it is time to install MySQL, the relational database management system. The MySQL server will organize and provide access to databases where your server can store information.
现在,您的Web服务器已启动并正在运行,是时候安装关系数据库管理系统MySQL。 MySQL服务器将组织并提供对服务器可用来存储信息的数据库的访问。
Again, you can use pkg
to acquire and install your software.
同样,您可以使用pkg
来获取和安装软件。
To install MySQL 8.0 using pkg
, use this command:
要使用pkg
安装MySQL 8.0 ,请使用以下命令:
sudo pkg install mysql80-server
sudo pkg安装mysql 80 -server
Enter y
at the confirmation prompt to install the MySQL server and client packages.
在确认提示时输入y
以安装MySQL服务器和客户端软件包。
To enable MySQL server as a service, add mysql_enable="YES"
to the /etc/rc.conf
file. You can us the sysrc
command to do just that:
要启用MySQL服务器即服务,请将mysql_enable="YES"
添加到/etc/rc.conf
文件。 您可以使用sysrc
命令来做到这一点:
- sudo sysrc mysql_enable="YES" sudo sysrc mysql_enable =“是”
Now start the MySQL server with the following command:
现在,使用以下命令启动MySQL服务器:
- sudo service mysql-server start sudo服务mysql服务器启动
You can verify the service is up and running:
您可以验证服务是否已启动并正在运行:
- sudo service mysql-server status sudo服务mysql服务器状态
You’ll read something similar to the following:
您将阅读类似于以下内容的内容:
Output
mysql is running as pid 21587.
Now that your MySQL database is running, you will want to run a simple security script that will remove some dangerous defaults and slightly restrict access to your database system. Start the interactive script by running this command:
现在您MySQL数据库正在运行,您将需要运行一个简单的安全脚本,该脚本将删除一些危险的默认值并稍微限制对数据库系统的访问。 通过运行以下命令来启动交互式脚本:
- sudo mysql_secure_installation 须藤mysql_secure_installation
The prompt will ask you if you want to set a password. Since you just installed MySQL, you most likely won’t have one, so type Y
and follow the instructions:
提示将询问您是否要设置密码。 由于您刚安装了MySQL,因此很可能没有MySQL,因此请输入Y
并按照说明进行操作:
Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
New password: password
Re-enter new password: password
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
For the rest of the questions, you should hit the y
key at each prompt to accept the recommended safe values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you’ve made.
对于其余的问题,应在每次提示时按y
键以接受建议的安全值。 这将删除一些示例用户和数据库,禁用远程root登录,并加载这些新规则,以便MySQL立即尊重您所做的更改。
At this point, your database system is now set up and you can move on to installing PHP.
至此,您的数据库系统已经建立,您可以继续安装PHP。
第3步-安装PHP (Step 3 — Installing PHP)
PHP is the component of your setup that will process code to display dynamic content. It can run scripts, connect to MySQL databases to get information, and hand the processed content over to the web server to display.
PHP是安装程序的组件,它将处理代码以显示动态内容。 它可以运行脚本,连接到MySQL数据库以获取信息,并将处理后的内容移交给Web服务器进行显示。
You can once again leverage the pkg
system to install your components. You’re going to include the mod_php
, php-mysql
, and php-mysqli
package as well.
您可以再次利用pkg
系统安装组件。 您mod_php
包括mod_php
, php-mysql
和php-mysqli
软件包。
To install PHP 7.3 with pkg
, run this command:
要使用pkg
安装PHP 7.3 ,请运行以下命令:
sudo pkg install php73 php73-mysqli mod_php73
须藤pkg安装php 73 php 73 -mysqli mod_php 73
Enter y
at the confirmation prompt. This installs the php73
, mod_php73
, and php73-mysqli
packages.
在确认提示时输入y
。 这将安装php 73
, mod_php 73
和php 73 -mysqli
软件包。
Now copy the sample PHP configuration file into place with this command:
现在,使用以下命令将示例PHP配置文件复制到位:
- sudo cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini 须藤cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
Now run the rehash
command to regenerate the system’s cached information about your installed executable files:
现在,运行rehash
命令以重新生成有关已安装的可执行文件的系统缓存信息:
- rehash 重新整理
Before using PHP, you must configure it to work with Apache.
在使用PHP之前,必须先配置它以与Apache一起使用。
安装PHP模块(可选) (Installing PHP Modules (Optional))
To enhance the functionality of PHP, you can optionally install some additional modules.
为了增强PHP的功能,您可以选择安装一些其他模块。
To see the available options for PHP 7.3 modules and libraries, you can type this:
要查看PHP 7.3模块和库的可用选项,可以键入以下内容:
pkg search php73
The results will be mostly PHP 7.3 modules that you can install:
结果大部分是可以安装PHP 7.3模块:
Output
php73-7.3.5 PHP Scripting Language
php73-aphpbreakdown-2.2.2 Code-Analyzer for PHP for Compatibility Check-UP
php73-aphpunit-1.8 Testing framework for unit tests
php73-bcmath-7.3.5 The bcmath shared extension for php
php73-brotli-0.6.2 Brotli extension for PHP
php73-bsdconv-11.5.0 PHP wrapper for bsdconv
php73-bz2-7.3.5 The bz2 shared extension for php
php73-calendar-7.3.5 The calendar shared extension for php
php73-composer-1.8.4 Dependency Manager for PHP
php73-ctype-7.3.5 The ctype shared extension for php
php73-curl-7.3.5 The curl shared extension for php
php73-dba-7.3.5 The dba shared extension for php
php73-deployer-6.4.3 Deployment tool for PHP
php73-dom-7.3.5 The dom shared extension for php
...
To get more information about what each module does, you can either search the internet or you can look at the long description of the package by typing:
要获取有关每个模块的功能的更多信息,可以搜索互联网,也可以通过键入以下内容查看软件包的详细说明:
pkg search -f package_name
pkg搜索-f package_name
There will be a lot of output, with one field called Comment which will have an explanation of the functionality that the module provides.
将有很多输出,其中一个字段称为Comment ,它将解释该模块提供的功能。
For example, to find out what the php73-calendar
package does, you could type this:
例如,要了解php73-calendar
软件包的功能,可以输入以下内容:
pkg search -f php73-calendar
pkg search -f php73-calendar
Along with a large amount of other information, you’ll find something that looks like this:
连同大量其他信息,您会发现如下所示:
Output
php73-calendar-7.3.5
Name : php73-calendar
Version : 7.3.5
...
Comment : The calendar shared extension for php
...
If, after researching, you decide that you would like to install a package, you can do so by using the pkg install
command.
经过研究后,如果您决定要安装软件包,可以使用pkg install
命令进行pkg install
。
For example, if you decide that php73-calendar
is something that you need, you could type:
例如,如果您确定需要php73-calendar
则可以输入:
sudo pkg install php73-calendar
须藤pkg安装php73-calendar
If you want to install more than one module at a time, you can do that by listing each one, separated by a space, following the pkg install
command, like this:
如果要一次安装多个模块,可以通过在pkg install
命令之后列出每个模块并用空格隔开来做到这一点,如下所示:
sudo pkg install package1 package2 ...
须藤pkg install package1 package2 ...
第4步-配置Apache使用PHP模块 (Step 4 — Configuring Apache to Use PHP Module)
Apache HTTP has a dedicated directory to write configuration files into it for specific modules. You will write one of those configuration files for Apache HTTP to “speak” PHP.
Apache HTTP有一个专用目录,可将特定模块的配置文件写入其中。 您将为Apache HTTP编写这些配置文件之一,以“讲” PHP。
- sudo vi /usr/local/etc/apache24/modules.d/001_mod-php.conf 须藤vi /usr/local/etc/apache24/modules.d/001_mod-php.conf
Add the following lines to that file:
将以下行添加到该文件:
<IfModule dir_module>
DirectoryIndex index.php index.html
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
</IfModule>
Now check Apache’s HTTP configuration is in good condition:
现在检查Apache的HTTP配置是否良好:
- sudo apachectl configtest 须藤apachectl configtest
You’ll see the following output:
您将看到以下输出:
Output
Performing sanity check on apache24 configuration:
Syntax OK
Because you’ve made configuration changes in Apache you have to restart the service for those to be applied. Otherwise Apache will still work with the prior configuration.
因为您已经在Apache中进行了配置更改,所以必须重新启动该服务才能应用这些服务。 否则,Apache仍将使用先前的配置。
- sudo apachectl restart sudo apachectl重新启动
Now you can move on to testing PHP on your system.
现在,您可以继续在系统上测试PHP。
第5步-测试PHP处理 (Step 5 — Testing PHP Processing)
In order to test that your system is configured properly for PHP, you can create a very basic PHP script.
为了测试您的系统是否针对PHP进行了正确配置,您可以创建一个非常基本PHP脚本。
You’ll call this script info.php
. In order for Apache to find the file and serve it correctly, it must be saved under a specific directory—DocumentRoot
—which is where Apache will look for files when a user accesses the web server. The location of DocumentRoot
is specified in the Apache configuration file that you modified earlier (/usr/local/etc/apache24/httpd.conf
).
您将将此脚本称为info.php
。 为了让Apache查找文件并正确提供文件,必须将其保存在特定目录DocumentRoot
-当用户访问Web服务器时,Apache将在该目录中查找文件。 DocumentRoot
的位置在您先前修改的Apache配置文件( /usr/local/etc/apache24/httpd.conf
)中指定。
By default, the DocumentRoot
is set to /usr/local/www/apache24/data
. You can create the info.php
file under that location by typing:
默认情况下, DocumentRoot
设置为/usr/local/www/apache24/data
。 您可以通过键入以下info.php
在该位置下创建info.php
文件:
- sudo vi /usr/local/www/apache24/data/info.php 须藤vi /usr/local/www/apache24/data/info.php
This will open a blank file. Insert this PHP code into the file:
这将打开一个空白文件。 将此PHP代码插入文件中:
<?php phpinfo(); ?>
Save and exit.
保存并退出。
Now you can test whether your web server can correctly display content generated by a PHP script. To try this out, you can visit this page in your web browser:
现在,您可以测试Web服务器是否可以正确显示PHP脚本生成的内容。 要尝试此操作,您可以在网络浏览器中访问此页面:
http://your_server_IP_address/info.php
You’ll see a PHP FreeBSD testing page.
您会看到一个PHP FreeBSD测试页面。
This page gives you information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are being applied correctly.
该页面从PHP的角度为您提供有关服务器的信息。 这对于调试和确保正确应用设置很有用。
If this was successful, then your PHP is working as expected.
如果成功,则您PHP可以正常工作。
You should remove this file after this test because it could actually give information about your server to unauthorized users. To do this, you can type this:
测试完成后,您应该删除此文件,因为它实际上可以将有关服务器的信息提供给未经授权的用户。 为此,您可以输入以下内容:
- sudo rm /usr/local/www/apache24/data/info.php 须藤rm /usr/local/www/apache24/data/info.php
You can always recreate this page if you need to access the information again later.
如果以后需要再次访问该信息,则始终可以重新创建此页面。
结论 (Conclusion)
Now that you have a FAMP stack installed, you have many choices for what to do next. You’ve installed a platform that will allow you to install most kinds of websites and web software on your server.
现在您已经安装了FAMP堆栈,接下来有很多选择。 您已经安装了一个平台,该平台将允许您在服务器上安装大多数类型的网站和Web软件。
freebsd 9.1.1