您的位置:首页 > 运维架构 > Linux

CentOS 安装 php 运行环境

2017-07-01 14:22 246 查看
本文翻译自 How To Install Linux, Apache, MySQL, PHP (LAMP) stack On CentOS 6

什么是 LAMP

LAMP 代表一组运行 web 服务器的开源软件,是 Linux, Apache, MySQL 和 PHP 的缩写。因为本文针对 CentOS,所以本文忽略关于 linux 的部分,将对其他软件的安装进行详解。

安装 LAMP

本教程需要拥有虚拟主机服务器的 root 权限。第三步和第四部

第一步:安装 Apache

Apache 是一款免费开源软件,世界上超过50%的web 服务器运行着 Apache.

安装 apache,打开终端并键入以下命令:

sudo yum install httpd


等待安装完成后,可以立即在你的 VPS 上启动 apache:

sudo service httpd start


在浏览器中输入你的服务器 IP 地址(如http://12.34.56.789),页面显示 “It Works!”则 Apache 安装成功。

[align=middle]安装成功会显示以下界面[/align]


注:我在启动 apache 时失败,错误提示

[shuai265@localhost ~]$ sudo service httpd start
[sudo] password for shuai265:
Starting httpd: (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
[FAILED]


查明是因为我之前使用 Hexo 创建博客网站,导致 80 端口被 nginx 占用,需要停止 nginx 。停止 nginx 后,可以正常启动 apache。

如何查询服务器 IP 地址

运行以下命令查询 IP 地址

ifconfig eth0 | grep inet | awk '{ print $2 }'


第二步:安装 MySQL

MySQL 是虚拟服务器上强大的数据管理系统,可以整理查找数据。

打开终端执行以下命令安装 MySQL:

sudo yum install mysql-server
sudo service mysqld start


安装期间 MySQL 重复请求允许。两次均输入Yes后,MySQL 会开始安装。

安装完成后,可以设置 MySQL root 密码:

sudo /usr/bin/mysql_secure_installation


执行命令后将提示你键入当前密码,由于你刚刚才安装MySQL,还没有设置过密码,直接键入回车表示当前密码为空。

Enter current password for root (enter for none):
OK, successfully used password, moving on...


接着提示你是否设置 root 密码,选择 Y 后继续根据提示操作。

CentOS在安装设置 MySQL 的过程中,会提出很多问题需要你指明 yes 或 no.

最简单的处理方式是一直设置yes。设置结束之后,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是开源的 web 脚本语言,广泛应用于动态网页中。

打开虚拟服务器的终端,键入以下命令安装 PHP:

sudo yum install php php-mysql


当 PHP 出现提示时,键入 yes 即可完成安装。

PHP Modules

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


如果你想了解其中某个模型的细节,将下列命令中的 name of module 替换成你感兴趣的类库:

yum info name of the module


如果你想要安装某个类库:

sudo yum install name_of_the_module


如果需要同时安装多个类库,则用空格分隔各个类库名即可。

设置进程在服务器重启时自动运行(当 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 页面查看结果:http://12.34.56.789/info.php (确保你的 IP 填写正确)



继续不要停

安装完成 LAMP,下面你可以使用 MySQL 做更多事情(A Basic MySQL Tutorial),创建 SSL 证书 ,或者 安装 FTP 服务器

参考链接

1 Nginx 在Linux下的启动、停止和重加载

2 apache won’t start httpd service start centos 6.3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息