千家信息网

LNMP环境一键安装

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,基础概念LAMP:Linux-Apache-Mysql-Php稳定、同步、客户体验度好、资源占用率高--访问量小LNMP: Linux-Nginx-Mysql-Php异步处理、负载均衡、并发访问量大、
千家信息网最后更新 2025年02月01日LNMP环境一键安装
  • 基础概念

LAMP:Linux-Apache-Mysql-Php

稳定、同步、客户体验度好、资源占用率高--访问量小

LNMP: Linux-Nginx-Mysql-Php

异步处理、负载均衡、并发访问量大、利用率高--访问量逐步增加加

LNMPA: Linux-Apache-Nginx-Mysql-Php -

前端niginx,后端apache--访问量大、运行稳定

Nginx :轻量的异步Web服务器/反向服务器代理及电子邮件(IMCP/POP3)代理服务器。

Nginx的处理连接方式和apache的模拟类似,nginx通过主进程master产生多个子进程worker,然后每个worker产生很多线程,由线程处理具体的要求,因为异步处理,故速度特别快!

Nginx与apache、lighttp性能综合对比,如下图:


  • 系统需求

unix为内核的系统版本:Centos/RHEL/Fedora/Debian/Ubuntu

需要3GB以上的硬盘空间

MySQL 5.6MaiiaDB 10必须1G以上的内存

SElinux必须关

Linux下严格区分大小写,输入命令需注意

需要yum源正常使用

  • 安装步骤

1、下载并安装LNMP一键安装包:

#tar -xvf lnmp1.2-full.tar.gz

#cd lnmp1.2-full

#./install.sh lnmp | lamp | lnmpa

安装失败想卸载,执行./uninstall进行卸载

使用网络yum源一键安装LNMP命令

安装LNMP执行:wget -c http://soft.vpser.net/lnmp/lnmp1.2-full.tar.gz &&tar xvf lnmp1.2-full.tar.gz && cd lnmp1.2-full && ./install.shlnmp
如需要安装LNMPA或LAMP,将./install.sh 后面的参数替换为lnmpalamp即可。

按上述命令执行后,会出现如下提示:

需要设置MySQL的root密码(不输入直接回车将会设置为root),输入后回车进入下一步,如下图所示:

这里需要确认是否启用MySQL InnoDB,如果不确定是否启用可以输入 y ,输入 y 表示启用,输入 n 表示不启用。默认为y 启用,输入后回车进入下一步,选择MySQL版本:
输入MySQL或MariaDB版本的序号,回车进入下一步,选择PHP版本:

输入PHP版本的序号,回车进入下一步,选择是否安装内存优化:

可以选择不安装、Jemalloc或TCmalloc,输入对应序号回车。

提示"Press any keyto install...or Press Ctrl+c to cancel"后,按回车键确认开始安装。

LNMP脚本就会自动安装编译Nginx、MySQL、PHP、phpMyAdmin、ZendOptimizer这几个软件。

3、安装完成
如果显示Nginx: OK,MySQL: OK,PHP: OK

并且Nginx、MySQL、PHP都是running,80和3306端口都存在,并Install lnmp V1.2 completed!enjoy it.的话,说明已经安装成功。
4、安装失败

如果出现类似上图的提示,则表明安装失败,说明没有安装成功!!

  • 相关软件安装目录

Nginx主目录 /usr/local/nginx

Nginx日志目录:/home/wwwlogs

Mysql /usr/local/mysql

Mysql数据库 /usr/local/mysql/var/

PHP /usr/local/php

PHPMyadmin /home/wwwroot/default/phpmyadmin

默认网站目录 /home/wwwroot/default/

  • 状态管理命令

LNMP 状态管理: lnmp {start|stop|reload|restart|kill|status}
LNMP 各个程序状态管理: lnmp{nginx|mysql|mariadb|php-fpm|pureftpd} {start|stop|reload|restart|kill|status}

  • LNMP配置文件

1、 配置文件目录

Niginx主配置文件目录 /usr/local/nginx/conf/nginx.conf

Mysql配置文件目录 /etc/my.cnf

PHP配置文件目录 /usr/local/php/etc/php.ini

Php-fpm配置目录 /usr/local/php/etc/php-fpm.conf

2、 配置详解

#vi /usr/local/nginx/conf/nginx.conf

user www www;
#程序运行用户和组

worker_processes auto;

#启动进程,指定nginx启动的工作进程数量,建议按照cpu数目来指定,一般等于cpu数目

error_log /home/wwwlogs/nginx_error.log crit;

#全局错误日志

pid /usr/local/nginx/logs/nginx.pid;

#主进程PID保存文件

#Specifies the value for maximum file descriptors that can be opened by thisprocess.
worker_rlimit_nofile 51200;

#文件描述符数量

events
{
use epoll;

#使用epoll模型,对于2.6以上的内核,建议使用epoll模型以提高性能

worker_connections 51200;

#工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行每个进程允许的最多连接数, 理论上每台nginx服务器的最大连接数为worker_processes*worker_connections,具体还要看服务器的硬件、带宽等。

multi_accept on;

}

http

#整体环境配置--网站配置
{
include mime.types;
default_type application/octet-stream;
#设定mime类型,文件传送类型mime.type文件定义

server_names_hash_bucket_size 128;

#保存服务器名字的hash表大小

client_header_buffer_size32k;

#客户端请求头部缓冲区大小

large_client_header_buffers 4 32k;

#最大客户端头缓冲大小

client_max_body_size 50m;

#客户端最大上传文件大小(M)

sendfile on;

#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文 件,对于普通应用,必须设为 on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.

tcp_nopush on;

#这个是默认的,结果就是数据包不会马上传送去,等到数据包最大时,一次性的 传输出去,这样有助于解决网络堵塞。(只在sendfile on时有效)
keepalive_timeout 60;

#连接超时时间

tcp_nodelay on;

#禁用nagle算法,也即不缓存数据。有效解决网络阻塞

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size256k;

#fastcgi设置

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_http_version1.1;

gzip_comp_level2;

gzip_types text/plain application/javascript

application/x-javascripttext/javascript text/css application/xml application/xml+rss;

gzip_varyon;

gzip_proxied expired no-cache no-store private auth;

gzip_disable "MSIE [1-6]\.";

#limit_conn_zone$binary_remote_addr zone=perip:10m;

##Ifenable limit_conn_zone,add "limit_conn perip 10;" to server section.

server_tokens off;

#隐藏nginx版本号(curl -I 192.168.4.154可以查看,更加安全

#log format

log_format access '$remote_addr -$remote_user [$time_local]

"$request"'

'$status $body_bytes_sent"$http_referer" '

'"$http_user_agent"$http_x_forwarded_for';

#定义日志格式

server

{

listen 80 default_server;

#listen[::]:80 default_server ipv6only=on;

#监听80端口,WEB服务的监听设置,可以采用"IP地址:端口"形式

server_name www.lnmp.org;

#服务器名,可以写多个域名,用空格分隔

index index.html index.htm index.php;

#默认网页文件

root /home/wwwroot/default;

#网页主目录

#error_page 404 /404.html;

includeenable-php.conf;

location /nginx_status

{

stub_status on;

access_log off;

}

#开启status状态监测

location ~.*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

#静态文件处理,保存期30天

location ~.*\.(js|css)?$

{

expires 12h;

}

#js和css文件处理,保存期12小时

location ~/\.

{

deny all;

}

access_log /home/wwwlogs/access.log access;

#正确访问日志

}

includevhost/*.conf;

#vhost/下子配置文件生效

}

检查nginx配置文件语句错误

/usr/local/nginx/sbin/nginx-t

平滑重启nginx进程

1)pkill -HUP nginx

2)/usr/local/nginx/sbin/nginx-s reload

七、配置实验

1.nginx状态监控

location /nginx_status{

stub_status on;

access_log off;

}

#客户端访问网址:http://IP/nginx_status

注:"Active connections"表示当前的活动连接数;

"server accepts handled requests"表示已经处理的连接信息

三个数字依次表示已处理的连接数、成功的TCP握手次数、已处理的请求数

实验结果

2.nginx虚拟主机

使用Nginx搭建虚拟主机服务器时,每个虚拟WEB站点拥有独立的"server { }"配置段,各自监听的IP地址、端口号可以单独指定,当然网站名称也是不同的。

---基于域名的虚拟主机---

1. 两个站点www.benet.comwww.accp.com

192.168.1.1 www.benet.com

192.168.1.1 benet.com

192.168.1.1 www.accp.com

192.168.1.1 accp.com

修改Linux下和windows下的hosts文件,使其能正常解析域名

2. 为两个虚拟WEB主机分别建立网页根目录,并准备测试首页

mkdir -p /home/wwwroot/benet/

mkdir -p /home/wwwroot/accp/

3. 修改nginx.conf配置文件---> 建立域名虚拟主机

配置两个"server {}"区域,分别对应两个WEB站点,指定各自的网站名称、监听 地址、网站根目录、访问日志等信息。

在主配置文件当中添加两个虚拟主机的配置信息(添加在http { }范围内

server{

listen 80;

server_name www.benet.com benet.com;

charset utf-8;

access_log logs/benet.access.log;

location / {

root /home/wwwroot/benet;

index index.php index.html;

}

}

server{

listen 80;

server_name www.accp.com accp.com;

charset utf-8;

access_log logs/accp.access.log;

location / {

root /home/wwwroot/accp;

index index.html index.php;

}

}

4. 重新启动nginx,使用浏览器分别通过www.benet.com和www.accp.com访问 两个WEB站点

检测配置文件的正确性 /usr/local/nginx/sbin/nginx -t

重新加载配置文件 pkill -1 nginx | pkill -HUP nginx

5、测试结果

注:

检测配置文件的正确性 出现的错误

A、文件中表示的注释,必须每一行行首都要有 #

B、修改配置文件时注意不能多加一个字母

C、有时提示的错行位置不一定准确

3.列表页显示

server

{

listen 80;

server_name www.benet.com;

index index.html index.htm index.php;

root /home/wwwroot/benet;

autoindex on;

A、注释掉benet主页文件


B、在配置文件相应的虚拟机主机配置区域内添加 autoindexon;


保存,重新加载配置文件 pkill -1 nginx

此时,网页只是显示主页目录

4.rewrite 网站域名更新时,使用跳转功能告知用户域名的更改

location ~ \.php$ {

proxy_pass http://127.0.0.1;

}

例1:域名跳转

输入www.benet.com,跳转到www.accp.com

server{

listen 80;

server_name www.benet.com benet.com;

charset utf-8;

access_log logs/benet.access.log access;

location / {

root /home/wwwroot/benet;

index index.php index.html;

}

if ($http_host = www.benet.com) {

#判断网站域名的头部是否为www.benet.com,若是,则进行下面的跳转

#本句为判断语句,但不是shell中的if语句,所以结尾不使用 fi 结尾

rewrite (.*) http://www.accp.com permanent;

}

}

注意:此语句的添加位置,在server内部,location的外部

刷新配置文件 测试正常跳转

例2:文件跳转

server{

listen 80;

server_name www.accp.com accp.com;

charset utf-8;

access_log logs/benet.access.log access;

location / {

root /home/wwwroot/accp;

index index.html index.php;

}

rewrite inde /indexa.html last;


0