一. 环境配置
1 gcc安装
yum install gcc gcc-c++
gcc -v
2 安装autoconf
yum install autoconf
3 libzip安装
wget http://packages.psychotic.ninja/7/plus/x86_64/RPMS//libzip-0.11.2-6.el7.psychotic.x86_64.rpm
wget http://packages.psychotic.ninja/7/plus/x86_64/RPMS//libzip-devel-0.11.2-6.el7.psychotic.x86_64.rpm
rpm -ivh ./libzip-0.11.2-6.el7.psychotic.x86_64.rpm
rpm -ivh ./libzip-devel-0.11.2-6.el7.psychotic.x86_64.rpm
=================================================
二 PHP安装
1 安装php需要的常用库
yum -y install zlib zlib-devel
yum install openssl
yum install openssl-devel
yum -y install libmcrypt-devel mhash-devel libxslt-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 e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel
yum install libsqlite3x-devel -y
yum install oniguruma-devel -y
yum -y install bison bison-devel
yum install https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/r/re2c-0.14.3-2.el7.x86_64.rpm
#测试re2c命令
re2c -v
2 安装PHP7.4
wget http://cn2.php.net/distributions/php-7.4.0.tar.gz
tar -zxvf php-7.4.0.tar.gz
cd php-7.4.0/
./configure --prefix=/server/php7.4 --with-config-file-path=/server/php7.4/etc --enable-fpm --with-mcrypt --enable-mbstring --enable-pdo --with-curl --disable-debug --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-zip --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysqli --enable-gd --with-jpeg-dir --with-freetype-dir --enable-calendar --with-openssl --enable-phpdbg --with-readline
注:旧版本为--with-gd (新版用--enable-gd)
make && make install
3 安装完成后进行配置
cp /server/php-7.4.0/php.ini-production /server/php7.4/etc/php.ini
cd /server/php7.4/etc
cp php-fpm.conf.default php-fpm.conf
使用vim命令对php-fpm.conf的内容进行如下修改
pid= /server/php7.4/var/run/php-fpm.pid
cd /server/php7.4/etc/php-fpm.d
cp www.conf.default www.conf
使用vim命令对www.conf的内容进行如下修改
user = www
group = www
listen = 127.0.0.1:9000
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
添加用户及用户组
groupadd -r www
useradd -r -g www www
启动
/server/php7.4/sbin/php-fpm
4 作为服务启动
cp init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
服务的重启或停止命令
service php-fpm restart | start|stop
检查启动进程
ps aux | grep php-fpm
5 启用opcache
从phpinfo中找到php.ini文件修改
zend_extension=opcache
;开启[opcache]
opcache.enable=1
;CLI环境下,PHP启用OPcache
opcache.enable_cli=0
;OPcache共享内存存储大小,单位MB
opcache.memory_consumption=128
;这个选项用于控制内存中最多可以缓存多少个PHP文件
opcache.max_accelerated_files=3000
;设置缓存的过期时间(单位是秒),为0的话每次都要检查(正式环境不建议设置太小)
opcache.revalidate_freq=3600
;从字面上理解就是“允许更快速关闭”。它的作用是在单个请求结束时提供一种更快速的机制来调用代码中的析构器,从而加快PHP的响应速度和PHP进程资源的回收速度,这样应用程序可以更快速地响应下一个请求。把它设置为1就可以使用这个机制了。
opcache.fast_shutdown=1
;如果启用(设置为1),OPcache会在opcache.revalidate_freq设置的秒数去检测文件的时间戳(timestamp)检查脚本是否更新。
如果这个选项被禁用(设置为0),opcache.revalidate_freq会被忽略
opcache.validate_timestamps=1
6 加入系统环境变量
使用vim命令打开/etc/profile文件,在文件最末尾加上如下代码
export PHP_HOME=/server/php7.4
export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin
保存修改后,使用source命令重新加载配置文件,命令如下
source /etc/profile
执行上述命令后,可使用
echo $PATH
7 安装PHP扩展流程:
(1) 找到phpize位置
#whereis phpize
/server/php7.4/bin/phpize
(2) 找到php-config位置
#find / -name php-config
/server/php7.4/bin/php-config
(3) 进入需要的扩展目录分别执行以下命令(需要用到上面的两个文件)
#cd zip-1.12.4
#/server/php7.4/bin/phpize
#./configure --with-php-config=/server/php7.4/bin/php-config
#make && make install
=================================================
三 安装Nginx
1 pcre安装(让nginx支持rewrite)
yum install pcre
yum install pcre-devel
yum install epel-release
2 正式安装nginx
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum info nginx
yum install nginx
3 启动Nginx并设置开机自动运行
systemctl start nginx
systemctl enable nginx
4 添加到服务
chkconfig --add nginx
5 服务启动命令
/usr/lib/systemd/system/nginx.service
6 修改nginx配置以支持PHP
地址:/usr/sbin/nginx -c /etc/nginx/nginx.conf
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "open_basedir=/www/web/";
fastcgi_param PHP_ADMIN_VALUE "session.save_path=/www/web/joyurl.com/runtime/tmp";
fastcgi_param PHP_VALUE "upload_tmp_dir=/www/web/joyurl.com/runtime/tmp";
include fastcgi_params;
}
#rewrite设置
location / {
if (!-e $request_filename) {
rewrite ^/static/(.*)$ /404.php last;
rewrite ^(.*)$ /index.php?p=$1 last;
}
}
=================================================