新手在linux下搭建PHP环境,并且运行
一、准备:
1,装有linux的电脑
注意:以下安装都是root权限
二、安装Apache
1.安装yum -y install httpd
2.开启apache服务systemctl start httpd.service
3.设置apache服务开机启动systemctl enable httpd.service
4.验证apache服务是否安装成功
在本机浏览器中输入虚拟机的ip地址,如果看到apache默认的页面--有Testing 123...字样,便是成功安装了apache服务了。
CentOS7查看ip地址的方式为:ip addr
访问应该是失败的,原因如下:
查了资料,说法是,CentOS7用的是Firewall-cmd,CentOS7之前用的是iptables防火墙;要想让外网能访问到apache主目录,就需要做以下的操作:firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
然后再访问外网ip,就可以了。
三、安装PHP
1.安装yum -y install php
2.重启apache服务systemctl restart httpd
或者systemctl restart httpd.service
然后,你可以写一个php文件在浏览器中运行一下了;
eg:vi /var/www/html/info.php
i<?php phpinfo(); ?>
Esc:wq
然后,在自己电脑浏览器输入 10.17.1.223/info.php
运行,会出现php的一些信息
四、安装MySQL
安装MariaDB
1.安装yum install mariadb mariadb-server mariadb-libs mariadb-devel
2.开启MySQL服务systemctl start mariadb.service
3.设置开机启动MySQL服务systemctl enable mariadb.service
4.数据库安全设置
[root@localhost ~]# 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):
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:
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] 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] n
... skipping.
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!
5.登陆数据库测试
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.02 sec)
MariaDB [(none)]>
五、将PHP和MySQL关联起来yum -y install php-mysql
六、安装常用的PHP模块
例如,GD库,curl,mbstring,...
1.安装:yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel
2.重启apache服务systemctl restart httpd.service
然后,再次在浏览器中运行info.php,你会看到安装的模块的信息;
至此,LAMP环境就搭建好了。