2019-11-06  2024-09-18    1297 字  3 分钟
PHP

Ubuntu 18.04

安装PHP7并配置Nginx

安装

 1sudo apt-get install software-properties-common python-software-properties
 2sudo add-apt-repository ppa:ondrej/php
 3sudo apt-get update
 4sudo apt-get -y install php7.3
 5
 6# 安装常用扩展
 7sudo -y apt-get install php7.3-fpm php7.3-mysql php7.3-curl php7.3-json php7.3-mbstring php7.3-xml php7.3-intl
 8
 9# 安装其他扩展(按需安装)
10sudo apt-get install php7.3-gd
11sudo apt-get install php7.3-soap
12sudo apt-get install php7.3-gmp
13sudo apt-get install php7.3-odbc
14sudo apt-get install php7.3-pspell
15sudo apt-get install php7.3-bcmath
16sudo apt-get install php7.3-enchant
17sudo apt-get install php7.3-ldap
18sudo apt-get install php7.3-opcache
19sudo apt-get install php7.3-readline
20sudo apt-get install php7.3-sqlite3
21sudo apt-get install php7.3-xmlrpc
22sudo apt-get install php7.3-bz2
23sudo apt-get install php7.3-interbase
24sudo apt-get install php7.3-pgsql
25sudo apt-get install php7.3-recode
26sudo apt-get install php7.3-sybase
27sudo apt-get install php7.3-xsl
28sudo apt-get install php7.3-cgi
29sudo apt-get install php7.3-dba
30sudo apt-get install php7.3-phpdbg
31sudo apt-get install php7.3-snmp
32sudo apt-get install php7.3-tidy
33sudo apt-get install php7.3-zip

配置Nginx+PHP

 1server {
 2    listen 80;
 3    listen [::]:80;
 4 
 5    root /var/www/akvicor.com/html;
 6    index index.php index.html index.htm index.nginx-debian.html;
 7 
 8    server_name akvicor.com www.akvicor.com;
 9
10    location / {
11        try_files $uri $uri/ /index.php$is_args$args;
12    }
13
14    location ~ \.php$ {
15        try_files $uri =404;
16
17        include fastcgi.conf;
18        fastcgi_pass 127.0.0.1:9000;
19    }
20}

编译安装PHP7

下载源码

PHP

1wget -c https://www.php.net/distributions/php-7.3.12.tar.gz
2tar -zxvf php-7.3.12.tar.gz
3cd php-7.3.12

安装需要的依赖

 1sudo apt-get install openssl
 2# configure: error: xml2-config not found. Please check your libxml2 installation.
 3sudo apt-get install libxml2-dev
 4# configure: error: Please reinstall the BZip2 distribution
 5sudo apt-get install libbz2-dev
 6# configure: error: Cannot find OpenSSL's libraries  or  ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib
 7sudo apt-get install libcurl4-openssl-dev 
 8# libxml2 not found. Please check your libxml2 installation
 9apt-get install libxml2-dev
10# configure: error: Please reinstall the libcurl distribution -easy.h should be in /include/curl/
11sudo apt-get install libcurl4-gnutls-dev
12# configure: error: jpeglib.h not found.
13sudo apt-get install libjpeg-dev
14# configure: error: png.h not found.
15sudo apt-get install libpng-dev
16# configure: error: libXpm.(a|so) not found.
17sudo apt-get install libxpm-dev
18# configure: error: freetype.h not found.
19sudo apt-get install libfreetype6-dev
20# configure: error: Your t1lib distribution is not installed correctly. Please reinstall it.
21sudo apt-get install libt1-dev
22# configure: error: mcrypt.h not found. Please reinstall libmcrypt.
23sudo apt-get install libmcrypt-dev
24# configure: error: Cannot find MySQL header files under yes.
25# Note that the MySQL client library is not bundled anymore!
26sudo apt-get install libmysql++-dev
27# configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
28sudo apt-get install libxslt1-dev
29# configure: error: Please reinstall the libzip distribution
30sudo apt-get install libzip-dev
31# configure: error: DBA: Could not find necessary header file(s).
32sudo apt-get install libgdbm-dev
33# configure: error: Cannot find OpenSSL's <evp.h>
34sudo apt-get install libssl-dev

编译配置

 1./configure --prefix=/usr/local/php \
 2--with-curl \
 3--with-freetype-dir \
 4--with-gd \
 5--with-gettext \
 6--with-iconv-dir \
 7--with-kerberos \
 8--with-libdir=lib64 \
 9--with-libxml-dir \
10--with-mysqli \
11--with-openssl \
12--with-pcre-regex \
13--with-pdo-mysql \
14--with-pdo-sqlite \
15--with-pear \
16--with-png-dir \
17--with-jpeg-dir \
18--with-xmlrpc \
19--with-xsl \
20--with-zlib \
21--with-bz2 \
22--with-mhash \
23--enable-fpm \
24--enable-bcmath \
25--enable-libxml \
26--enable-inline-optimization \
27--enable-mbregex \
28--enable-mbstring \
29--enable-opcache \
30--enable-pcntl \
31--enable-shmop \
32--enable-soap \
33--enable-sockets \
34--enable-sysvsem \
35--enable-sysvshm \
36--enable-xml \
37--enable-zip

编译成功显示:

 1| License:                                                           |
 2| This software is subject to the PHP License, available in this     |
 3| distribution in the file LICENSE.  By continuing this installation |
 4| process, you are bound by the terms of this license agreement.     |
 5| If you do not agree with the terms of this license, you must abort |
 6| the installation process at this point.                            |
 7+--------------------------------------------------------------------+
 8
 9Thank you for using PHP.
10
11config.status: creating php7.spec
12config.status: creating main/build-defs.h
13config.status: creating scripts/phpize
14config.status: creating scripts/man1/phpize.1
15config.status: creating scripts/php-config
16config.status: creating scripts/man1/php-config.1
17config.status: creating sapi/cli/php.1
18config.status: creating sapi/fpm/php-fpm.conf
19config.status: creating sapi/fpm/www.conf
20config.status: creating sapi/fpm/init.d.php-fpm
21config.status: creating sapi/fpm/php-fpm.service
22config.status: creating sapi/fpm/php-fpm.8
23config.status: creating sapi/fpm/status.html
24config.status: creating sapi/phpdbg/phpdbg.1
25config.status: creating sapi/cgi/php-cgi.1
26config.status: creating ext/phar/phar.1
27config.status: creating ext/phar/phar.phar.1
28config.status: creating main/php_config.h
29config.status: main/php_config.h is unchanged
30config.status: executing default commands

编译并安装

1make && make install

配置相应文件

1cp php.ini-development /usr/local/php/lib/php.ini 
2cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
3cp sapi/fpm/php-fpm /usr/local/bin
4cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

然后设置php.ini,使用:vim /usr/local/php/lib/php.ini 打开php配置文件找到 cgi.fix_pathinfo 配置项,这一项默认被注释并且值为1,根据官方文档的说明,这里为了当文件不存在时,阻止Nginx将请求发送到后端的PHP-FPM模块,从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为0,设置完毕保存并且退出。

创建web用户

1groupadd www-data
2useradd-g www-data www-data
3
4// 打开www.conf文件,配置用户权限
5vim /usr/local/php/etc/php-fpm.d/www.conf
6默认user和group的设置为nobody,将其改为www-data

启动php-fpm服务

1/usr/local/bin/php-fpm

php-fpm服务默认使用9000端口,使用 netstat -tln |grep 9000 可以查看端口使用情况,9000端口正常使用,说明php-fpm服务启动成功。

配置mginx

打开nginx.conf文件 首先找到user 设置为:user www-data www-data; 然后在service{}层中 添加php location段

 1       location / {
 2              root  /var/www/test;
 3              index index.php index.html; # 添加index.php
 4        }
 5
 6
 7        location ~ \.php$ {
 8           root           /var/www/test; # 项目文件根目录
 9           fastcgi_pass   127.0.0.1:9000; # 对应的php-fpm地址和端口
10           fastcgi_index  index.php; # 默认访问文件
11           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # 将 /scripts改为$document_root
12           include        fastcgi_params;
13        }

修改完之后保存nginx.conf文件 并且重启nginx

1/usr/local/nginx/sbin/nginx -s reload

然后在项目路径中创建一个index.php文件,内容可以如下:

1<?php 
2    echo phpinfo(); 
3?>

然后打开浏览器输入对应的地址进行访问,看到输出页面,说明nginx和php都配置成功。

除另有声明外本博客文章均采用 知识共享 (Creative Commons) 署名 4.0 国际许可协议 进行许可转载请注明原作者与文章出处