centos8配置lnmpr


linux下,centos8配置nginx/mysql/php/redis

本文不断打磨,有什么事装个系统和环境,使用最新的系统和软件,如有更新实时记录!!

下面涉及到版本的,建议都访问目录获取最新的release版本。

可能存在的安装错误和解决方案

这个放在前面,大部分都是lnmp的基础库,建议先安装

如果提示:-bash: make: command not found
执行yum -y install automake autoconf libtool make

nginx相关

如果提示:./configure: error: the HTTP rewrite module requires the PCRE library
执行yum -y install pcre-devel

如果提示:./configure: error: C compiler cc is not found
执行yum -y install gcc

如果提示:./configure: error: SSL modules require the OpenSSL library.
执行yum -y install openssl openssl-devel

php相关:

如果提示:configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
执行yum install sqlite-devel

如果提示:configure: error: Package requirements (libcurl >= 7.15.5) were not met:
执行yum install -y libcurl-devel

如果提示:configure: error: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met:
执行yum install libicu-devel

或者直接省事:

yum install -y gcc gcc-c++  make zlib zlib-devel 
yum install -y automake autoconf libtool make
yum install -y openssl openssl-devel pcre pcre-devel  libjpeg libjpeg-devel libpng libpng-devel
yum install -y curl curl-devel libicu-devel libcurl-devel sqlite-devel openldap openldap-devel
yum install -y freetype freetype-devel libxml2 libxml2-devel


yum install -y glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel
yum install -y e2fsprogs e2fsprogs-devel krb5 krb5-devel ncurses ncurses-devel 
yum install -y nss_ldap openldap-clients openldap-servers

如果提示:configure: error: Cannot find ldap libraries in /usr/lib.
执行cp -frp /usr/lib64/libldap* /usr/lib/

如果提示:configure: error: Package requirements (oniguruma) were not met:
其实引入mbstring但是不要正则设置--enable-mbstring --disable-mbregex即可,如果要,则:
1.执行yum安装:

yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-6.7.0-1.el7.x86_64.rpm
yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-devel-6.7.0-1.el7.x86_64.rpm

2.执行源码安装:

# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
# tar -zxf oniguruma-6.9.4.tar.gz
# cd oniguruma-6.9.4
# ./autogen.sh && ./configure --prefix=/usr --libdir=/lib64 
# make && make install

进入目录后,网上有方案2,不过我centos8编译成功后依然提示Package 'oniguruma', required by 'virtual:world', not found

方案2(亲测无效):
autoreconf -vfi 
./configure 
make && make install 

nginx安装与配置(其实推荐使用openresty了)

wget http://nginx.org/download/nginx-1.23.3.tar.gz
tar -zxvf nginx-1.23.3.tar.gz 
cd nginx-1.23.3/
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install

安装完毕后,测试一下:/usr/local/nginx/sbin/nginx -t

配置安全组后,可以配置防火墙端口:

firewall-cmd --state
firewall-cmd --list-ports
systemctl enable firewalld --now
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
systemctl restart firewalld.service

nginx 开机启动(服务方式,推荐)

(nginx/openresty)

vim /lib/systemd/system/nginx.service

Nginx:

[Unit]
Description=nginx
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

Openresty:

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

设置开机启动systemctl enable nginx.service服务,然后就可以:
查看状态:systemctl status nginx.service
开启服务:systemctl start nginx.service

nginx 开机启动2(不推荐,推荐使用systemctl 取代service和chkconfig)

#vim /etc/init.d/nginx
(https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/)
Usage: /etc/init.d/nginx {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}
#chmod a+x /etc/init.d/nginx
#chkconfig --add /etc/init.d/nginx
#chkconfig nginx on

增加开机启动:
直接启动nginx服务:/usr/local/nginx/sbin/nginx
将上述命令放到/etc/rc.d/rc.local
(好像无效,centos7要通过chkconfig实现,其实直接使用1的系统服务方式最简单)

php7安装与配置

wget https://www.php.net/distributions/php-7.4.3.tar.gz
tar -zxvf php-7.4.3.tar.gz
cd php-7.4.3/

最基础的编译参数:`
./configure --prefix=/usr/local/php --enable-fpm --with-pdo-mysql --with-openssl`

在编译之前,需要确保/usr/local/phpjpeglib库存在。
这个参数--enable-gd-jis-conv 会导致"imagettfbbox(): any2eucjp(): invalid code in input string"异常
作为一个开发环境,推荐使用以下完整编译参数,遇到问题直接yum就可以成功的是:

./configure  \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--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-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbstring --disable-mbregex \
--enable-intl \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--with-gettext \
--enable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm \
--enable-pcntl \
--enable-gd \
--with-jpeg \
--with-freetype

如果提示PEAR package PHP_Archive not installed,其实可以完全跳过,等下make install的时候会自动安装。
如果你需要搞一下,那么更改一下编译参数,将--with-pear修改为--without-pear --disable-phar,然后安装完毕再装pear:

wget  http://pear.php.net/go-pear.phar 
/usr/local/php/bin/php go-pear.phar

在版本PHP7.4之前,可参考如下编译命令:

./configure  --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --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-zlib --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbstring --disable-mbregex --enable-intl --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --with-gettext --enable-fileinfo --enable-opcache --with-pear --enable-maintainer-zts --with-ldap=shared --without-gdbm --enable-pcntl --with-gd --with-jpeg-dir --with-freetype-dir

当上面的configure出现了如下提示,则表示一切准备就绪(否则就遇神杀神佛挡弑佛ba...)

Generating files
configure: patching main/php_config.h.in
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

完美,接下来执行三部曲(其实test没啥用):

$ make
$ make test
$ make install

安装成功提示:

[root@localhost php-7.4.3]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20190902/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM defconfig:     /usr/local/php/etc/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing phpdbg binary:         /usr/local/php/bin/
Installing phpdbg man page:       /usr/local/php/php/man/man1/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:          /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.9
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.5
warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.9 is not the recommended version 1.4.4
[PEAR] PEAR           - installed: 1.10.12
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/root/php-7.4.11/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin/phar.phar
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

三大缺少文件:

一、将同目录中的www.conf.defalt修改成php-fpm.conf:

cd /usr/local/php/etc/php-fpm.d/
cp -a www.conf.default www.conf

二、将/usr/local/php/etc/php-fpm.conf.default拷贝一个作为php-fpm.conf:

cd /usr/local/php/etc/
cp -a php-fpm.conf.default php-fpm.conf

三、压缩包的根目录拷贝php.ini-production到php安装目录/usr/local/php/etc下:

cp -a /root/php-7.4.3/php.ini-development /usr/local/php/etc/php.ini

手动启动php-fpm

先创建编译指定的www用户:useradd www(会自动创建同名用户组)

执行:/usr/local/php/sbin/php-fpm

表示已经启动了php-fpm了:ps aux|grep php-fpm |grep -v grep

php-fpm 开机启动

修改/usr/local/php/etc/php-fpm.conf,开启pid去掉pid = run/php-fpm.pid注释

推荐使用服务方式:
创建文件内容如下:sudo vim /usr/lib/systemd/system/php-fpm.service

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重新加载systemctl配置:sudo systemctl daemon-reload
开机启动服务:sudo systemctl enable php-fpm.service
基本操作:

systemctl status php-fpm
systemctl start php-fpm
systemctl restart php-fpm
systemctl stop php-fpm
systemctl didable php-fpm # 取消开机启动

以下方式不推荐:

php-fpm启动脚本,拷贝到/etc/init.d/目录下:
cp /root/php-7.4.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
授权`chmod a+x /etc/init.d/php-fpm`

ps aux|grep php-fpm
查看状态:/etc/init.d/php-fpm status
{start|stop|force-quit|restart|reload|status|configtest}

设置开机启动
#chkconfig --add /etc/init.d/php-fpm
#chkconfig php-fpm on

(配置user和groud为www的/usr/local/php/etc/php-fpm.d/www.conf否则mkdir不行)
(配置session存储/usr/local/php/etc/php.ini配置session.save_path = "/var/lib/php/session")

详细配置(含用phpize无需重新安装增加openssl模块):

cp www.conf.default www.conf
/usr/local/php/sbin/php-fpm -t
[root@localhost php-fpm.d]# service php-fpm start
ps aux|grep php-fpm
vim /etc/rc.local 
vim /etc/rc.d/rc.local
location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

收尾配置

很重要?:/root/php-7.4.30/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin

composer install --ignore-platform-reqs

当前用户的全局变量

# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
# ln -s /usr/local/php/bin/php /usr/local/bin/

安装了php7之后再安装php5的操作:
(待续...)

重启改为手动启动LNMPR的方式

创建~/start.sh文件,粘帖下面内容后执行sh start.sh即可手动启动

sudo systemctl start mariadb
/usr/local/php/sbin/php-fpm
/usr/local/nginx/sbin/nginx
/etc/init.d/redis start

注意这些路径都必须按本文的安装路径缺省

其他

  • php查看编译参数:
/usr/local/php/bin/php -i |grep configure
  • File not found.
一般都是与php-fpm衔接出错,注意fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;即可
  • 安装composer
官网https://packagist.org/
直接下载到当前目录:wget https://getcomposer.org/composer.phar
下载完之后,拷贝到全局执行目录:cp -a composer.phar /usr/local/bin/composer
小小授个权:chmod u+x /usr/local/bin/composer
  • mysql和redis
剩下的mysql和redis由本站点的其他笔记发布,搜索mariadbredis即可...
  • composer的安装和使用
# /usr/local/php/bin/php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# /usr/local/php/bin/php composer-setup.php
直接安装:# /usr/local/php/bin/php /root/composer.phar install
  • Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] No such file or directory’
出现这个问题,很坑,居然是需要在php.ini中配置缺省的sock文件,
当然不一定等于/tmp/mysql.sock,也可能等于/var/lib/mysql/mysql.sock
mysql.default_socket = /tmp/mysql.sock
pdo_mysql.default_socket= /tmp/mysql.sock
mysqli.default_socket =/tmp/mysql.sock

原文链接:https://blog.yongit.com/note/211000.html