CentOS 7.8 默认的nginx1.16.1和php5.4版本较老,有安全漏洞,本文介绍如何升级到nginx1.22和php7.3、并同时卸载yum的安装。
本文也同样适用于从头编译安装nginx和php。
1)装备工作 ----------------------------------------------------------
1.1)安装所有依赖 :
yum install gcc autoconf gcc-c++ libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-deve pcre-devel
1.2)升级 libzip(必须)
yum remove libzip libzip-dev
wget http://www.libzip.org/libzip-1.3.2.tar.gz
tar zxvf libzip-1.3.2.tar.gz
./configure&&make&&make install
1.3)#卸载原先的 php
yum remove php
1.4) nginx也可以不卸载,先停止掉service nginx stop。因为卸载,要卸一堆的相关的包,那些包留在那里,也没啥问题。
如果真要,则: service nginx stop&&yum remove nginx。
2)编译nginx ------------------------------------------------------------------------
2.1)下载nginx源码
wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar zxvf nginx-1.22.0.tar.gz
2.2)编译
groupadd nginx&&useradd -g nginx -s /sbin/nologin -M nginx
configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/nginx.pid --lock-path=/usr/local/nginx/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi --with-file-aio --with-http_realip_module --with-pcre
#编译,-j为--job,开启8个工作任务
make -j 8&&make install
----------------------------------------------------------
2.3) vim /usr/local/nginx/conf/nginx.conf 修改默认配置
2.3.1)打开行开头,用nginx用户启动
user nginx;
2.3.2)打开error.log行注释
error_log /usr/local/nginx/logs/error.log;
2.3.3)注释掉nginx的access_log行后,重启nginx是报了如下错误:
nginx: [emerg] unknown log format "main" in /usr/local/nginx/conf/nginx.conf
其原因很简单,是因为log_format没有开启的原因,在http{}中找到:
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
将每行前面的#去掉后在重启就可以了
2.3.4)----打开gzip
#是否启动gzip压缩,on代表启动,off代表开启
gzip on;
#需要压缩的常见静态资源
gzip_types text/plain application/javascript application/json application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
#由于nginx的压缩发生在浏览器端而微软的ie6很坑爹,会导致压缩后图片看不见所以该选项是禁止ie6发生压缩
gzip_disable "MSIE [1-6]\.";
#如果文件大于1k就启动压缩
gzip_min_length 1k;
#以16k为单位,按照原始数据的大小以4倍的方式申请内存空间,一般此项不要修改
gzip_buffers 4 16k;
#压缩的等级,数字选择范围是1-9,数字越小压缩的速度越快,消耗cpu就越大
gzip_comp_level 2;
#是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
tcp_nodelay on;
tcp_nopush on;
types_hash_max_size 2048;
2.3.5)让nginx侦听在所有网卡ip,修改原有的行,在后面附加: default_server
listen 80 default_server;
listen [::]:80 default_server;
2.4)设置 /usr/share/html/nginx/ 为主目录
这里似乎其它目录不好使,就用原先yum安装的 nginx主目录:/usr/share/html/nginx,在 http{ 段 后面添加如下,同时注释掉: location / 相关行
root /usr/share/html/nginx;
2.4)测试nginx能否启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ps -ef|grep nginx&&netstat -ano|grep :80|grep LISTEN
3.安装编译php7 ---------------------------------------------------------------
wget https://www.php.net/distributions/php-7.3.5.tar.gz
tar zxvf php-7.3.5.tar.gz
3.1)
./configure --prefix=/usr/local/php --with-mysqli --with-pdo-mysql --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-simplexml --enable-xml --disable-rpath --enable-bcmath --enable-soap --enable-zip --with-curl --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-mbstring --enable-sockets --with-gd --with-openssl --with-mhash --enable-opcache --disable-fileinfo enable-session --enable-ftp --enable-pdo --enable-tokenizer
make -j 8&make install
3.2)形成php.ini
cp src/php-7.3.5/php.ini-development /usr/local/php/etc/php.ini
修改 /usr/local/php/etc/php.ini里的时区行:
grep date.timezone /usr/local/php/etc/php.ini
vim /usr/local/php/etc/php.ini
date.timezone = Asia/Shanghai
3.3)php-fpm.conf.default复制一份新的php-fpm配置文件:
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
配置错误日志:直接打开注释 ;error_log=log/php-fpm.log
配置pid文件: 直接打开注释行 ;pid = run/php-fpm.pid
保存退出
3.3)配置www.conf
cd /usr/local/php/etc/php-fpm.d
cp www.conf.default www.conf
vim /usr/local/php/etc/php-fpm.d/www.conf
#把启动用户改为和nginx服务同一个启动用户(注意,这里不能使用root用户,否则会启动不起来)
user = nginx
group = nginx
3.4)管理php-fpm配置:
cp src/php-7.3.5/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
配置开机启动php-fpm:
systemctl enable php-fpm
启动php-fpm:
systemctl restart php-fpm
查看状态:
systemctl status php-fpm
测试fpm的9000端口:
netstat -anop|grep :9000&&ps -ef|grep php-fpm
3.5)添加php环境变量:
vim /etc/profile
在其它已有行,或者文件的末尾,追加:
export PATH=$PATH:'/usr/local/php/bin/'
保存退出。
source /etc/profile
测试:
php -v
3.5)将php-fpm加入到nginx.conf的server段{
server{
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)${
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?${
expires 15d;
# access_log off;
}
}
4.将nginx设置成服务:----------------------------------------------------------
此时php和nginx联动,都设置好,做最后的nginx设置.
在centos7中,服务都是使用systemdctl命令而不是service了,即使是使用service命令其实也是链接到systemdctl命令。
4.1)cd /lib/systemd/system/ #切换目录到关于系统进程的目录
mv nginx.service nginx.service.bak (备份其它已有通过yum安装的nginx服务器
vim nginx.service (输入内容:
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
4.2)#开启nginx开机自启服务,关闭对应disable
systemctl enable nginx
4.3)将新的nginx软连接到 /sbin/nginx(同时备份老的nginx)
mv /sbin/nginx /sbin/nginx.yum
ln -s /usr/local/nginx/sbin/nginx /sbin/nginx
5)重启动 nginx ----------------------------------------------------------
systemctl restart nginx
或者service nginx restart
查看状态:
systemctl status nginx&&ps aux|grep nginx&&netstat -ano|grep :80|grep LISTEN
6)测试nginx+php
5.1)vim /usr/share/html/nginx/index.php
<?php
phpinfo();
?>
5.1)wget -O php.html http://localhost/index.php
然后观察 php.html内容。
---- Below pic is only nothing with this blog.


本文详细介绍了如何在CentOS7.8上将默认的nginx1.16.1和php5.4升级到nginx1.22和php7.3,包括升级libzip,卸载旧版PHP,编译安装nginx和php,修改配置文件,设置开机启动服务等步骤,确保安全性和性能提升。

被折叠的 条评论
为什么被折叠?



