CentOS和RHEL 7:安装Linux,Apache,MariaDB,PHP(LAMP)堆栈

时间:2020-01-09 10:40:20  来源:igfitidea点击:

如何使用CLI或通过基于ssh的会话在RHEL 7版或CentOS Linux 7版上安装LAMP(Linux,Apache,MariaDB,PHP)堆栈? RHEL 7已经发布,并且CentOS Linux 7正在进行许多重大更改。
此教程说明了如何安装LAMP服务器。

有关LAMP的更多信息

LAMP只是一个软件包或一个平台,由Linux操作系统,Apache Web服务器,MySQL数据库服务器和PHP(或Perl/Python)脚本语言组成。

LAMP堆栈用于完全利用自由和开源软件来构建重型动态网站。
在本教程中,我将解释如何在CentOS 7或RHEL 7上使用Linux,Apache,MySQL/MariaDB(代替MySQL),PHP(LAMP)堆栈。

步骤1:在CentOS 7/RHEL 7服务器上安装Apache

执行以下yum命令以安装Apache Web服务器:

sudo yum install httpd

输出示例:

Loaded plugins: amazon-id, rhui-lb
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-17.el7 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-17.el7 for package: httpd-2.4.6-17.el7.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-17.el7.x86_64
--> Running transaction check
---> Package httpd-tools.x86_64 0:2.4.6-17.el7 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
======================================================================================================
Package            Arch          Version               Repository                               Size
======================================================================================================
Installing:
httpd              x86_64        2.4.6-17.el7          rhui-REGION-rhel-server-releases        1.2 M
Installing for dependencies:
httpd-tools        x86_64        2.4.6-17.el7          rhui-REGION-rhel-server-releases         77 k
mailcap            noarch        2.1.41-2.el7          rhui-REGION-rhel-server-releases         31 k
 
Transaction Summary
======================================================================================================
Install  1 Package (+2 Dependent packages)
 
Total download size: 1.3 M
Installed size: 3.9 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): httpd-tools-2.4.6-17.el7.x86_64.rpm                                     |  77 kB  00:00:00     
(2/3): httpd-2.4.6-17.el7.x86_64.rpm                                           | 1.2 MB  00:00:00     
(3/3): mailcap-2.1.41-2.el7.noarch.rpm                                         |  31 kB  00:00:00     
-----------------------------------------------------------------------------------------------------
Total                                                                 2.0 MB/s | 1.3 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-tools-2.4.6-17.el7.x86_64                                                    1/3 
  Installing : mailcap-2.1.41-2.el7.noarch                                                        2/3 
  Installing : httpd-2.4.6-17.el7.x86_64                                                          3/3 
  Verifying  : mailcap-2.1.41-2.el7.noarch                                                        1/3 
  Verifying  : httpd-tools-2.4.6-17.el7.x86_64                                                    2/3 
  Verifying  : httpd-2.4.6-17.el7.x86_64                                                          3/3 
 
Installed:
  httpd.x86_64 0:2.4.6-17.el7                                                                         
 
Dependency Installed:
  httpd-tools.x86_64 0:2.4.6-17.el7                   mailcap.noarch 0:2.1.41-2.el7                  
 
Complete!

在启动时启用httpd服务

要确保httpd服务在引导时自动启动,请执行:

sudo systemctl enable httpd.service

输出示例:

ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

以下命令将在引导时禁用httpd服务:

sudo systemctl disable httpd.service

输出示例:

rm '/etc/systemd/system/multi-user.target.wants/httpd.service'

在CentOS/RHEL v7.x上启动httpd服务

sudo systemctl start httpd.service

在此阶段,您可以将Web浏览器指向服务器的IP地址,例如http://10.41.143.156。
屏幕上应显示以下页面:
检查Apache是否在CentOS/RHEL 7服务器上运行

在CentOS/RHEL v7.x上停止httpd服务

sudo systemctl stop httpd.service

在CentOS/RHEL v7.x上重新启动httpd服务

sudo systemctl restart httpd.service

在CentOS/RHEL v7.x上查找httpd服务状态

要验证httpd服务正在运行,请执行:

systemctl is-active httpd.service

输出示例:

active

在CentOS/RHEL v7.x上正常重启httpd服务

sudo apachectl graceful

在CentOS/RHEL v7.x上测试httpd/Apache配置文件是否有错误

sudo apachectl configtest

输出示例:

Syntax OK

httpd服务默认配置

  • 默认配置文件:/etc/httpd/conf/httpd.conf
  • 加载模块的配置文件:/etc/httpd/conf.modules.d/目录(例如PHP)
  • 选择MPM(处理模型)作为可加载模块[worker,prefork(默认)]和事件:/etc/httpd/conf.modules.d/00-mpm.conf
  • 默认端口:80和443(SSL)
  • 默认日志文件:/var/log/httpd/{access_log,error_log}

步骤2:在CentOS 7/RHEL 7服务器上安装MariaDB

MariaDB MySQL的增强的直接替代品。

RHEL/CentOS v7.x从MySQL迁移到MariaDB,以满足其数据库管理系统的需求。
执行以下yum命令来安装MariaDB服务器:

sudo yum install mariadb-server mariadb

要启动mariadb,请执行:

sudo systemctl start mariadb.service

要确保mariadb服务在启动时自动启动,请执行:

sudo systemctl enable mariadb.service

输出示例:

ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'

要停止/重新启动并禁用mariadb服务,请使用以下命令:

sudo systemctl stop mariadb.service #<-- Stop mariadb server
sudo systemctl restart mariadb.service #<-- Restart mariadb server
sudo systemctl disable mariadb.service #<-- Disable mariadb server
sudo systemctl is-active mariadb.service   #<-- Is mariadb server running?

保护MariaDB

执行以下命令:

sudo /usr/bin/mysql_secure_installation

输出示例:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

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

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y    
New password: YOUR-NEW-PASSWORD-HERE
Re-enter new password: YOUR-NEW-PASSWORD-HERE
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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, MariaDB 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 MariaDB
installation should now be secure.

Thanks for using MariaDB!

测试MariaDB安装

输入以下命令

mysql -u root -p

步骤3:在CentOS/RHEL v7.x上安装PHP

要安装PHP和gd/msyql之类的模块,请输入以下yum命令:

sudo yum install php php-mysql php-gd php-pear

您必须重新启动httpd(Apache)服务,执行:

sudo systemctl restart httpd.service

要搜索所有其他php模块,请执行:

sudo yum search php

输出示例:

php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-ldap.x86_64 : A module for PHP applications that use LDAP
php-mysql.x86_64 : A module for PHP applications that use MySQL databases
php-odbc.x86_64 : A module for PHP applications that use ODBC databases
php-pdo.x86_64 : A database access abstraction module for PHP applications
php-pear.noarch : PHP Extension and Application Repository framework
php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php-pgsql.x86_64 : A PostgreSQL database module for PHP
php-process.x86_64 : Modules for PHP script using system process interfaces
php-recode.x86_64 : A module for PHP applications for using the recode library
php-soap.x86_64 : A module for PHP applications that use the SOAP protocol
php-xml.x86_64 : A module for PHP applications which use XML
php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol

要查找有关模块类型的更多信息:

sudo yum info php-pgsql

要安装名为php-pgsql的php模块,请执行:

sudo yum install php-pgsql

在服务器上测试PHP

创建一个名为/var/www/html/test.php的文件,如下所示:

sudo vi /var/www/html/test.php

追加以下代码:

<?php
   phpinfo(INFO_GENERAL);
?>

保存并关闭文件。
将您的Web浏览器指向服务器的IP地址,例如http://10.41.143.156/test.php(可将10.41.143.156替换为您的实际IP地址):

http://10.41.143.156/test.php