linux php環境搭建的方法:先取得相關安裝套件;然後安裝Apache以及mysql;接著修改設定檔「httpd.conf」;最後設定環境變數和開機自啟,並編譯安裝PHP即可。
一、取得安裝套件
二、安裝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-devel2) 安裝依賴套件apr
注意:如果依賴套件不存在,則去網站找最新的套件,改成相應版本號下載即可
wget http://mirror.bit.edu.cn/apache//apr/apr-1.6.3.tar.gz tar zxvf apr-1.6.2.tar.gzcd apr-1.6.2 ./configure --prefix=/usr/local/apr make && make install3) 安裝依賴套件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.gzcd apr-util-1.6.0 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr make && make install4) 安裝依賴套件pcre
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz tar zxvf pcre-8.41.tar.gzcd 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/httpd2) 修改/etc/init.d/httpd,在第3行加入以下内容
# chkconfig: 345 85 15# description: Activates/Deactivates Apache Web Server注意: 代码中的 # 不可以去掉
3) 设置系统服务开机自启
systemctl enable httpd4) 启动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/mysql2) 创建用户和用户组并分配相应的权限
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-tablesbasedir = /usr/local/server/mysqldatadir = /usr/local/server/mysql/dataport = 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 # 启动MySQLbin/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 #重启mysql4 ) 用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/profile2) 设置开机自启
cp support-files/mysql.server /etc/init.d/mysqld systemctl enable mysqld
5. 防火墙设置
CentOS默认开启了 firewall 防火墙,下面我们使用firewall开启3306l端口
1) 开启之前我们先查询下3306端口是否开启
firewall-cmd --query-port=3306/tcp2) 我们可以选择临时开启或者永久开启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-serverscp -frp /usr/lib64/libldap* /usr/lib/2) 解压PHP安装包
tar zxvf php-7.2.6.tar.gz3) 编译安装
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<br>
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/"<br>
<br>
3. 修改httpd.conf
载入PHP模块,如httpd.conf中有下列代码则直接去掉前面#即可,没有则加入
LoadModule php7_module modules/libphp7.so在底部加入以下代码使得Apache可以解析php文件
<IfModule mod_php7.c> AddType application/x-httpd-php .php</IfModule>找到如下代码,在index.html后面加入index.php
<IfModule dir_module> DirectoryIndex index.html</IfModule>重启Apache
service httpd restart
4. 测试PHP是否成功安装
创建/usr/local/server/apache/htdocs/index.php
vim /usr/local/server/apache/htdocs/index.php在index.php中编写以下代码
<?php phpinfo();?>如果出现以下页面则安装成功
MYSQL8.0的密码验证方式从mysql_native_password改为了caching_sha2_password<br>
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環境搭建教學的詳細內容。更多資訊請關注PHP中文網其他相關文章!