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

(编辑:jimmy 日期: 2024/12/27 浏览:2)

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

一. 环境准备

桥接模式

能访问外网

#ping www.baidu.com

ping得通则能到外网

关闭防火墙

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

关闭seLinux

#vim /etc/selinux/config

改为:

SELINUX=disabled

配置yum源

# 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源

删除旧版本

# yum -y remove php

二. Apache

1. 安装Apache

# yum install httpd

2. 配置Apache

# 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服务

# systemctl start httpd

4. 查看httpd状态

systemctl status httpd

三. PHP 7.0.7

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

php-7.0.7.tar.gz

2. 解压源码包

# tar zxvf php-7.0.7.tar.gz

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

# 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. 编译并安装

# make && make install

7. 安装完查看PHP版本

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

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

# vim /etc/profil

添加进去

PATH=$PATH:/usr/local/php/bin
export PATH

9. 更新环境变量

# source /etc/profile

10. 查看环境变量

# echo $PATH

11. 查看PHP版本

# php -v

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

# 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:

# systemctl enable php-fpm

启动php-fpm

# systemctl start php-fpm

查看状态

# 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的配置

#测试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. 解决办法

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

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

//重新启动系统
# reboot

16. 重启Apache和php-fpm

# systcl restart httpd
# systcl restart php-fpm

17. 安装 对应的xdebug

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

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

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

18. yum安装PHPMyAdmin

# yum install phpmyadmin

修改配置文件

# 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  #注释掉
   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配置文件

# 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

# 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

# yum install mysql-community-server

3. 启动MySQL

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

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

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

5. 修改MySQL的root默认密码

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

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

mysql_secure_installation
//输入root默认密码
Enter password for user root: ***
//设置新密码
New password:***
Re-enter new password: ***
//是否更改root密码
Change the password for root "htmlcode">
//也可以使用mysql直接更改root密码
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
exit;

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

安装完成

希望本文所述对大家CentOS服务器配置有所帮助。