centos7安装php环境的方法:首先用yum快速搭建lamp平台;然后安装php,并将php与mysql关联起来;接着安装常用php模块,并创建“phpinfo();”用于测试php;最后重启apache服务器即可。
centos7下面安装php环境
用yum快速搭建lamp平台
实验环境:
[root@nmserver-7 html]# cat /etc/redhat-release centos release 7.3.1611 (altarch) [root@nmserver-7 html]# uname -alinux nmserver-7.test.com 3.10.0-514.el7.centos.plus.i686 #1 smp wed jan 25 12:55:04 utc 2017 i686 i686 i386 gnu/linux
1、安装apache
1.1 安装apache
[root@nmserver-7 ~]# yum install httpd httpd-devel
1.2 启动apache服务
[root@nmserver-7 ~]# systemctl start httpd
1.3 设置httpd服务开机启动
[root@nmserver-7 ~]# systemctl enable httpdcreated symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
1.4 查看服务状态
[root@nmserver-7 ~]# systemctl status httpd● httpd.service - the apache http server loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) active: active (running) since 五 2017-07-21 17:21:37 cst; 6min ago docs: man:httpd(8) man:apachectl(8) main pid: 2449 (httpd) status: "total requests: 11; current requests/sec: 0; current traffic: 0 b/sec" cgroup: /system.slice/httpd.service ├─2449 /usr/sbin/httpd -dforeground ├─2450 /usr/sbin/httpd -dforeground ├─2451 /usr/sbin/httpd -dforeground ├─2452 /usr/sbin/httpd -dforeground ├─2453 /usr/sbin/httpd -dforeground ├─2454 /usr/sbin/httpd -dforeground ├─2493 /usr/sbin/httpd -dforeground ├─2494 /usr/sbin/httpd -dforeground └─2495 /usr/sbin/httpd -dforeground7月 21 17:21:35 nmserver-7.test.com systemd[1]: starting the apache http server...7月 21 17:21:36 nmserver-7.test.com httpd[2449]: ah00558: httpd: could not reliably determine the server's fully q...ssage7月 21 17:21:37 nmserver-7.test.com systemd[1]: started the apache http server.hint: some lines were ellipsized, use -l to show in full.
1.5 防火墙设置开启80端口[root@nmserver-7 ~]# firewall-cmd --permanent --zone=public --add-service=httpsuccess[root@nmserver-7 ~]# firewall-cmd --permanent --zone=public --add-service=httpssuccess[root@nmserver-7 ~]# firewall-cmd --reloadsuccess
1.6确认80端口监听中
[root@nmserver-7 ~]# netstat -tulpactive internet connections (only servers)proto recv-q send-q local address foreign address state pid/program name tcp 0 0 0.0.0.0:ssh 0.0.0.0:* listen 1084/sshd tcp 0 0 localhost:smtp 0.0.0.0:* listen 1486/master tcp6 0 0 [::]:ssh [::]:* listen 1084/sshd tcp6 0 0 localhost:smtp [::]:* listen 1486/master tcp6 0 0 [::]:http [::]:* listen 2449/httpd udp 0 0 localhost:323 0.0.0.0:* 592/chronyd udp6 0 0 localhost:323 [::]:* 592/chronyd
1.8 查服务器ip
[root@nmserver-7 ~]# ip addr1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state unknown qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: ens33: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000 link/ether 00:0c:29:56:bc:cf brd ff:ff:ff:ff:ff:ff inet 192.168.8.9/24 brd 192.168.8.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe56:bccf/64 scope link valid_lft forever preferred_lft forever3: bridge0: <no-carrier,broadcast,multicast,up> mtu 1500 qdisc noqueue state down qlen 1000 link/ether ea:89:d5:c7:32:73 brd ff:ff:ff:ff:ff:ff
1.9 浏览器登陆
如果浏览器访问不了,配置阿里云控制台的安全组80
2、安装mysql
2.1安装mysql
[root@nmserver-7 ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel root@nmserver-7 ~]# rpm -qa |grep mariamariadb-libs-5.5.52-1.el7.i686mariadb-5.5.52-1.el7.i686mariadb-server-5.5.52-1.el7.i686mariadb-devel-5.5.52-1.el7.i686
2.2 开启mysql服务,并设置开机启动,检查mysql状态
[root@nmserver-7 ~]# systemctl start mariadb [root@nmserver-7 ~]# systemctl enable mariadb created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.[root@nmserver-7 ~]# systemctl status mariadb ● mariadb.service - mariadb database server loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled) active: active (running) since 六 2017-07-22 21:19:20 cst; 21s ago main pid: 9603 (mysqld_safe) cgroup: /system.slice/mariadb.service ├─9603 /bin/sh /usr/bin/mysqld_safe --basedir=/usr └─9760 /usr/libexec/mysqld --basedir=/usr --datadir=/v...7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...7月 22 21:19:16 nmserver-7.test.com mysqld_safe[9603]: 170722 21...7月 22 21:19:16 nmserver-7.test.com mysqld_safe[9603]: 170722 21...7月 22 21:19:20 nmserver-7.test.com systemd[1]: started mariadb ...
[root@nmserver-7 ~]# netstat -tulpactive internet connections (only servers)proto recv-q send-q local address foreign address state pid/program name tcp 0 0 0.0.0.0:ssh 0.0.0.0:* listen 1084/sshd tcp 0 0 0.0.0.0:mysql 0.0.0.0:* listen 9760/mysqld tcp6 0 0 [::]:ssh [::]:* listen 1084/sshd tcp6 0 0 [::]:http [::]:* listen 2449/httpd udp 0 0 localhost:323 0.0.0.0:* 592/chronyd udp6 0 0 localhost:323 [::]:* 592/chronyd
2.3 数据库安全设置
[root@nmserver-7 ~]# 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 currentpassword for the root user. if you've just installed mariadb, andyou 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 mariadbroot user without the proper authorisation.set root password? [y/n] ynew password: re-enter new password: password updated successfully!reloading privilege tables.. ... success!by default, a mariadb installation has an anonymous user, allowing anyoneto log into mariadb without having to have a user account created forthem. this is intended only for testing, and to make the installationgo a bit smoother. you should remove them before moving into aproduction environment.remove anonymous users? [y/n] y ... success!normally, root should only be allowed to connect from 'localhost'. thisensures 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 canaccess. this is also intended only for testing, and should be removedbefore 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 farwill 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 mariadbinstallation should now be secure.thanks for using mariadb!
2.4 登陆数据库测试
[root@nmserver-7 ~]# mysql -uroot -penter password: welcome to the mariadb monitor. commands end with ; or \g.your mariadb connection id is 11server version: 5.5.52-mariadb mariadb servercopyright (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)]>
3、安装php
3.1 安装php
[root@nmserver-7 ~]# yum -y install php[root@nmserver-7 ~]# rpm -ql php/etc/httpd/conf.d/php.conf/etc/httpd/conf.modules.d/10-php.conf/usr/lib/httpd/modules/libphp5.so/usr/share/httpd/icons/php.gif/var/lib/php/session
3.2 将php与mysql关联起来
[root@nmserver-7 ~]# yum install php-mysql[root@nmserver-7 ~]# rpm -ql php-mysql/etc/php.d/mysql.ini/etc/php.d/mysqli.ini/etc/php.d/pdo_mysql.ini/usr/lib/php/modules/mysql.so/usr/lib/php/modules/mysqli.so/usr/lib/php/modules/pdo_mysql.so
3.3 安装常用php模块
[root@nmserver-7 ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath
3.4 测试php
[root@nmserver-7 ~]# cd /var/www/html/[root@nmserver-7 html]# ls[root@nmserver-7 html]# pwd/var/www/html[root@nmserver-7 html]# vi info.php<?php phpinfo();?>~ ~ ~ ~ ~ ~ ~ ~ :wq
3.5重启apache服务器
[root@nmserver-7 html]# systemctl restart http
3.6测试php
在自己电脑浏览器输入 192.168.8.9/info.php,你可以看到已经安装的模块;
更多相关知识,请访问!
以上就是centos7如何安装php环境的详细内容。