LNMP,表示Linux、Nginx、MySQL、PHP的集合服务器环境,它是基于CentOS/Debian编写的Nginx、PHP、MySQL、phpMyAdmin、eAccelerator一键安装包。可以在云服务器、VPS、独立主机上轻松的安装LNMP生产环境。
Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,它的发音为 “engine X”, 是一个高性能的HTTP和反向代理服务器。在高并发连接的情况下,Nginx是Apache服务器的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。Nginx 0.8.46 + PHP 5.2.14 (FastCGI) 可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍。
本文原文转自米扑博客:腾讯云实验室:搭建 LNMP 环境
准备工作
操作系统环境
系统: CentOS release 6.8 (Final)
内核:Linux VM_124_53_centos 2.6.32-642.6.2.el6.x86_64
CPU:单核
内存:1GB
系统参数
1 2 3 4 5 6 7 8 9 | # uname -a Linux VM_124_53_centos 2.6.32-642.6.2.el6.x86_64
#1 SMP Wed Oct 26 06:52:09 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux # free
totalused
free
sharedbufferscached Mem:102012876320425692438843516537620 -/+ buffers
/cache
:182068838060 Swap:000 # whoami root |
安装 lsb_release
yum -y install redhat-lsb
查看系统版本
# lsb_release -a
LSB Version::base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description:CentOS release 6.8 (Final)
Release:6.8
Codename:Final
top 命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | top
- 10:48:57 up1:26,1 user,load average: 0.29, 0.45, 0.24 Tasks:94 total,1 running,93 sleeping,0 stopped,0 zombie Cpu(s):0.0%us,0.3%sy,0.0%ni, 99.7%
id
,0.0%wa,0.0%hi,0.0%si,0.0%stMem:1020128k total,763180k used,256948k
free
,43548k buffers Swap:0k total,0k used,0k
free
,537724k cached
PID USERPRNIVIRTRESSHR S %CPU %MEMTIME+COMMAND
1 root200 19364 1544 1228 S0.00.20:00.69 init
2 root200000 S0.00.00:00.00 kthreadd
3 rootRT0000 S0.00.00:00.00 migration
/0
4 root200000 S0.00.00:00.02 ksoftirqd
/0
5 rootRT0000 S0.00.00:00.00 stopper
/0
6 rootRT0000 S0.00.00:00.00 watchdog
/0
7 root200000 S0.00.00:00.16 events
/0
8 root200000 S0.00.00:00.00 events
/0
9 root200000 S0.00.00:00.00 events_long
/0
10 root200000 S0.00.00:00.00 events_power_ef
11 root200000 S0.00.00:00.00 cgroup
12 root200000 S0.00.00:00.00 khelper |
一、搭建 Nginx 静态服务器
1. 安装 Nginx
yum install nginx -y
2. 修改 /etc/nginx/conf.d/default.conf
去除对 IPv6 地址的监听,可参考下面的代码示例:
vim/etc/nginx/conf.d/default.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | server {
listen80 default_server;
# listen[::]:80 default_server;
server_name_;
root
/usr/share/nginx/html
;
# Load configuration files for the default server block.
include
/etc/nginx/default
.d/*.conf;
location / {
}
error_page 404
/404
.html;
location =
/40x
.html {
}
error_page 500 502 503 504
/50x
.html;
location =
/50x
.html {
} } |
3. 启动 Nginx
nginx
【腾讯云实验室(搭建 LNMP 环境)】
4. 查看网页
可访问实验机器外网 HTTP 服务(http://119.29.116.97)来确认是否已经安装成功。
![腾讯云实验室(搭建 LNMP 环境)](https://img.it610.com/image/info8/7d460c00bfd443b2a1b3176b1519bda2.jpg)
文章图片
5. 将 Nginx 设置为开机启动
chkconfig nginx on
6. 查看开机启动的所有进程(chkconfig)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # chkconfig abrt-ccpp0:off1:off2:off3:on4:off5:on6:off abrtd0:off1:off2:off3:on4:off5:on6:off acpid0:off1:off2:on3:on4:on5:on6:off atd0:off1:off2:off3:on4:on5:on6:off auditd0:off1:off2:on3:on4:on5:on6:off blk-availability0:off1:on2:on3:on4:on5:on6:off bootlocal0:off1:off2:off3:on4:off5:off6:off crond0:off1:off2:on3:on4:on5:on6:off cups0:off1:off2:on3:on4:on5:on6:off htcacheclean0:off1:off2:off3:off4:off5:off6:off httpd0:off1:off2:off3:off4:off5:off6:off ip6tables0:off1:off2:off3:off4:off5:off6:off iptables0:off1:off2:on3:on4:on5:on6:off irqbalance0:off1:off2:off3:off4:off5:off6:off kdump0:off1:off2:on3:on4:on5:on6:off lvm2-monitor0:off1:on2:on3:on4:on5:on6:off mdmonitor0:off1:off2:on3:on4:on5:on6:off messagebus0:off1:off2:on3:on4:on5:on6:off mysqld0:off1:off2:on3:on4:on5:on6:off netconsole0:off1:off2:off3:off4:off5:off6:off netfs0:off1:off2:off3:on4:on5:on6:off network0:off1:off2:on3:on4:on5:on6:off nfs-rdma0:off1:off2:off3:off4:off5:off6:off nginx0:off1:off2:off3:off4:off5:off6:off ntpd0:off1:off2:off3:off4:off5:off6:off ntpdate0:off1:off2:off3:off4:off5:off6:off php-fpm0:off1:off2:on3:on4:on5:on6:off portreserve0:off1:off2:on3:on4:on5:on6:off postfix0:off1:off2:on3:on4:on5:on6:off psacct0:off1:off2:off3:off4:off5:off6:off quota_nld0:off1:off2:off3:off4:off5:off6:off rdisc0:off1:off2:off3:off4:off5:off6:off rdma0:off1:off2:off3:off4:off5:off6:off restorecond0:off1:off2:off3:off4:off5:off6:off rsyslog0:off1:off2:on3:on4:on5:on6:off saslauthd0:off1:off2:off3:off4:off5:off6:off sshd0:off1:off2:on3:on4:on5:on6:off udev-post0:off1:on2:on3:on4:on5:on6:off |
二、安装 MySQL 数据库服务
1. 安装 MySQL
yum -y install mysql-server
CentOS 安装配置MariaDB
MariaDB 官方安装教程:MariaDB 10.2 CentOS repository list
CentOS 7vim /etc/yum.repos.d/MariaDB-10.2.repo
1 2 3 4 5 | [mariadb] name = MariaDB baseurl = http:
//yum
.mariadb.org
/10
.2
/centos7-amd64 gpgkey=https:
//yum
.mariadb.org
/RPM-GPG-KEY-MariaDB gpgcheck=1 |
# yum remove MariaDB-server MariaDB-client
yum -y install MariaDB-client MariaDB-server MariaDB-devel
2. 启动 MySQL 服务
service mysqld restart
3. 设置 MySQL 账户 root 密码
/usr/bin/mysqladmin -u root password 'mimvp-demo'
4. 登录MySQL查看数据库
mysql -u root -p'mimvp-demo'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # mysql -u root -p'mimvp-demo' Welcome to the MySQL monitor.Commands end with ;
or \g. Your MySQL connection
id
is 40 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and
/or
its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and
/or
its affiliates. Other names may be trademarks of their respective owners. Type
'help;
'
or
'\h'
for
help. Type
'\c'
to
clear
the current input statement. mysql> mysql> mysql> show databases;
+--------------------+ | Database| +--------------------+ | information_schema | | mysql| |
test
| +--------------------+ 3 rows
in
set
(0.28 sec) |
5. 将 MySQL 设置为开机自动启动:
chkconfig mysqld on
三、搭建 PHP 环境
1. 安装 PHP
yum -y install php php-fpm php-mysql
注:CentOS 6 默认已经安装了 PHP-FPM 及 PHP-MYSQL,下面命令执行的可能会提示已经安装。
2. 安装之后,启动 PHP-FPM 进程
service php-fpm start
3. 查看启动的端口号
netstat -nlpt | grep php-fpm
启动之后,可以使用下面的命令查看 PHP-FPM 进程监听哪个端口(默认监听9000端口)
netstat -nlpt | grep php-fpm
1 2 | # netstat -nlpt | grep php-fpm tcp00 127.0.0.1:90000.0.0.0:*LISTEN13894
/php-fpm |
4. 把 PHP-FPM 也设置成开机自动启动:
chkconfig php-fpm on
四、配置 Nginx 并运行 PHP 程序
1. 配置 Nginx
在 /etc/nginx/conf.d 目录中新建一个名为 php.conf 的文件,并配置 Nginx 端口 ,配置示例如下:
vim /etc/nginx/conf.d/php.conf
1 2 3 4 5 6 7 8 9 10 11 | server {
listen 8000;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .php$ {
root
/usr/share/php
;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
includefastcgi_params;
} } |
2. 重启 nginx 服务
service nginx restart
这时候,我们就可以在/usr/share/php 目录下新建一个 info.php 文件来检查 php 是否安装成功了,文件内容参考如下:
vim /usr/share/php/info.php
1 2 3 4 | # cat /usr/share/php/info.php |
3. 访问网页
访问 http://119.29.116.97:8000/info.php 可浏览到我们刚刚创建的 info.php 页面了
![腾讯云实验室(搭建 LNMP 环境)](https://img.it610.com/image/info8/3b4a2343f66c488dbd512a65f5a6d66e.jpg)
文章图片
![腾讯云实验室(搭建 LNMP 环境)](https://img.it610.com/image/info8/25c4d6a2358e4d3c9925a063fab5821b.jpg)
文章图片
五、Nginx 应用实例
米扑科技的大多产品,建立在 LNMP 和 LAMP 服务器框架上,示例如下:
米扑科技: http://mimvp.com(LNMP + LAMP)
米扑代理: http://proxy.mimvp.com(LNMP)
米扑支付: http://pay.mimvp.com(LNMP)
米扑财富: http://money.mimvp.com(LNMP)
米扑博客: https://blog.mimvp.com(LAMP)
米扑论坛: http://forum.mimvp.com(LAMP)
推荐阅读
- Linux|109 个实用 shell 脚本
- linux笔记|linux 常用命令汇总(面向面试)
- Linux|Linux--网络基础
- linux|apt update和apt upgrade命令 - 有什么区别()
- linux|2022年云原生趋势
- Go|Docker后端部署详解(Go+Nginx)
- 开源生态|GPL、MIT、Apache...开发者如何选择开源协议(一文讲清根本区别)
- GitHub|7 款可替代 top 命令的工具