1、 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc gcc-c++2、 安装 PCRE (Perl Compatible Regular Expressions)库,PCRE是一个正则表达式库,nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
yum install -y pcre pcre-devel3、安装 zlib 库 —— zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要安装 zlib 库:
yum install -y zlib zlib-devel4、安装OpenSSL库。OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。Nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要安装 OpenSSL 库:
yum install -y openssl openssl-devel5、安装Nginx到目录/usr/local/services:
官网下载tar.gz安装包使用wget命令下载:wget -c https://nginx.org/download/nginx-1.14.0.tar.gz进入解压后目录,编译安装:./configure --prefix=/usr/local/services/nginx&make&make install启动停止nginx: ./nginx —— 启动./nginx -s stop —— 强制停掉nginx进程./nginx -s quit —— 待nginx进程处理完任务后停止./nginx -s reload —— 重新加载配置然后在浏览器里访问 80 端口,如果可以看到如下页面,表示 Nginx 安装成功:
PHP-FPM(FastCGI Process Manager:FastCGI进程管理器),是 FastCGI 的实现,并提供了进程管理的功能。进程包含 master 进程和 worker 进程两种进程。
master 进程只有一个,负责监听端口,接收来自 Web Server 的请求,worker 进程则一般有多个(具体数量根据实际需要配置),每个进程内部都嵌入了一个 PHP 解释器,是 PHP 代码真正执行的地方。PHP从 5.3.3 版本开始已经讲 php-fpm 纳入php源码核心了,不再是第三方的包,所以不需要另外下载了。只需要在编译 php 的时候带上--enable-fpm就可以了。
① 获取tar.gz安装包
wget -c http://cn.php.net/distributions/php-7.1.17.tar.gz② 进入解压后目录:
./configure --prefix=/usr/local/services/php7 \ --with-openssl \ --with-pcre-regex \ --with-kerberos \ --with-libdir=lib \ --with-libxml-dir \ --with-mysqli=shared,mysqlnd \ --with-pdo-mysql=shared,mysqlnd \ --with-pdo-sqlite \ --with-gd \ --with-iconv \ --with-zlib \ --with-xmlrpc \ --with-xsl \ --with-pear \ --with-gettext \ --with-curl \ --with-png-dir \ --with-jpeg-dir \ --with-freetype-dir \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --enable-mysqlnd \ --enable-zip \ --enable-inline-optimization \ --enable-shared \ --enable-libxml \ --enable-xml \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-mbregex \ --enable-mbstring \ --enable-ftp \ --enable-gd-native-ttf \ --enable-pcntl \ --enable-sockets \ --enable-soap \ --enable-session \ --enable-opcache \ --enable-fpm \ --enable-maintainer-zts \ --enable-fileinfo执行./configure时发现有些库没有装,于是yum安装了一下库:
yum install libxml2-devel // error: libxml2 not found yum install curl-devel // error: cURL version 7.10.5 or later is required to compile php with cURL support yum -y install libjpeg-devel // error: jpeglib.h not found. yum -y install libpng-devel // error: png.h not found. yum install freetype freetype-devel // error: freetype-config not found. yum install libxslt libxslt-devel // error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution配置成功以后,执行make && make install安装。
③ 配置php
cp /usr/local/php-7.1.17/php.ini-development /usr/local/services/php7/lib/php.ini修改 php.ini 的内容:
######避免PHP信息暴露在http头中 expose_php = Off ######避免暴露php调用mysql的错误信息 display_errors = Off ######在关闭display_errors后开启PHP错误日志 log_errors = On ######错误日志路径 error_log = /usr/local/services/php7/log/php_errors.log ######设置PHP的扩展库路径 extension_dir = "/usr/local/services/php7/lib/php/extensions/no-debug-zts-20160303/" ######设置PHP的一些动态库 zend_extension = opcache.so extension=amqp.so extension=redis.so extension=mongodb.so extension=swoole.so ######设置PHP的时区 date.timezone = Asia/Shanghai ######开启opcache [opcache] opcache.enable=1 opcache.enable_cli=1④ 配置 php-fpm
cp etc/php-fpm.conf.default etc/php-fpm.conf修改 php-fpm.conf 的内容:
######设置错误日志的路径 error_log = /usr/local/php7/log/php-fpm.log ######设置日志级别 log_level = error ######引入www.conf文件中的配置 include=/usr/local/php7/etc/php-fpm.d/*.conf⑤ 配置www.conf
www.conf是 php-fpm 进程服务的扩展配置文件
cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf修改 www.conf 的内容:
######设置用户和用户组 user = www group = www如果不存在则创建用户&用户组
groupadd www useradd -g www www⑥ 启动php-fpm
/usr/local/services/php7/sbin/php-fpm可以查看进程或者9000端口的监听情况。
Lumen是Laravel框架的精简版,可以快速的开发API接口服务。
1、安装composer
curl -sS https://getcomposer.org/installer | php如果提示找不到php,请建软链:
ln -s /usr/local/services/php7/bin/php /usr/bin/php2、创建lumen项目
如果我们要在/data/project/目录下建项目,进入该目录并切换到www用户(不能用root用户执行composer命令),执行:
php composer.phar create-project --prefer-dist laravel/lumen demo执行成功以后,就在当前目录下创建了一个叫demo的项目。
3、写一个最简单的接口
在文件/data/project/demo/routes/web.php里添加一个路由:
<?php $router->get('/', function () use ($router) { return $router->app->version(); }); $router->get('getHelloWorld', 'ExampleController@getHelloWorld'); // 添加一个接口在文件/data/project/demo/app/Http/Controllers/ExampleController.php里添加一个getHelloWorld方法:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ExampleController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { } // 添加一个方法 public function getHelloWorld(Request $request) { return "hello, lumen"; } }到这里接口就开发完成了
1、nginx转发配置
假设我们指定请求的域名为api.lumen.com,则当 nginx 收到通过该域名发来的HTTP请求时,将请求转发到后端的php-fpm模块。
在/usr/local/services/nginx/conf 配置目录下新建一个配置文件api.lumen.com.conf,内容如下:
server { listen 80; server_name api.lumen.com; root /data/project/demo/public; index index.php; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Headers' 'X_forwarded-for,Content-Type'; add_header 'Access-Control-Allow-Origin' '$http_origin'; add_header 'Access-Control-Allow-Methods' 'GET,POST'; location / { if ($request_method = 'OPTIONS') { return 200; } try_files $uri $uri/ /index.php?$query_string; } location ~ \.php{ try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi.conf; } }然后在nginx.conf的最后把上面的配置文件 include 进去:
http { ... include api.lumen.com.conf; }重新加载 nginx 配置:
./nginx -s reload2、测试接口
在/etc/hosts里添加:
127.0.0.1 api.lumen.com请求接口:
[root@VM_104_153_centos conf]# curl "http://api.lumen.com/getHelloWorld" hello, lumen成功返回。
