第二十课 LNMP
程序员文章站
2022-05-17 18:01:17
...
目录
- LNMP架构介绍
- MySQL安装
- PHP安装
- Nginx安装
- 验证 PHP 能否解析
1. LNMP架构介绍
其实就是四个大写字母的缩写,分别是 Linux + Nginx + MySQL + PHP,不同于Apache,LNMP中 Nginx 与 PHP 可以不必在一台机子上
1.1 LNMP 架构图
ps. 与 LAMP 不同,PHP 不再以 Apache 的 Module 的形式存在,而是独立出来成为单独的服务。
1.2 NGINX
Nginx是一款俄罗斯工程师开发的轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,其特点是占有内存少,并发能力强,中国使用 nginx 网站的企业有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx 官网
2 MySQL安装
3.1 下载、解压
- 将安装包下载到 /usr/local/src/ 中(300MB)
[root@LNMP ~]# cd /usr/local/src
[root@LNMP src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
- 解压缩,并放于正式安装目录
[root@LNMP src]# tar -xzvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
[root@LNMP src]# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
[root@LNMP src]# cd /usr/local/mysql
3.2 建立账户与目录
- 建立使用 MySQL 的账户
[root@LNMP mysql]# useradd mysql
- 建立存放数据的目录
[root@LNMP mysql]# mkdir /data/
3.3 安装
- 执行安装命令,若在最终输出中看到 两个 OK 则表示成功安装。(此处不再赘述依赖关系的解决过程)
[aaa@qq.com mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql # 只需提前创建 /data/即可,而 /data/mysql 会由该脚本自动建立
3.4 配置
- 将安装包中带有的 MySQL 配置文件复制到 /etc/ 下(CentOS 7 初始安装时就已经有 /etc/my.cnf 了,所以其实不替换也可以,只要保证有必要的配置信息)
## 在此我采用替换的方法,所以要提前备份一下原有的 /etc/my.cnf
[root@LNMP mysql]# mv /etc/my.cnf /etc/my.cnf.bak
[root@LNMP mysql]# cp support-files/my-default.cnf /etc/my.cnf
- 修改配置文件内容
[aaa@qq.com mysql]# vi /etc/my.cnf
## 配置如下几行(配置文件中本就有,但被注释了)
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
socket = /tmp/mysql.sock
- 将 MySQL 服务的启动程序复制到 /etc/init.d/mysqld 下
[root@LNMP mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@LNMP mysql]# chmod 755 /etc/init.d/mysqld
- 修改启动程序内容
$ vi /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
## 该行中增加 --basedir="$basedir"
$bindir/mysqld_safe --datadir="$datadir" --basedir="$basedir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
- 将 mysqld 添加至开机启动
[root@LNMP mysql]# chkconfig --add mysqld
3.5 MySQL 的启动
[root@LNMP mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
.. SUCCESS!
## 也可以采用以下命令启动 MySQL
[root@LNMP mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql
3.6 启动情况查看
## 看看是否按照我们的设置正确开启了服务,如 basedir、datadir、port、user 等
[root@LNMP mysql]# ps aux | grep mysql
root 41896 0.0 0.0 113304 1648 pts/0 S 6月26 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --basedir=/usr/local/mysql --pid-file=/data/mysql/localhost.localdomain.pid
mysql 42074 3.4 11.7 1300828 455004 pts/0 Sl 6月26 0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root 42103 0.0 0.0 112720 972 pts/0 S+ 00:00 0:00 grep --color=auto mysql
## 看看端口情况
[root@LNMP mysql]# netstat -ltnp | grep mysql
tcp6 0 0 :::3306 :::* LISTEN 42074/mysqld
3 PHP安装
6.1 下载、解压
[root@LNMP src]# cd /usr/local/src/
[root@LNMP src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
[root@LNMP src]# tar -xjvf php-5.6.30.tar.gz
[root@LNMP src]# cd php-5.6.30
6.2 创建使用账户
[root@LNMP mysql-5.6.30]# useradd -s /sbin/nologin php-fpm
6.3 安装
[aaa@qq.com mysql-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
[aaa@qq.com mysql-5.6.30] make && make install
6.4 php常用命令
# 查看配置信息
$ /usr/local/php-fpm/bin/php -i
$ /usr/local/php-fpm/sbin/php-fpm -i
# 查看模块信息
$ /usr/local/php-fpm/bin/php -m
$ /usr/local/php-fpm/sbin/php-fpm -m
# 配置文件语法检查
$ /usr/local/php-fpm/sbin/php-fpm -t
6.4 配置
- 复制 php 配置文件
[root@LNMP php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
- 修改 /usr/local/php/etc/php-fpm.conf
vim /usr/local/php/etc/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
listen.mode = 666
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
- 复制启动文件
[root@LNMP php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@LNMP php-5.6.30]# chmod 755 /etc/init.d/php-fpm
- 设置开机启动
[root@LNMP php-5.6.30]# chkconfig --add php-fpm
[root@LNMP php-5.6.30]# chkconfig php-fpm on
6.5 启动 php-fpm
[root@LNMP php-5.6.30]# /etc/init.d/php-fpm start
[root@LNMP php-5.6.30]# ps aux | grep php-fpm
root 34161 0.0 0.1 227292 4944 ? Ss 10:53 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 34162 0.0 0.1 227292 4708 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34163 0.0 0.1 227292 4708 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34164 0.0 0.1 227292 4712 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34165 0.0 0.1 227292 4712 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34166 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34167 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34168 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34169 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34170 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34171 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34172 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34173 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34174 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34175 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34176 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34177 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34178 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34179 0.0 0.1 227292 4716 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34180 0.0 0.1 227292 4720 ? S 10:53 0:00 php-fpm: pool www
php-fpm 34181 0.0 0.1 227292 4720 ? S 10:53 0:00 php-fpm: pool www
root 34185 0.0 0.0 112704 956 pts/0 S+ 10:53 0:00 grep --color=auto php-fpm
4 Nginx安装
4.1 下载、解压
[root@LNMP php-5.6.30]# cd /usr/local/src
[root@LNMP src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@LNMP src]# tar xzvf nginx-1.12.2.tar.gz
[root@LNMP src]# cd nginx-1.12.2
4.2 安装
## ./configure
[root@LNMP nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
## make && make install
[root@LNMP nginx-1.12.2]# make && make install
4.3 配置
- 编辑启动文件
[aaa@qq.com nginx]# 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_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
[aaa@qq.com nginx-1.12.2]# chmod 755 /etc/init.d/nginx
- 修改 nginx 配置文件
[aaa@qq.com nginx-1.12.2]# cd /usr/local/nginx/conf/
[aaa@qq.com conf]# mv nginx.conf nginx.conf.bak
[aaa@qq.com conf]# vim 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;
}
}
}
- 配置文件语法检查
[aaa@qq.com 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@LNMP nginx-1.12.2]# chkconfig --add nginx
[root@LNMP nginx-1.12.2]# chkconfig nginx on
- 开启 nginx
## 开启
[root@LNMP nginx-1.12.2]# /etc/init.d/nginx start
Reloading systemd: [ OK ]
Starting nginx (via systemctl): [ OK ]
## 查看服务
[root@LNMP nginx-1.12.2]# netstat -tlnp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 39185/nginx: master
6. 测试能否 PHP 解析
6.1 建立 PHP 文件
[aaa@qq.com html]# vim /usr/local/nginx/html/test.php
<?php
phpinfo();
?>
6.2 iptables 放通
[root@LNMP html]# iptables -F
6.2 浏览器进行访问,显示如下页面则为成功
上一篇: 设置.htaccess 禁止访问网站根上面的某个php文件
下一篇: Nginx 环境搭建