如何在 CentOS 6 上安装 Linux、nginx、MySQL、PHP (LEMP)如何在 CentOS 6 上安装 Linux、nginx、MySQL、PHP (LEMP)如何在 CentOS 6 上安装 Linux、nginx、MySQL、PHP (LEMP)如何在 CentOS 6 上安装 Linux、nginx、MySQL、PHP (LEMP)
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 CentOS 6 上安装 Linux、nginx、MySQL、PHP (LEMP)

状态:已弃用

本文涵盖不再受支持的 CentOS 版本。如果您当前正在运行运行 CentOS 6 的服务器,我们强烈建议您升级或迁移到受支持的 CentOS 版本。

原因:

请参阅:

可能会对以下 DigitalOcean 教程感兴趣,因为它概述了在 CentOS 7 服务器上安装 LEMP 堆栈:

  • 如何在 CentOS 7 上安装 Linux、Nginx、MySQL、PHP (LEMP) 堆栈

关于 Lemp

LEMP 堆栈是一组开源软件,用于启动和运行 Web 服务器。该首字母缩写词代表 Linux、nginx(发音为 Engine x)、MySQL 和 PHP。由于服务器已经在运行 CentOS,因此 linux 部分得到了处理。这是安装其余部分的方法。

第一步——安装所需的存储库

我们将使用 Yum 安装所有必需的软件。但是,由于不能直接从 CentOS 使用 nginx,因此我们需要安装 epel 存储库。

sudo yum install epel-release

第二步——安装 MySQL

下一步是开始在虚拟专用服务器上安装服务器软件,从 MySQL 和依赖项开始。

 sudo yum install mysql-server

下载完成后,重启MySQL:

sudo /etc/init.d/mysqld restart

您可以使用以下命令对 MySQL 进行一些配置:

sudo /usr/bin/mysql_secure_installation

提示将询问您当前的 root 密码。

因为你刚刚安装了 MySQL,你很可能没有,所以按回车键留空。

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!

第三步——安装nginx

与 MySQL 一样,我们将使用 yum 在我们的虚拟专用服务器上安装 nginx:

sudo yum install nginx

nginx 不会自行启动。要运行 nginx,请键入:

sudo /etc/init.d/nginx start

您可以通过将浏览器定向到您的 IP 地址来确认 nginx 已安装在您的虚拟专用服务器上。

您可以运行以下命令来显示服务器的 IP 地址。

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

第四步——安装 PHP

php-fpm 包位于 REMI 存储库中,此时它已被禁用。我们需要做的第一件事是启用 REMI 存储库并安装 php 和 php-fpm:

sudo yum install php-fpm php-mysql

第五步——配置 php

我们需要对 php 配置做一点小改动。打开 php.ini:

 sudo vi /etc/php.ini

找到 cgi.fix_pathinfo=1 行,并将 1 更改为 0。

cgi.fix_pathinfo=0

如果这个数字保持为 1,php 解释器将尽最大努力处理尽可能接近请求文件的文件。这是一个可能的安全风险。如果这个数字设置为 0,相反,解释器将只处理确切的文件路径——一个更安全的选择。保存并退出。

第六步——配置nginx

打开默认的 nginx 配置文件:

sudo vi /etc/nginx/nginx.conf

将工作进程数增加到 4,然后保存并退出该文件。

现在我们应该配置 nginx 虚拟主机。

为了使默认的 nginx 文件更简洁,虚拟主机详细信息位于不同的位置。

sudo vi /etc/nginx/conf.d/default.conf

配置应包括以下更改(更改的详细信息在配置信息下):

#
# The default server
#
server {
    listen       80;
    server_name example.com;

   
    location / {
        root   /usr/share/nginx/html;
        index index.php  index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

以下是更改的详细信息:

  • 在索引行中添加 index.php。
  • 将server_name改为你的域名或IP地址(替换配置中的example.com)
  • 将根更改为/usr/share/nginx/html;
  • 取消注释以 \location ~ \.php${\ 开头的部分,
  • 更改根以访问实际文档根,/usr/share/nginx/html;
  • 更改 fastcgi_param 行以帮助 PHP 解释器找到我们存储在文档根目录中的 PHP 脚本。

保存并退出

打开 php-fpm 配置:

sudo vi /etc/php-fpm.d/www.conf

将用户和组中的apache替换为nginx:

[...]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;	will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
[...]

重启 php-fpm 完成。

sudo service php-fpm restart

第七步——结果:创建一个 php 信息页面

尽管安装了 LEMP,我们仍然可以通过创建一个快速的 php 信息页面来在线查看组件

要设置它,首先创建一个新文件:

sudo vi /usr/share/nginx/html/info.php

添加以下行:

<?php
phpinfo();
?>

然后保存退出。

重新启动 nginx 以使所有更改生效:

sudo service nginx restart

最后访问您的 php 信息页面(确保将示例 IP 地址替换为正确的 IP 地址):http://12.34.56.789/info.php

它看起来应该与此类似。

第八步——设置自动启动

你快完成了。最后一步是将所有新安装的程序设置为在 VPS 启动时自动启动。

sudo chkconfig --levels 235 mysqld on
sudo chkconfig --levels 235 nginx on
sudo chkconfig --levels 235 php-fpm on
©2015-2025 艾丽卡 support@alaica.com