Linux下PHP开发环境搭建(Apache2.4+PHP7.1+MySQL8.0)

一、获取安装包

  • PHP下载地址:http://cn.php.net/distributions/php-7.2.6.tar.gz
  • Apache下载地址:http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.33.tar.gz
  • MySQL下载地址: https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz

二、安装Apache

    1. 依赖包安装

1) 安装编译器gcc、gcc-c++

yum install -y gcc gcc-c++

2) 安装依赖包expat-devel、zlib-devel、openssl-devel

yum install -y expat-devel zlib-devel openssl-devel

2) 安装依赖包apr

注意:如果依赖包不存在,则去网站找最新的包,改成相应版本号下载即可

wget http://mirror.bit.edu.cn/apache//apr/apr-1.6.3.tar.gz
tar zxvf apr-1.6.2.tar.gz
cd apr-1.6.2
./configure --prefix=/usr/local/apr
make && make install

3) 安装依赖包apr-util

wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
tar zxvf apr-util-1.6.0.tar.gz
cd apr-util-1.6.0
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install

4) 安装依赖包pcre

wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
tar zxvf pcre-8.41.tar.gz
cd pcre-8.41
./configure --prefix=/usr/local/pcre
make && make install

注意:将apr、apr-util安装包拷贝到Apache安装包的srclib目录中

           名称分别命名为apr、apr-util,不要后面的版本号

    2. 安装过程

1) 解压Apache安装包

tar zxvf httpd-2.4.33.tar.gz

 注意:       将apr、apr-util安装包拷贝到Apache安装包的srclib目录中

        名称分别命名为apr、apr-util,不要后面的版本号

2) 编译、安装

cd httpd-2.4.28
./configure --prefix=/usr/local/server/apache \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-pcre=/usr/local/pcre \--with-mysql=shared,mysqlnd                                                                                    
--enable-so \
--enable-ssl \
--enable-deflate \
--enable-rewrite \
--enable-headers \
--enable-expires \
--disable-cgid\
--disable-cgi

 

    3. 修改配置文件httpd.conf

vim /usr/local/server/apache/conf/httpd.conf

去掉ServerName前面的 #

并将ServerName后面的网址改为localhost:80

    4. 将httpd加入系统服务并设置开机自启

1) 将httpd加入系统服务

cp /usr/local/server/apache/bin/apachectl /etc/init.d/httpd

2) 修改/etc/init.d/httpd,在第3行加入以下内容

# chkconfig: 345 85 15
# description: Activates/Deactivates Apache Web Server

注意: 代码中的 # 不可以去掉

3) 设置系统服务开机自启

systemctl enable httpd

4) 启动Apache

service httpd start

 

三、安装MySQL

    1. 安装前准备

1) 解压安装包

tar zxvf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz 
mv mysql-8.0.11-linux-glibc2.12-x86_64 /usr/local/server/mysql

2) 创建用户和用户组并分配相应的权限

groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql .   #注意后面有个点

 

    2. mysql的初始化并做基本配置

1) 初始化mysql

cd /usr/local/server/mysql
bin/mysqld \
--initialize \
--user=mysql \
--basedir=/usr/local/server/mysql \
--datadir=/usr/local/server/mysql/data \

注意 : 如果报错 , 则缺哪个库yum安装即可                                                                                                                             bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

yum install libaio                                                                            

2) 配置mysql

vim my.cnf  # 创建配置文件

本示例仅保证mysql可以正常运行,更多配置请参考官方文档说明

[mysqld]
skip-grant-tables
basedir = /usr/local/server/mysql
datadir = /usr/local/server/mysql/data
port = 3306

将配置文件软链接到 /etc/ 目录

rm -rf /etc/my.cnfln -s /usr/local/server/mysql/my.cnf /etc/my.cnf

    3. 启动mysql并设置root用户密码

1) 启动mysql

support-files/mysql.server start  # 启动MySQL
bin/mysql -uroot -p  # 这里直接回车,无须输入密码

2) 重置root用户密码

use mysql;
update user set authentication_string='' where user='root';
exit;

3 )  删除/etc/my.cnf文件的 skip-grant-tables , 重启mysql服务

  support-files/mysql.server restart  #重启mysql

4 )  用root用户进行登录

  mysql -u root -p  
  passwrod:直接回车

5 )  使用ALTER修改root用户密码,初始化完毕 。退出,使用新密码登录

  ALTER user 'root'@'localhost' IDENTIFIED BY 'new_password';

    4. 设置环境变量和开机自启

1) 设置环境变量

编辑profile文件

vim /etc/profile

添加下列信息到profile尾部

export PATH=$PATH:/usr/local/server/mysql/bin

使环境变量立即生效

source /etc/profile

2) 设置开机自启

cp support-files/mysql.server /etc/init.d/mysqld  
systemctl enable mysqld 

 

    5. 防火墙设置

CentOS默认开启了 firewall 防火墙,下面我们使用firewall开启3306l端口

1) 开启之前我们先查询下3306端口是否开启

firewall-cmd --query-port=3306/tcp

2) 我们可以选择临时开启或者永久开启3306端口

firewall-cmd --add-port=3306/tcp  # 临时开启3306端口  
firewall-cmd --permanent --zone=public --add-port=3306/tcp  # 永久开启3306端口

3) 重启firewall

firewall-cmd --reload

 

    6. 远程访问

1) 给予任何主机访问mysql的权限

create user root@'%' identified by 'your_password';                                                           
grant all privileges on *.* to root@'%';

2) 使权限修改生效

FLUSH PRIVILEGES;

四、安装PHP

    1. 安装步骤

1) 安装依赖包

yum -y install wget vim pcre pcre-devel openssl openssl-devel \
libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng \
libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib \
zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel \
curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel \
nss_ldap jemalloc-devel cmake boost-devel bison automake libevent \
libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt \
mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel \
libcurl libcurl-devel openjpeg-devel libcurl.x86_64 libcurl-devel.x86_64 \
libjpeg-turbo libjpeg-turbo-devel libpng freetype  libpng-devel \
freetype-devel icu libicu libicu-devel  openldap openldap-clients \
openldap-devel openldap-servers
cp -frp /usr/lib64/libldap* /usr/lib/

2) 解压PHP安装包

tar zxvf php-7.2.6.tar.gz

3) 编译安装

cd php-7.2.6
./configure --prefix=/usr/local/server/php \
--with-apxs2=/usr/local/server/apache/bin/apxs \
--with-config-file-path=/usr/local/server/php \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--with-libmbfl \
--enable-ftp \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm \make && make install

 

    2. 配置php.ini

1) 将配置文件拷贝到PHP安装目录

cp php.ini-* /usr/local/server/php/

2) 生成php.ini

cp php.ini-development /usr/local/server/php/php.inicp /usr/local/server/php/etc/php-fpm.conf.default /usr/local/server/php/etc/php-fpm.conf

3 ) 修改php.ini配置文件

expose_php = Off
short_open_tag = ON
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 32M
date.timezone = Asia/Shanghai
mbstring.func_overload=2
extension_dir = "/usr/local/server/php/lib/php/extensions/no-debug-zts-20170718/"



    3. 修改httpd.conf

载入PHP模块,如httpd.conf中有下列代码则直接去掉前面#即可,没有则加入

LoadModule php7_module modules/libphp7.so

在底部加入以下代码使得Apache可以解析php文件


	AddType application/x-httpd-php .php

找到如下代码,在index.html后面加入index.php


    DirectoryIndex index.html

重启Apache

service httpd restart

 

    4. 测试PHP是否成功安装

创建/usr/local/server/apache/htdocs/index.php

vim /usr/local/server/apache/htdocs/index.php

在index.php中编写以下代码



   phpinfo();

?>

如果出现以下页面则安装成功

MYSQL8.0安装后 phpMyAdmin无法登陆解决

MYSQL8.0的密码验证方式从mysql_native_password改为了caching_sha2_password

vim my.cnf

default_authentication_plugin=mysql_native_password  

进入mysql修改一下密码和加密插件

use mysql;  
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';    
FLUSH PRIVILEGES; 


你可能感兴趣的:(Linux下PHP开发环境搭建(Apache2.4+PHP7.1+MySQL8.0))