最近在搭建网站,下面是我用的LNMP版本:
  Centos 8
  PHP 7.4.10
  Nginx 1.18.0
  Mysql 8.0.21

安装Nginx

yum install yum-utils

编写nginx.repo文件:

vi /etc/yum.repos.d/nginx.repo

然后复制下面到nginx.repo里面:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

这里我们一般都用stable(稳定版),如果要使用mainline(主线版),需要使用命令切换

yum-config-manager --enable nginx-mainline

然后我们就可以直接用yum安装:

 yum install nginx

安装完成之后启动:

nginx

安装PHP

先安装依赖包:

yum install gcc gcc-c++ make libxml2-devel openssl-devel curl-devel  libpng-devel sqlite-devel oniguruma libzip-devel bzip2-devel  readline-devel libxslt-devel

除了上面的,我们还需要单独下载oniguruma-devel进行安装:

wget http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-1.el8.x86_64.rpm

下载完成之后用rpm安装:

rpm -ivh oniguruma-devel-6.8.2-1.el8.x86_64.rpm

接着我们可以下载php源码包了:

wget https://www.php.net/distributions/php-7.4.10.tar.gz

下载完成之后解压:

tar -zxvf php-7.4.10.tar.gz

解压完成之后,进入目录(cd php-7.4.10)执行configure,这里面的–with-fpm-user和–with-fpm-group最好使用新建的普通账号(可以使用命令addusr):

./configure --enable-fpm   --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-opcache --enable-pcntl -with-xsl --enable-sockets  --enable-exif --with-gettext --with-readline --with-bz2  --enable-calendar --with-openssl --with-zlib --with-curl --enable-gd --enable-ftp  --enable-mbstring  --with-zip --enable-mysqlnd

配置完成之后,会看到下面的内容,表示配置成功了:

+--------------------------------------------------------------------+
| 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.

接下来我们就可以开始编译了,在编译的时候我们可以加上-j选项来加快编译速度,后面的数字就是CPU核心数(可以使用命令cat /proc/cpuinfo查看):

make -j4

编译时间比较久,等编译结束之后,可以看到下面的内容表示编译成功了:

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
invertedregexiterator.inc
directorytreeiterator.inc
clicommand.inc
directorygraphiterator.inc
pharcommand.inc
phar.inc
Build complete.
Don't forget to run 'make test'.

接下来直接安装就可以了:

make install

安装结束之后,需要拷贝配置文件到对应的目录下面:

cp php.ini-production /usr/local/php/php.ini
cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin

php.ini-production是生产环境的配置文件,php.ini-development是开发环境的配置文件。

修改/usr/local/etc/php-fpm.conf文件,把NONE改成/usr/local:

include=/usr/local/etc/php-fpm.d/*.conf

直接启动php-fpm:

php-fpm -c /usr/local/php/php.ini

安装php的扩展

在ext文件下,可以看到很多的文件夹,这些文件夹里面就是对应扩展的源码:

bcmath curl exif ftp iconv libxml odbc pdo pdo_odbc posix shmop sockets sysvmsg xml zend_test
bz2 date ext_skel.php gd imap mbstring opcache pdo_dblib pdo_pgsql pspell simplexml sodium sysvsem xmlreader zip
calendar dba ffi gettext intl mysqli openssl pdo_firebird pdo_sqlite readline skeleton spl sysvshm xmlrpc zlib
com_dotnet dom fileinfo gmp json mysqlnd pcntl pdo_mysql pgsql reflection snmp sqlite3 tidy xmlwriter
ctype enchant filter hash ldap oci8 pcre pdo_oci phar session soap standard tokenizer xsl

这里以安装mysqli为例,首先我们需要安装autoconf:

yum install autoconf

进入fileinfo目录,执行phpize命令:

/usr/local/bin/phpize

完成之后执行configure:

./configure -with-php-config=/usr/local/bin/php-config

配置完成之后,执行make编译安装:

make -j2 && make install

成功之后,在输出信息中可以找到对应的so文件,默认所有扩展都会安装在/usr/local/lib/php/extensions/no-debug-non-zts-20190902/目录下面:

Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/

安装MySQL

关于MySQL的安装,可以参考我的另一篇文章,里面有详细说明:

2+

本文为原创文章,转载请注明出处,欢迎访问作者网站(和而不同)

发表评论

error: Content is protected !!