一次lnmp环境升级记录

集团内部要对所有服务器进行版本升级,因此代码要迁移,环境要搭建

环境变化

现役环境:

服务器版本 PHP Nginx Apache
centos 6.8 php5.6 nginx/1.12.0 Apache/2.2.15 (Unix)

升级后环境:

服务器版本 PHP Nginx Apache
centos 7.4 php5.6 nginx/1.15.10 Apache/2.4.6 (CentOS)

相关路径记录

1
2
3
4
5
6
7
8
9
10
11
12
nginx路径
/usr/local/nginx

php路径
/usr/local/php56/

php.ini和php-fpm.conf路径
/usr/local/php56/etc

Php扩展路径
/usr/local/php56/include/php/ext
/usr/local/php56/lib/php/extensions

安装记录

1. 安装nginx

1.检查并安装Nginx基础依赖包pcre-devel openssl-devel
名称中带有”devel”字符串的软件包是必须要安装的

1
2
3
4
[appuser@xxxxx ~]# rpm -qa openssl-devel openssl
openssl-1.0.2k-8.el7.x86_64
[appuser@xxxxx ~]# rpm -qa pcre-devel pcre
pcre-8.32-17.el7.x86_64

2.安装pcre-devel以及openssl-devel

1
# yum install -y openssl-devel pcre-devel

安装后查看下

1
2
3
4
5
6
[appuser@xxxxx~]# rpm -qa openssl-devel openssl
openssl-1.0.2k-8.el7.x86_64
openssl-devel-1.0.2k-8.el7.x86_64
[appuser@xxxxx~]# rpm -qa pcre-devel pcre
pcre-devel-8.32-17.el7.x86_64
pcre-8.32-17.el7.x86_64

3.开始安装Nginx:

1
# mkdir /usr/local/nginx

进入目录

1
# cd /usr/local/nginx

下载软件包,进入http://nginx.org/download/复制对应版本的下载链接地址。

1
# wget http://nginx.org/download/nginx-1.15.10.tar.gz

解压安装

1
2
3
4
5
[appuser@xxxxx nginx]# ls -l nginx-1.15.10.tar.gz
-rw-r--r-- 1 root root 981687 Oct 17 21:20 nginx-1.15.10.tar.gz
[appuser@xxxxx nginx]# useradd -M -s /sbin/nologin nginx
[appuser@xxxxx nginx]# tar zxf nginx-1.15.10.tar.gz
[appuser@xxxxx nginx]# cd nginx-1.15.10/

安装

1
2
# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module  --with-http_addition_module --with-http_sub_module  --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module  --user=nginx  --group=nginx
# make && make install

安装完成后的优化:

1
# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

启动Nginx

1
# nginx

查看

1
2
# netstat -anput | grep nginx
# ps -ef | grep nginx

停止

1
2
3
4
5
nginx -s stop   # 关闭
nginx -s quit # 退出(也是关闭)
nginx -s reload # 重新加载配置文件
nginx -s reopen # 重新打开日志文件
nginx -t # 测试配置文件
2. 安装php,php-fpm
yum安装php5.6
1
# yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof -y

查看php版本

1
2
3
4
5
6
# php --version

[appuser@xxxx /]$ php -v
PHP 5.6.0 (cli) (built: Apr 9 2019 16:00:50)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies

测试php-fpm

1
# netstat -lnt | grep 9000
安装php扩展

pgsql 扩展

找到相应的版本 php 5.6.0 https://www.php.net/releases/

1、从php官网下载一个合适版本的php;
2、从PHP解压包里ext目录找到pdo_pgsql 和 pgsql目录,并且复制放到liunx的 /root 目录(随便一个)
3、分别进入pdo_pgsql 和 pgsql目录 :
phpize
./configure (./configure –with-php-config=/usr/local/php56/bin/php-config)
make

make install

Ps:这里不要直接make && make install,可能会报错,很抓狂

4、修改php配置文件php.ini,添加pgsql.so、pdo_pgsql.so模块。
extension=pgsql.so

5、重启php-fpm

pdo_pgsql 扩展
按照相同的办法,安装 pdo_pgsql
还有php配置文件php.ini添加

1
extension=pdo_pgsql.so

安装pgsql扩展时候报错如下: (不知道啥情况,先放着)

1
2
3
Installing shared extensions:     /usr/local/php56/lib/php/extensions/no-debug-non-zts-20131226/
cp: cannot create regular file '/usr/local/php56/lib/php/extensions/no-debug-non-zts-20131226/#INST@558502#': Permission denied
make: *** [install-modules] 错误 1

![image-20190410131947892](/Users/magicyou/Library/Application Support/typora-user-images/image-20190410131947892.png)

基本完成,配置nginx并上传代码

1.创建项目文件夹

1
2
3
4
5
cd /
mkdir www
cd www
mkdir web-osp
chmod -R 777 web-osp

代码用命令行或者工具同步进服务器对应文件夹

2.修改配置文件nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#user  nobody;
worker_processes 4;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;
underscores_in_headers on;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format addHeaderlog '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for" ["$http_oam_uid"]';


access_log logs/access.log addHeaderlog;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;#8 128
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
#gzip on;

server {
listen 80;
server_name test.data.com.cn;
location / {
proxy_pass http://10.0.xx.x:9010/;
}
}

server {

include conf.d/*.conf;
}

编辑conf.d文件夹下的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
server {
listen 9010;
server_name 127.0.0.1;
root "/www/web-ceshi";
index index.html index.htm index.php info.php;
location / {
try_files $uri @rewrite;
proxy_set_header OAM_UID $http_OAM_UID;
proxy_pass_request_headers on;
}
location @rewrite {
set $static 0;
if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {
set $static 1;
}
if ($static = 0) {
rewrite ^/(.*)$ /index.php?s=/$1;
}
}

location ~ /Uploads/.*\.php$ {
deny all;
}
location ~ \.php/ {
if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $1;
fastcgi_param PATH_INFO $2;
fastcgi_param SCRIPT_FILENAME $document_root$1;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

重启php-fpm、nginx

部署基本完毕,尝试进入项目,出现两次502,记录如下

第一次 502 Bad Gateway

nginx配置和php-fpm端口不一致,改好,能登录

1
fastcgi_pass 127.0.0.1:9000;
第二次 502 Bad Gateway

登录后进入主页 502

查找问题

https://host.zzidc.com/wljc/928.html

查看是否是php-fpm的问题,找到正在使用的配置文件

1
2
3
4
5
6
[appuser@XXXXX lib]$ ps -ef | grep php-fpm
root 13824 1 0 2018 ? 00:17:16 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www 18922 13824 0 12:48 ? 00:00:01 php-fpm: pool www
www 24891 13824 0 14:08 ? 00:00:01 php-fpm: pool www
www 25449 13824 0 14:15 ? 00:00:00 php-fpm: pool www
appuser 28587 25750 0 14:46 pts/1 00:00:00 grep php-fpm

编辑php-fpm.conf

1
# /usr/local/php/etc/php-fpm.conf

没有 php-fpm.conf 的话

1
# cp php-fpm.conf.default php-fpm.conf

设置

1
2
3
request_terminate_timeout

request_terminate_timeout = 600

重启php-fpm,还是不行

去看php-fpm日志记录,发现日志没开,先打开日志记录

  • 1.修改php-fpm.conf中的配置**,如果没有请增加:

复制代码 代码如下:

1
2
3
4
[global]
error_log = log/php_error_log
[www]
catch_workers_output = yes
  • 2.修改php.ini中配置

,没有则增加:

复制代码 代码如下:

1
2
3
log_errors = On
error_log = "/usr/local/php/var/log/error_log"
error_reporting=E_ALL&~E_NOTICE
  • 3.重启php-fpm

当PHP执行错误时就能看到错误日志在”/usr/local/php56/var/log/php_error_log”中了

  • 4.浏览器进项目,然后查看看日志
1
2
# /usr/local/php56/var/log/php_error_log
[10-Apr-2019 15:30:58] WARNING: [pool www] child 272566 said into stderr: "php-fpm: pool www: symbol lookup error: /usr/local/php56/lib/php/extensions/no-debug-non-zts-20131226/pgsql.so: undefined symbol: _safe_emalloc_string"

重新装pgsql扩展
安装的时候不要make && make install
先 make, 然后make install

刷新网页

Call to undefined function bcdiv()

继续填坑

查找问题,没有安装扩展bcmath

同安装pgsql一样,安装后基本没发现其他问题

后续配置Apache,ldap集团单点登录

Apache的单点登录配置简单预热,详细看下一篇

cd /etc/httpd/conf/

sudo vim httpd-vhosts.conf

1
2
3
4
5
6
<VirtualHost *:9008>
ServerAdmin jituan-web-osp
DocumentRoot "/www/jituan-web"
ServerName jituan-web-osp
ServerAlias jituan-web-osp
</VirtualHost>

查看httpd进程

ps -ef | grep httpd

1
2
3
4
5
6
7
第一、启动、终止、重启

systemctl start httpd.service #启动

systemctl stop httpd.service #停止

systemctl restart httpd.service #重启

启动报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
appuser@xxxx conf]$ sudo systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 三 2019-04-10 17:18:23 CST; 10min ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 604898 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
Process: 604895 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
Main PID: 604895 (code=exited, status=1/FAILURE)

4月 10 17:18:23 xxxx systemd[1]: Starting The Apache HTTP Server...
4月 10 17:18:23 xxxx httpd[604895]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
4月 10 17:18:23 xxxx httpd[604895]: no listening sockets available, shutting down
4月 10 17:18:23 xxxx httpd[604895]: AH00015: Unable to open logs
4月 10 17:18:23 xxxx systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
4月 10 17:18:23 xxxx kill[604898]: kill: cannot find process ""
4月 10 17:18:23 xxxx systemd[1]: httpd.service: control process exited, code=exited status=1
4月 10 17:18:23 xxxx systemd[1]: Failed to start The Apache HTTP Server.
4月 10 17:18:23 xxxx systemd[1]: Unit httpd.service entered failed state.
4月 10 17:18:23 xxxx systemd[1]: httpd.service failed.

更根据上面的信息

修改端口80为9008

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[appuser@xxxx /]$ sudo systemctl restart httpd.service
您在 /var/spool/mail/appuser 中有新邮件
[appuser@xxxx /]$ sudo systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2019-04-10 17:35:06 CST; 8s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 604898 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
Main PID: 654829 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─654829 /usr/sbin/httpd -DFOREGROUND
├─654834 /usr/sbin/httpd -DFOREGROUND
├─654835 /usr/sbin/httpd -DFOREGROUND
├─654836 /usr/sbin/httpd -DFOREGROUND
├─654837 /usr/sbin/httpd -DFOREGROUND
└─654838 /usr/sbin/httpd -DFOREGROUND

4月 10 17:35:06 xxxx systemd[1]: Starting The Apache HTTP Server...
4月 10 17:35:06 xxxx systemd[1]: Started The Apache HTTP Server.