准备工作
yum install -y yum-utils
package-cleanup --cleandupes
yum update -y
yum install -y epel-release
# 如果找不到epel-release包,则进行下面两步操作
# wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# rpm -ivh epel-release-latest-7.noarch.rpm
开始安装
- 安装nginx
yum install -y nginx
# 配置开机启动
systemctl enable nginx.service
- 防火墙开启80端口
yum install -y firewalld firewall-config
systemctl start firewalld.service
systemctl enable firewalld.service
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https #如果用不到https服务,此步跳过
firewall-cmd --permanent --zone=trusted --add-port=80/tcp
firewall-cmd --permanent --zone=trusted --add-port=443/tcp #如果用不到443端口,此步跳过
firewall-cmd --reload
firewall-cmd --permanent --zone=trusted --list-port
firewall-cmd --zone=public --permanent --add-service=mysql #需要远程连接mysql时再进行这步操作
- 安装必备的依赖
yum install -y \
gcc-c++ autoconf make \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel unzip \
zip libzip-devel \
glibc glibc-devel \
glib2 glib2-devel \
bzip2 bzip2-devel \
curl curl-devel libcurl-devel \
ncurses openssl-devel \
gdbm-devel db4-devel libXpm-devel \
libX11-devel gd-devel gmp-devel \
readline-devel libxslt-devel \
expat-devel xmlrpc-c xmlrpc-c-devel \
libicu-devel libmcrypt-devel \
libmemcached-devel \
oniguruma oniguruma-devel \
sqlite-devel \
wget
yum install -y http://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.5+rev1-2.el7.remi.x86_64.rpm
yum install -y http://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.5+rev1-2.el7.remi.x86_64.rpm
- 编译安装 GD 库
wget https://github.com/libgd/libgd/releases/download/gd-2.3.3/libgd-2.3.3.tar.gz
tar -xf libgd-2.3.3.tar.gz
cd libgd-*
./configure --bindir=/usr/sbin/ \
--sbindir=/usr/sbin/ \
--libexecdir=/usr/libexec \
--sysconfdir=/etc/ \
--localstatedir=/var \
--libdir=/usr/lib64/ \
--includedir=/usr/include/ \
--datarootdir=/usr/share \
--infodir=/usr/share/info \
--localedir=/usr/share/locale \
--mandir=/usr/share/man/ \
--docdir=/usr/share/doc/libgd
make
make install
- 编译安装 freetype
cd /usr/src/
wget https://download.savannah.gnu.org/releases/freetype/freetype-2.10.0.tar.bz2 -O freetype.tar.bz2
tar -xjf freetype.tar.bz2
cd freetype-*
./configure --prefix=/usr/local/freetype
make && make install
php7.3 环境需要重新按照 libzip 至最新版,详见《php7.3 环境重新安装libzip》
- 下载php7
官方下载地址 http://php.net/downloads.php
cd /usr/src/
wget https://www.php.net/distributions/php-7.4.28.tar.bz2 -O php7.bz2
tar -xjf php7.bz2
mv php-* php7
cd /usr/src/php7
#注意,因为php7.1以后[]不支持字符串操作,凡是这种形式的$arr[]=(string)$someString; 的写法
#在php7.1版本都以后会报"[] operator not supported for strings" 的错误
- 编译php
# 查看可配置的编译参数
./configure --help
# 视情况增删参数,以下为一些常用的
./configure --prefix=/usr/local/php7 \
--enable-gd \
--enable-fpm \
--enable-dom \
--enable-pdo \
--enable-json \
--enable-exif \
--enable-intl \
--enable-soap \
--enable-pcntl \
--enable-bcmath \
--enable-filter \
--enable-session \
--enable-sockets \
--enable-calendar \
--enable-mbstring \
--enable-simplexml \
--disable-fileinfo \
--with-bz2 \
--with-cdb \
--with-xpm \
--with-jpeg \
--with-pear \
--with-curl \
--with-pear \
--with-zlib \
--with-mhash \
--with-iconv \
--with-mysqli \
--with-openssl \
--with-pdo-mysql \
--with-mysql-sock \
--with-openssl-dir \
--with-external-gd \
--with-freetype=/usr/local/freetype
- 安装php
make
make install
- 测试是否安装成功
/usr/local/php7/bin/php -v
- 做软链,以便直接用php运行
ln -sf /usr/local/php7/bin/php /usr/local/bin/php
ln -sf /usr/local/php7/bin/php-config /usr/bin/php-config
- 验证
php -v
- 复制配置文件
cp /usr/src/php7/php.ini-development /usr/local/php7/lib/php.ini
- 配置php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
cp /usr/src/php7/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
- 启动php-fpm
service php-fpm start
# 配置开机启动
chkconfig php-fpm on
相关配置
配置nginx
- 修改nginx.conf文件
vi /etc/nginx/nginx.conf
注意:server 中需添加root 配置,否则$document_root无效,下面的是/etc/nginx/nginx.conf示例配置
# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
worker_rlimit_nofile 65535;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 20m;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
- 重启nginx
service nginx restart
- 在web目录下新建一个php文件
echo '<?php phpinfo();' > /usr/share/nginx/html/info.php
- 浏览器访问验证
> http://127.0.0.1/info.php
nginx虚拟机配置
以 www.test.com 为例
# 编辑虚拟机配置文件 vhosts若不存在则需创建
mkdir /etc/nginx/vhosts
vi /etc/nginx/vhosts/test.com.conf
输入以下内容
server {
listen 80;
server_name www.test.com; # 网站域名
root "/usr/share/nginx/html/test"; #网站路径
location / {
index index.html index.htm index.php;
if (!-e $request_filename) { #地址重写规则,可在地址栏中隐藏index.php
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
#autoindex on; # 自动索引
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
安装php7扩展
安装mysql
常见问题
解决SELinux启用带来的资源无法访问的问题。问题原因:SELiux阻止了nginx用户访问nginx非授权目录。
#查看问题报告
yum install setroubleshoot
sealert -a /var/log/audit/audit.log > /var/log/audit/audit.format.log
#添加nginx的访问web目录权限
ausearch -c 'nginx' --raw | audit2allow -M my-nginx
semodule -i my-nginx.pp
永久关系SELinux
vim /etc/selinux/config
#更改SELINUX=disable
如有其它问题,请在下方留言^_^
按你的这种安装方式是不是只支持PDO连接MySQL数据库了?
是 php7 不支持mysql扩展了,可以用mysqli
PHP如何关联MySQL模块?
在./configure编译时 ,有这样两个参数 --with-mysql-sock 和--with-mysqli ,就是关联mysql模块的