LNMP生产实战环境
Posted DonotCTR
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP生产实战环境相关的知识,希望对你有一定的参考价值。
1、概述
LNMP所代表的是linux、nginx、mysql、php四个不同的服务,linux系统是基础,nginx是web服务器,mysql数据库数据存储,php是编码语言。php语言在很多公司涉及到web页面,或者游戏开发方面,还是有些公司在使用的。这里主要讲述这个架构在生产环境中搭建,mysql一般情况都不用搭建的,一般情况下都是搭建过一次就ok,如有兴趣了解,可以参考:MySQL相关知识 ,例如我们公司在mysql这个数据存储方面,就是使用的云数据库,减少运维成本。一般情况,nginx和php-fpm都是安装在同一台服务器上,并且php-fpm端口地址都是127.0.0.1:9000,php-fpm在使用方面安装第三方插件swoole、amqp、redis就足够用了。通过本篇文档帮助更多的同行者熟悉LNMP这个环境,望多多支持。
相关软件包地址:
链接:https://pan.baidu.com/s/1sCQDa_TR5uZXmchzb7UznQ
提取码:gEt5
--来自百度网盘超级会员V5的分享
2、nginx部署
直接YUM安装即可,除非有更高的模块要求,rpm包是提前下载好的
yum install -y nginx-1.16.1-1.el7.ngx.x86_64.rpm
常见的配置参数:
SSL证书配置
80跳转443
数据缓存
fastcgi优化参数
server
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/cert/***.crt; #SSL证书,这个cert目录一般是没有,需要创建的
ssl_certificate_key /etc/nginx/cert/***.key; #SSL证书
server_name *******; #域名
server_tokens off;
root *********; #项目的主目录
index index.php index.html index.htm;
client_max_body_size 300m;
#自动跳转到HTTPS
if ($server_port = 80)
rewrite ^(.*)$ https://$host$1 permanent;
location /
if ( -f $request_filename)
break;
if ( !-e $request_filename)
rewrite ^(.*)$ /index.php/$1 last;
break;
error_page 500 502 503 504 /50x.html;
location = /50x.html
root /usr/share/nginx/html;
location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf)$
expires 1h;
location ~ .*\\.(js|css)?$
expires 1h;
location ~ .+\\.php($|/)
fastcgi_split_path_info ^(.+?\\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_connect_timeout 600;
fastcgi_read_timeout 600;
fastcgi_send_timeout 600;
fastcgi_buffer_size 64k;
fastcgi_buffers 8 16k;
fastcgi_busy_buffers_size 64k;
fastcgi_temp_file_write_size 64k;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php?IF_REWRITE=1;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $script;
fastcgi_param HTTP_X_FORWARDED_FOR $http_x_forwarded_for;
include fastcgi_params;
if ($time_iso8601 ~ "(\\d4)-(\\d2)")
set $time $1$2;
location ~ /\\.well-known/apple-app-site-association
allow all;
location ~ /\\.
deny all;
3、php-fpm部署
3.1、基础依赖
yum -y install gcc automake autoconf libtool make gcc-c++ glibc libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel pcre pcre-devel libmcrypt libmcrypt-devel libcurl libcurl-devel gmp gmp-devel readline readline-devel
3.2、libmcrypt---(必备)
tar -xf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure --prefix=/usr/local/libmcrypt
make
make install
3.3、PHP基础配置
tar -xf php-7.1.26.tar.gz
cd php-7.1.26
./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --with-mcrypt=/usr/local/libmcrypt --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gd --with-iconv --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache
make
make install
cp php.ini-production /usr/local/php7/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm
chmod +x /etc/init.d/php7-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
3.4、PHP常用配置
作为运维,啥也可以先不优化,先把错误日志、慢查询这些开起来,方便运维优化php-fpm性能和开发通过慢查询优化代码。
基础配置,个人习惯问题,添加软连接方便使用tab
ln -s /usr/local/php7/sbin/php-fpm /sbin/php-fpm
ln -s /usr/local/php7/bin/php /sbin/php
开启慢查询配置文件:/usr/local/php7/etc/php-fpm.d/www.conf
slowlog = log/$pool.log.slow #这个是/usr/local/php7目录下新建一个log目录
request_slowlog_timeout = 3 #慢查询时间为3,这个开发优化代码方便多了,这个是根据公司业务做的
mkdir -p /usr/local/php7/log/ #不提前创建,php-fpm -t检查配置会报错的
php-fpm -t
/etc/init.d/php7-fpm start
ll /usr/local/php7/log/
总用量 0
-rw-------. 1 root root 0 4月 14 00:31 www.log.slow
开启错误日志:/usr/local/php7/etc/php.ini
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
log_errors = On
error_log = var/log/php_errors.log #代表/usr/local/php7/var/log/php-fpm -t
php-fpm -
NOTICE: configuration file /usr/local/php7/etc/php-fpm.conf test is successful
/etc/init.d/php7-fpm reload
ll /usr/local/php7/var/log/ #此时此刻你会发现没有那个日志文件,不要惊讶,我一开始也是一样。
配置文件:/usr/local/php7/etc/php-fpm.conf
error_log = log/php_errors.log
php-fpm -t
NOTICE: configuration file /usr/local/php7/etc/php-fpm.conf test is successful
/etc/init.d/php7-fpm reload
ll /usr/local/php7/var/log/ #已经成功
其他具体的参数优化后面持续更新中,往多多关注
3.5、redis插件
tar -xf redis-3.1.6.tgz
cd redis-3.1.6
/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make
make install
echo "extension=redis.so" >> /usr/local/php7/etc/php.ini
3.6、swoole插件
tar -xf swoole.v1.10.5.tar.gz
cd swoole-src-1.10.5/
/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make
make install
echo "extension=swoole.so" >> /usr/local/php7/etc/php.ini
3.7、amqp插件
tar -xf rabbitmq-c-0.8.0.tar.gz
cd rabbitmq-c-0.8.0
./configure --prefix=/usr/local/rabbitmq-c-0.8.0
make
make install
tar -xf amqp-1.9.3.tgz
cd amqp-1.9.3
/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c-0.8.0
make
make install
echo "extension=amqp.so" >> /usr/local/php7/etc/php.ini
3.8、插件验证
php-fpm -t
/etc/init.d/php7-fpm reload
所有安装的模块
3.9、PHP验证
在任意目录下创建一个index.php文件
phpinfo();
在nginx上配置一下,方便访问index.php文件
server
listen 8090;
server_tokens off;
root /data/www/; #那个index.php记得放在这里目录下哦
index index.php index.html index.htm;
location /
if ( -f $request_filename)
break;
if ( !-e $request_filename)
rewrite ^(.*)$ /index.php/$1 last;
break;
error_page 500 502 503 504 /50x.html;
location = /50x.html
root /usr/share/nginx/html;
location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf)$
expires 1h;
生产环境LNMP (果图片)