当前位置: > CentOS > CentOS教程 >

Centos7.4环境安装lamp-php7.0教程

时间:2019-04-18 16:39来源:linux.it.net.cn 作者:IT

本文实例讲述了Centos7.4环境安装lamp-php7.0的方法。分享给大家供大家参考,具体如下:

一. 环境准备

桥接模式

能访问外网

 
1
#ping www.baidu.com

ping得通则能到外网

关闭防火墙

 
1
2
#systemctl disable firewalld //禁用防火墙
#systemctl stop firewalld    //关闭防火墙

关闭seLinux

 
1
#vim /etc/selinux/config

改为:

 
1
SELINUX=disabled

配置yum源

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# cd /etc/yum.repos.d/
# mkdir repo_bak      //创建备份源文件夹
# mv *.repo repo_bak/    //将源文件都放到备份文件夹
# wget http://mirrors.aliyun.com/repo/Centos-7.repo     //配置阿里源
# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo //配置网易源
# ls        //列出/etc/yum.repos.d/目录下的文件
Centos-7.repo CentOS-Base-163.repo repo.bak
# yum clean all         //清除系统所有yum缓存
# yum makecache         //生成yum缓存
# yum list | grep epel-release     //查看epel源列表
# yum install -y epel-release     //安装epel源
# ls         //epel源安装成功,比原来多了一个epel.repo和epel-testing.repo文件
Centos-7.repo CentOS-Base-163.repo epel.repo epel-testing.repo repo.bak
# wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo         //下载阿里源的epel源
# ls
CentOS7-Base-163.repo Centos-7.repo epel-7.repo epel.repo epel-testing.repo repo_bak
# yum clean all        //再次清除系统yum缓存
# yum makecache        //重新生成新的yum缓存
# yum repolist enabled        //查看系统可用的yum源
# yum repolist all            //查看所有yum源

删除旧版本

 
1
# yum -y remove php

二. Apache

1. 安装Apache

 
1
# yum install httpd


2. 配置Apache

 
1
2
3
4
5
6
7
8
9
10
# vim /etc/httpd/conf/httpd.conf
//搜索
# AddType application/x-gzip .gz .tgz
//然后在后面添加
# AddType application/x-httpd-php .php
//搜索
# IfModule dir_module
//在DirectoryIndex下面添加  index.php
//修改#ServerName www.example.com:80
# ServerName localhost:80

修改默认Web站点目录

找到: DocumentRoot "/var/www/html"
修改为: DocumentRoot "/var/www"

找到:<Directory "/var/www/html">
修改为:<Directory "/var/www">

3. 启动Apache服务

 
1
# systemctl start httpd

4. 查看httpd状态

 
1
systemctl status httpd

三. PHP 7.0.7

1. 下载好对应版本的centos的PHP源码包

 
1
php-7.0.7.tar.gz

2. 解压源码包

 
1
# tar zxvf php-7.0.7.tar.gz

3. 进入解压包编译安装php所需要的依赖包

 
1
2
3
4
5
# yum install -y gcc gcc-c++ make automake autoconf gd file bison patch mlocate flex diffutils zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers openldap-devellibxslt-devel kernel-devel libtool-libs readline-devel gettext-devel libcap-devel php-mcrypt libmcrypt libmcrypt-devel recode-devel gmp-devel icu libxslt libxslt-devel php-devel
# yum -y install libxslt-devel*
# yum -y install perl*
# yum -y install httpd-devel
# find / -name apxs //得到的路径是:/usr/bin/apxs 编译参数的时候用到,是配置Apache支持PHP的关键参数

4. 编译参数

./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-xmlreader \
--enable-xmlwriter \
--enable-soap \
--enable-calendar \
--with-curl \
--with-zlib \
--with-gd \
--with-pdo-sqlite \
--with-pdo-mysql \
--with-mysqli \
--with-mysql-sock \
--enable-mysqlnd \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--enable-exif \
--enable-bcmath \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-openssl \
--enable-ftp \
--with-kerberos \
--with-gettext \
--with-xmlrpc \
--with-xsl \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-fpm-systemd \
--with-apsx2 \
--disable-fileinfo

6. 编译并安装

 
1
# make && make install


7. 安装完查看PHP版本

1
# /usr/local/php/bin/php -v


 

8. 编辑 /etc/profil 添加环境变量

 
1
# vim /etc/profil

添加进去

?
1
2
PATH=$PATH:/usr/local/php/bin
export PATH

9. 更新环境变量

 
1
# source /etc/profile

10. 查看环境变量

 
1
# echo $PATH

11. 查看PHP版本

 
1
# php -v

12. 配置PHP配置文件php-fpm

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# cp php.ini-production /etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# vim /usr/local/php/etc/php-fpm.conf
# 配置错误日志:
#
# error_log = /usr/local/php/var/php-fpm.log
#
# 配置pid文件:
#
# pid = /usr/local/php/var/run/php-fpm.pid
#
# 保存退出
# cd /usr/local/src/php-7.2.4
# cp ./sapi/fpm/php-fpm.service 到 /usr/lib/systemd/system/下

13. 启动php-fpm

配置开机启动php-fpm:

 
1
# systemctl enable php-fpm

启动php-fpm

 
1
# systemctl start php-fpm

查看状态

 
1
2
3
4
5
6
7
8
9
# systemctl status php-fpm
● php-fpm.service – The PHP FastCGI Process Manager
Loaded: error (Reason: Invalid argument)
Active: inactive (dead)
Dec 13 14:37:27 localhost.localdomain systemd[1]: [/usr/lib/systemd/system/php-fpm.service:7] Not an absolute path, ignoring: ${prefix}/va…fpm.pid
Dec 13 14:37:27 localhost.localdomain systemd[1]: [/usr/lib/systemd/system/php-fpm.service:8] Executable path is not absolute, ignoring: $…pm.conf
Dec 13 14:37:27 localhost.localdomain systemd[1]: php-fpm.service lacks both ExecStart= and ExecStop= setting. Refusing.
Dec 13 14:37:27 localhost.localdomain systemd[1]: Cannot add dependency job for unit php-fpm.service, ignoring: Unit is not loaded properl…gument.
Hint: Some lines were ellipsized, use -l to show in full.

14. 测试php-fpm的配置

 
1
2
3
4
5
6
7
8
9
10
#测试php-fpm配置
/usr/local/php/sbin/php-fpm -t
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf -t
#启动php-fpm
/usr/local/php/sbin/php-fpm
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf
#关闭php-fpm
kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
#重启php-fpm
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

15. 解决办法

 
1
# vim /usr/lib/systemd/system/php-fpm.service

将其中的${prefix}和${exec_prefix}改成/usr/local/php然后保存

 
1
2
//重新启动系统
# reboot

16. 重启Apache和php-fpm

 
1
2
# systcl restart httpd
# systcl restart php-fpm

17. 安装 对应的xdebug

#复制phpinfo.php整个网页内容,粘贴到https://xdebug.org/wizard.php 确定Xdebug版本

根据提示文档进行操作安装

18. yum安装PHPMyAdmin

?
1
# yum install phpmyadmin

修改配置文件

?
1
# vi /etc/httpd/conf.d/phpMyAdmin.conf
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<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  #注释掉
   Require all granted  #新添加
   </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/setup/>
  <IfModule mod_authz_core.c>
   # Apache 2.4
   <RequireAny>
   #Require ip 127.0.0.1 #注释掉
   #Require ip ::1  #注释掉
   Require all granted  #新添加
   </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>

修改phpmyadmin配置文件

 
1
2
3
4
5
# vi /etc/phpMyAdmin/config.inc.php
//修改如下内容
# $cfg['Servers'][$i]['host'] = '127.0.0.1';
# $cfg['Servers'][$i]['user'] = 'root';
# $cfg['Servers'][$i]['password'] = '123456';

重启Apache

 
1
# systemctl restart httpd

安装完MySQL后测试

 
 
# 浏览器地址栏输入: 服务器ip/phpMyAdmin

四. 安装MySQL 5.7

1. 安装源

 
 
# rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

2. yum安装MySQL

 
1
# yum install mysql-community-server

3. 启动MySQL

 
1
2
# systemctl start mysqld
# systemctl enable mysqld //开机自启

4. 在更改root默认密码前,可以根据需要,关闭密码策略。 如果没有关闭密码策略,则新密码要求是大小写字母+数字+字符的组合。

 
1
2
3
4
5
# vim /etc/my.cnf
[mysqld]
validate_password = off
//重启,使修改后的密码策略生效
# systemctl restart mysqld

5. 修改MySQL的root默认密码

 
1
2
3
4
//查看默认密码
# grep 'temporary password' /var/log/mysqld.log
//默认密码是: )U4FB:Kw!evF
# 2018-12-18 A temporary password is generated for root@localhost: )U4FB:Kw!evF

运行安全设置向导,并修改默认密码

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql_secure_installation
//输入root默认密码
Enter password for user root: ***
//设置新密码
New password:***
Re-enter new password: ***
//是否更改root密码
Change the password for root ? ((Press y|Y for Yes, any other key for No) ://直接回车,表示No
//是否删除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
//禁止root远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
//删除test数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
//重新加载权限表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y



 
1
2
3
4
//也可以使用mysql直接更改root密码
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
exit;



 
1
2
3
4
5
//为安全root仅限本地登录,根据需要可以新建一个有管理员权限的远程用户
mysql -uroot -p
GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%' IDENTIFIED BY '登录密码' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit;

安装完成



(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容