在CentOS 7中安装LAMP堆栈(Apache,MariaDB,PHP)
今天,我们将在CentOS 7 64位服务器中安装 LAMP 堆栈。
正如我们所知道的那样, LAMP 堆栈是Linux,Apache Web服务器,MySQL/MariaDB,PHP的组合。
LAMP 堆栈用于部署基于Web的应用程序和主机动态。
安装和配置 LAMP 堆栈是微不足道的。
让我们跳过理论部分并立即开始实际部分。
虽然它在CentOS上进行了测试,但这些步骤对于Rhel,Fedora和科学Linux发行版是相同的。
1.安装apache.
以root用户运行以下命令以安装apache webserver:
yum install httpd
然后,使用命令启动并启用Apache服务:
systemctl start httpd
systemctl enable httpd
接下来,通过防火墙或者路由器允许HTTP(端口80)和HTTPS(端口443)服务。
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
重新启动防火墙以生效更改。
systemctl restart firewalld
测试apache.
打开Web浏览器并导航到http://localhost /或者http://ip_address /。
我们将对遵循Apache测试页面很满意。
如果你看到这个页面,很棒!阿帕奇正在工作!!
2.安装MariaDB.
使用命令安装MariaDB服务器:
yum install mariadb-server mariadb
首先启动并启用MariaDB服务,如下所示。
systemctl start mariadb
systemctl enable mariadb
设置数据库root密码
默认情况下,MySQL root密码为空,不建议生产使用。
我们需要将root密码设置为未经授权的用户保护数据库访问。
要设置数据库root密码,请运行:
mysql_secure_installation
当它要求我们设置root用户的密码时,按Enter键。
为root用户输入两次的强密码,并继续默认值。
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found 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 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] ## Press ENTER New password: ## Set new password for database root user Re-enter new password: ## Re-enter new password 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] ## Press ENTER ... 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] ## Press ENTER ... 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] ## Press ENTER - 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] ## Press ENTER ... 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!
数据库root密码已分配。
3.安装PHP.
以root用户身份运行以下命令以安装PHP及其模块:
yum install php php-mysql php-gd php-pear
测试PHP:
现在让我们检查PHP是否正常工作。
为此,请在Web根文件夹中创建一个示例"info.php"文件:
vi /var/www/html/info.php
添加以下行:
<?php phpinfo(); ?>
重新启动Apache服务以生效更改:
systemctl restart httpd
打开Web浏览器并导航到http://ip_address/info.php。
我们将看到以下PHP页面,列出了所有已安装的模块和其他详细信息,如PHP版本,构建日期和命令等。
要安装所有PHP模块(但不推荐),运行:
yum install php*
重新启动Apache服务以生效更改。
刷新PHP测试页面以查看新安装的模块或者组件列表。
4.安装phpmyadmin.
PHPMYADMIN是一个免费的开源数据库管理工具,用于管理来自Web浏览器的MySQL/MariaDB数据库。
PHPMyAdmin不在默认存储库中使用。
要安装它,我们需要使用命令启用epel存储库:
yum install epel-release
然后,安装phpmyadmin,如下所示:
yum install phpmyadmin
现在,打开Web浏览器并使用URL http://localhost/phpmyadmin访问phpmyadmin。
输入mysql/mariadb根用户名及其密码。
如果我们想从远程系统访问PHPMyAdmin,我们需要执行一些另外的步骤。
配置phpmyadmin.
我们可能知道,PHPMyAdmin默认情况下只能从localhost本身访问。
要从网络上的任何主机访问PHPMyAdmin,我们需要执行一些其他步骤。
请记住,除非由SSL妥善保护,否则允许PHPMyAdmin向其他除LocalHost以外的任何人视为危险。
我们仍然希望从远程系统中访问它,编辑PHPMyAdmin.conf文件:
vi /etc/httpd/conf.d/phpMyAdmin.conf
查找并注释出整个<目录>部分。
#<Directory /usr/share/phpMyAdmin # AddDefaultCharset UTF-8 # <IfModule mod_authz_core.c> # # Apache 2.4 # <RequireAny> # Require ip 127.0.0.1 # Require ip ::1 # </RequireAny> # </IfModule> # <IfModule !mod_authz_core.c> # # Apache 2.2 # Order Deny,Allow # Deny from All # Allow from 127.0.0.1 # Allow from ::1 # </IfModule> #</Directory>
添加以下行:
<Directory /usr/share/phpMyAdmin Options none AllowOverride Limit Require all granted </Directory>
保存并关闭文件。
然后,编辑"config.inc.php"文件:
vi /etc/phpMyAdmin/config.inc.php
精细的行,并将"cookie"单词更改为"http"。
$cfg['Servers'][$i]['auth_type'] = 'http'; //Authentication method (config, http or cookie based)?
保存并关闭文件。
使用命令重新启动Apache服务:
systemctl restart httpd
现在,我们可以从网络中的任何远程客户端访问PHPMyAdmin。
要访问它,请键入:http://ip_address/phpmyadmin /在浏览器的地址列中。
输入MySQL或者MariaDB用户名和密码:
从现在开始,我们可以从PHPMyAdmin仪表板上创建,删除和管理数据库。