如何在 CentOS 6 上安装 Linux、Apache、MySQL、PHP (LAMP)
状态:已弃用
本文涵盖不再受支持的 CentOS 版本。如果您当前正在运行运行 CentOS 6 的服务器,我们强烈建议您升级或迁移到受支持的 CentOS 版本。
原因:
请参阅:
可能会对以下 DigitalOcean 教程感兴趣,因为它概述了在 CentOS 7 服务器上安装 LAMP 堆栈:
- 如何在 CentOS 7 上安装 Linux、Apache、MySQL、PHP (LAMP) 堆栈
关于灯
LAMP 堆栈是一组用于启动和运行 Web 服务器的开源软件。首字母缩写词代表 Linux、Apache、MySQL 和 PHP。由于服务器已经在运行 CentOS,因此 linux 部分得到了处理。这是安装其余部分的方法。
设置
本教程中的步骤要求虚拟专用服务器上的用户具有 root 权限。您可以在初始服务器设置教程的第 3 步和第 4 步中了解如何进行设置。
第一步——安装 Apache
Apache 是一种免费的开源软件,运行着全球 50% 以上的 Web 服务器。
要安装 apache,请打开终端并输入以下命令:
sudo yum install httpd
安装后,您可以启动在您的 VPS 上运行的 apache:
sudo service httpd start
就是这样。要检查是否安装了 Apache,请将浏览器指向服务器的 IP 地址(例如 http://12.34.56.789)。该页面应该像这样显示“It works!”字样。
如何找到服务器的 IP 地址
您可以运行以下命令来显示服务器的 IP 地址。
ifconfig eth0 | grep inet | awk '{ print $2 }'
第二步——安装 MySQL
MySQL 是一个强大的数据库管理系统,用于在虚拟服务器上组织和检索数据
要安装 MySQL,请打开终端并输入以下命令:
sudo yum install mysql-server
sudo service mysqld start
在安装过程中,MySQL 会两次询问您的许可。在你对两者都说“是”之后,MySQL 将安装。
安装完成后,您可以设置 root MySQL 密码:
sudo /usr/bin/mysql_secure_installation
提示将询问您当前的 root 密码。
由于您刚刚安装了 MySQL,您很可能没有,所以按 Enter 将其留空。
Enter current password for root (enter for none):
OK, successfully used password, moving on...
然后提示会问你是否要设置root密码。继续并选择 Y 并按照说明进行操作。
CentOS 自动执行设置 MySQL 的过程,询问您一系列是或否的问题。
对所有选项都说“是”是最简单的。最后,MySQL 将重新加载并实施新的更改。
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
第三步——安装 PHP
PHP 是一种开源网络脚本语言,广泛用于构建动态网页。
要在您的虚拟专用服务器上安装 PHP,请打开终端并输入以下命令:
sudo yum install php php-mysql
一旦您对 PHP 提示回答是,PHP 将被安装。
PHP模块
PHP 还有各种有用的库和模块,您可以将它们添加到您的服务器上。您可以通过键入以下内容来查看可用的库:
yum search php-
然后终端将显示可能的模块列表。开头看起来像这样:
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Human Language and Character Encoding Support
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-imap.x86_64 : A module for PHP applications that use IMAP
要查看有关每个模块功能的更多详细信息,请在终端中键入以下命令,将模块名称替换为您想要了解的任何库。
yum info name of the module
一旦你决定安装模块,输入:
sudo yum install name of the module
您可以通过用空格分隔每个模块的名称来一次安装多个库。
恭喜!现在你的 Droplet 上有了 LAMP 堆栈!
我们还应该将进程设置为在服务器启动时自动运行(一旦 Apache 启动,php 将自动运行):
sudo chkconfig httpd on
sudo chkconfig mysqld on
第四步——结果:查看服务器上的 PHP
尽管 LAMP 安装在您的虚拟服务器上,我们仍然可以通过创建一个快速的 php 信息页面来在线查看组件
要设置它,首先创建一个新文件:
sudo nano /var/www/html/info.php
添加以下行:
<?php
phpinfo();
?>
然后保存退出。
重新启动 apache,以便所有更改在您的虚拟服务器上生效:
sudo service httpd restart
最后访问您的 php 信息页面(确保将示例 IP 地址替换为正确的 IP 地址):http://12.34.56.789/info.php
它看起来应该与此类似。
查看更多
安装 LAMP 后,您可以继续使用 MySQL 做更多事情(安装 FTP 服务器。