2019独角兽企业重金招聘Python工程师标准>>>

本文索引:

  • LAMP架构介绍
  • Mysql的安装
  • PHP的安装
  • Nginx介绍
  • Nginx的安装

LNMP架构介绍

LNMP = Linux + Ningx + Mysql + PHP

由Nginx取代apache,提供web服务;

PHP作为一个独立服务存在而非apache的一个模块,这个服务为php-fpm;

Nginx直接处理静态请求,动态请求会转发给php-fpm。

Nginx在处理静态文件的速率较Apache要快的多,这时两者的底层设计所决定的。同时Nginx可以处理的并发访问量也较Apache要大的多,毕竟Apache创建之初并没有考虑到当今的高并发访问量的规模会如此之大。Apache采用的是多进程的方式,对内存的要求会较高,而且有限制;Nginx采用多线程的方式,在内存消耗方面要比Apache好的多。


Mysql的安装

Mysql安装的方法与LAMP架构中一致

可能缺少的软件包:

[root@localhost src]# yum install -y perl-Data-Dumper libaio-devel

具体操作如下:

  • 解压软件包,重命名并移动至/usr/local
[root@localhost src]# tar zxf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
[root@localhost src]# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
  • 创建相关目录和用户
[root@localhost src]# cd /usr/local/mysql/
[root@localhost mysql]# useradd mysql
[root@localhost mysql]# mkdir /data
  • 初始化
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • 配置my.cnf
[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf
[root@localhost mysql]# vi /etc/my.cnf
"修改[mysqld]内的2行即可basedir = /usr/local/mysqldatadir = /data/mysql
保存退出"
  • 配置自启动脚本
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# vi /etc/init.d/mysqld
"同样要修改一下参数
basedir=/usr/local/mysql
datadir=/data/mysql"
  • 配置开启启动并启动服务
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# service mysqld start

本人安装过程中报的一个错(读安装过程报错十分重要)

...
./bin/mysqld: Can't create/write to file '/tmp/iboFQUc3' (Errcode: 30 - Read-only file system)
...

发现是我之前在tmp下挂载了磁盘镜像,mysql_install_db脚本对/tmp目录又有写权限导致的错误。执行umount /tmp后,脚本运行正常。


PHP的安装

PHP的安装的部分参数较LAMP不同

  1. 解压压缩包
[root@localhost src]# tar zxvf php-5.6.30.tar.gz
[root@localhost src]# cd php-5.6.30
  1. 创建用户php-fpm
[root@localhost src]# useradd -M -s /sbin/nologin php-fpm
  1. 安装依赖包
[root@localhost php-5.6.30]# yum install -y epel-release
[root@localhost php-5.6.30]# yum install -y gcc gcc-c++ libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel
  1. 编译安装
[root@localhost php-5.6.30]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl
[root@localhost php-5.6.30]# make && make install

php7版本已经取消--with-mysql参数,编译安装时该参数不写,写了会报错

  1. 修改配置文件
[root@localhost php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
[root@localhost php-5.6.30]# cd  /usr/local/php-fpm/etc
[root@localhost etc]# vi php-fpm.conf
[root@localhost etc]# cat php-fpm.conf
[global] # 定义全局参数
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www] # 模块名
listen = /tmp/php-fcgi.sock  # 监听socket
#listen = 127.0.0.1:9000
listen.mode = 666 # 监听是socket才会生效
user = php-fpm  # 定义用户
group = php-fpm # 定义组
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
  1. 设置启动脚本,并设置开机启动
# 启动脚本模板文件在/usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm
[root@localhost etc]# cd /usr/local/src/php-5.6.30
[root@localhost php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.6.30]# chmod 755 /etc/init.d/php-fpm
[root@localhost php-5.6.30]# chkconfig --add php-fpm
[root@localhost php-5.6.30]# chkconfig --level 345 php-fpm on
  1. 启动php-fpm服务
[root@localhost php-5.6.30]# service php-fpm start
Starting php-fpm  done

php-fpm脚本用法

相对于LAMP中php安装目录,LNMP安装的php多了一些文件、目录


[root@localhost php-5.6.30]# ls /usr/local/php-fpm/
bin  etc  include  lib  php  sbin  var
[root@localhost php-5.6.30]# ls /usr/local/php-fpm/var/
log  run# sbin目录下是php-fpm启动脚本
[root@localhost php-5.6.30]# ls /usr/local/php-fpm/sbin/
php-fpm
  • 查看编译参数
[root@localhost php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -i | grep -i configure
[19-Dec-2017 22:10:30] NOTICE: PHP message: PHP Warning:  Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
Configure Command =>  './configure'  '--prefix=/usr/local/php-fpm' '--with-config-file-path=/usr/local/php-fpm/etc' '--enable-fpm' '--with-fpm-user=php-fpm' '--with-fpm-group=php-fpm' '--with-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-pdo-mysql=/usr/local/mysql/' '--with-mysql-sock=/tmp/mysql.sock' '--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--with-mcrypt' '--enable-soap' '--enable-gd-native-ttf' '--enable-ftp' '--enable-mbstring' '--enable-exif' '--with-pear' '--with-curl' '--with-openssl'
  • 查看php的模块
[root@localhost ~]# /usr/local/php-fpm/sbin/php-fpm -m
[PHP Modules]
cgi-fcgi
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
...
  • 检查php-fpm配置文件语法
[root@localhost ~]# /usr/local/php-fpm/sbin/php-fpm -t
[02-Jan-2018 21:43:03] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

安装过程报错提示及解决方法

  • "error: xml2-config not found."
yum install -y libxml2-devel
  • "error: Cannot find OpenSSL's <evp.h>"
yum install -y openssl-devel
  • "error: Please reinstall the libcurl distribution"
yum install -y libcurl-devel
  • "error: jpeglib.h not found"
yum install -y libjpeg-devel
  • "error: png.h not found"
yum install -y libpng-devel
  • "error: freetype-config not found"
yum install -y freetype-devel
  • "error: mcrypt.h not found"
yum install -y epel-release
yum install -y libmcrypt-devel

Nginx介绍

官网 nginx.org,由俄罗斯程序员开发的一种web服务器,小巧而强大。Ningx在处理静态文件方便较apache更好,被更多国内外网站也采用。Ningx可以通过外接模块来扩展功能,这点与apache一样。

Ningx不仅可以应用于web服务,同样可以用于反向代理服务以及负载均衡。

Nginx是一个开源的软件,淘宝基于其开发的Tengine,在使用上与其一致,其内的服务名、配置文件等都一致,不同的是它添加了一些定制化的模块,在安全限速方面的表现很好,另外还支持对js、css合并功能。

openresty:一个由Nginx核心+lua相关组件和模块组成的支持lua的高性能web容器。

Nginx安装

当前最新稳定版:http://nginx.org/download/nginx-1.12.2.tar.gz

  • 下载并解压
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
--2017-12-24 22:22:26--  http://nginx.org/download/nginx-1.12.2.tar.gz
正在解析主机 nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
正在连接 nginx.org (nginx.org)|206.251.255.63|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:981687 (959K) [application/octet-stream]
正在保存至: “nginx-1.12.2.tar.gz”100%[============================>] 981,687     74.1KB/s 用时 9.3s   2017-12-24 22:22:36 (103 KB/s) - 已保存 “nginx-1.12.2.tar.gz” [981687/981687])[root@localhost src]# tar zxf nginx-1.12.2.tar.gz
  • 编译、安装
[root@localhost src]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@localhost src]# make && make install
  • 创建启动脚本
[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{echo -n $"Starting $prog: "mkdir -p /dev/shm/nginx_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL=$?echoreturn $RETVAL
}
stop()
{echo -n $"Stopping $prog: "killproc -p $NGINX_PID $NGINX_SBIN -TERMrm -rf /dev/shm/nginx_tempRETVAL=$?echoreturn $RETVAL
}
reload()
{echo -n $"Reloading $prog: "killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL=$?echoreturn $RETVAL
}
restart()
{stopstart
}
configtest()
{$NGINX_SBIN -c $NGINX_CONF -treturn 0
}
case "$1" instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo $"Usage: $0 {start|stop|reload|restart|configtest}"RETVAL=1
esac
exit $RETVAL
  • 更改配置文件权限并开机启动
[root@localhost nginx-1.12.2]# chmod 755 /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# chkconfig nginx on# 如果无法直接使用nginx命令,则表示/etc/init.d不在PATH变量内,可以通过修改/etc/profile来添加
  • 修改配置文件
[root@localhost nginx-1.12.2]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{use epoll;worker_connections 6000;
}
http
{include mime.types;default_type application/octet-stream;server_names_hash_bucket_size 3526;server_names_hash_max_size 4096;log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'' $host "$request_uri" $status'' "$http_referer" "$http_user_agent"';sendfile on;tcp_nopush on;keepalive_timeout 30;client_header_timeout 3m;client_body_timeout 3m;send_timeout 3m;connection_pool_size 256;client_header_buffer_size 1k;large_client_header_buffers 8 4k;request_pool_size 4k;output_buffers 4 32k;postpone_output 1460;client_max_body_size 10m;client_body_buffer_size 256k;client_body_temp_path /usr/local/nginx/client_body_temp;proxy_temp_path /usr/local/nginx/proxy_temp;fastcgi_temp_path /usr/local/nginx/fastcgi_temp;fastcgi_intercept_errors on;tcp_nodelay on;gzip on;gzip_min_length 1k;gzip_buffers 4 8k;gzip_comp_level 5;gzip_http_version 1.1;gzip_types text/plain application/x-javascript text/css text/htm application/xml;server{listen 80;server_name localhost;index index.html index.htm index.php;root /usr/local/nginx/html;location ~ \.php$ {include fastcgi_params;fastcgi_pass unix:/tmp/php-fcgi.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;}    }
}
  • nginx配置文件检测
[root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  • 启动服务
[root@localhost nginx-1.12.2]# /etc/init.d/nginx start
  • 查看是否启动成功
[root@localhost ~]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  确定  ][root@localhost ~]# ps aux | grep nginx
root       1302  0.0  0.0  20500   628 ?        Ss   21:40   0:00nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     1307  0.0  0.2  22944  3464 ?        S    21:40   0:00nginx: worker process
nobody     1308  0.0  0.2  22944  3212 ?        S    21:40   0:00nginx: worker process
root       2386  0.0  0.0 112680   976 pts/0    R+   21:42   0:00 grep --color=auto nginx

可能的错误

在配置自定义启动脚本之前如果使用PREFIX/sbin/nginx启动了脚本,那么可能出现如下错误:

[root@localhost nginx-1.12.2]# nginx start
Starting nginx (via systemctl):  Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.[失败]

解决方法是使用PREFIX/sbin/nginx -s stop先关闭nginx进程,然后使用自定义脚本启动即可。


转载于:https://my.oschina.net/LuCastiel/blog/1600842

LNMP架构之环境搭建相关推荐

  1. 14天学会安卓开发(第一天)Android架构与环境搭建

    14天学会安卓开发 作者:神秘的N (英文名  corder_raine) 联系方式:369428455(反馈) 交流群 :284552167(示例,原文档下载) 版权为作者所有,如有转载请注明出处 ...

  2. LNMP架构介绍与搭建

    笔记内容: 12.1 LNMP架构介绍 12.2 MySQL安装 12.3/12.4 PHP安装 12.5 Nginx介绍 笔记日期:2017.10.18 <br> 12.1 LNMP架构 ...

  3. LNMP架构和论坛搭建以及一键部署

    文章目录 数据流向 一.Nginx服务安装 1.关闭防火墙 2.将所需软件包拖入/opt目录下 3.安装依赖包 4.创建运行用户.组 5.配置软件模块 6.编译安装Nginx 7.优化配置文件路径,便 ...

  4. spring源码深度解析—Spring的整体架构和环境搭建

    概述 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的Java 开发框 ...

  5. 关于JUnit5 你必须知道的(一) JUnit5架构和环境搭建

    由于工作中都是使用maven来构建项目,所以下面有关环境搭建都是使用的maven(IDE使用的idea) 凡是开发对于JUnit这个框架一定都不陌生,JUnit 作为一个单元测试框架已经诞生了数十年. ...

  6. android入门之系统架构和环境搭建

    1.android背景 android起源 android系统是由安迪鲁宾团队开发的,最初用于数码相机,2005.08被google收购.(真是抱了一个好大腿)在接下来的几年中,android的发展简 ...

  7. 软件测试b s环境如何配置,B/S架构测试环境搭建_DB2篇(Win32系统)

    前言:前一篇分享了Oracle环境下的环境搭建和恢复,这一篇分享下DB2数据库的环境搭建,欢迎拍砖. 一.搭建测试环境: (1)新建数据库,DB2安装完成之后,在开始菜单中查看对应的信息,步骤是&qu ...

  8. Hadoop-HA高可用架构分布式环境搭建教程

    Hadoop-HA完全分布式系统搭建 为什么需要搭建HA版本 在hadoop1.x的架构中,是存在单点故障的,因为namenode节点只有一个,而namenode又是存储元数据的主节点,所以只要nam ...

  9. LNMP架构安装及搭建Discuz论坛

    文章目录 一.LNMP概述 1.LNMP 2.Nginx 3.MySQL 4.PHP 5.LNMP工作原理 二.安装Nginx服务 1.关闭防火墙及SElinux 2.将nginx软件包拖入到/opt ...

最新文章

  1. Linux内核链表交换节点,[笔记]Linux内核链表:结点的插入、删除以及链表的遍历...
  2. 浅析堡垒机概念及工作原理(转)
  3. 在浙学计算机基础2020答案,浙江大学2020年硕士研究生复试分数线的基本要求
  4. Android本地存储键值对,flutter本地存储键值对简单数据(相当于web的localstorage) 代码实现...
  5. Django从理论到实战(part55)--将网站上传到GitHub
  6. 二元函数泰勒公式例题_高等数学入门——二元函数可微性的判断方法总结
  7. swift5自定义构造函数和自定义变量报required init?(coder: NSCoder) { fatalError(“init(coder:) has not bee错问题
  8. 使用Java EE 8中的反应式API加速服务
  9. 树状数组的区间修改+查询
  10. 计算机基础应用的培养活动记录,小学少年宫计算机兴趣小组活动记录表
  11. 一个XP使用者眼中的Windows 7
  12. 重磅长文!先进院李骁健等人:在体神经界面技术的发展-从小到大规模记录
  13. 计算机 英语简历,计算机毕业生英语简历范文
  14. 笔记本电脑计计算机硬盘分区,笔记本电脑如何分区,小编教你笔记本电脑如何分区...
  15. 基于SSH会员积分消费管理系统
  16. selenium firefox 启动报错 Timed out wating 45 seconds for firefox to start
  17. 两个div右侧固定,左侧自适应屏幕
  18. 水果店从哪里进货便宜,水果店都有哪些进货渠道
  19. VS6 sp6补丁下载 [防VC6卡死]
  20. 慢啃《编程珠玑》【持续更新ing……】

热门文章

  1. Hadoop - MapReduce
  2. 两个命令把 Vim 打造成 Python IDE
  3. 鸟哥的Linux私房菜(基础篇)- 第十九章、认识与分析登录文件
  4. 0330 第九次课:软件包安装及卸载
  5. Git与GitHub的使用
  6. [译文]Domain Driven Design Reference(五)—— 为战略设计的上下文映
  7. React系列---React+Redux工程目录结构划分
  8. nginx 同一个IP上配置多个HTTPS主机
  9. memcached与spring集成
  10. JQuery与CSS相结合的下拉框