LNMP架构搭建
提前下载上传相关软件包
mysql的安装
此实验使用glibc安装
编写脚本文件
# vim mysql.sh
#!/bin/bash
tar -zxf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
useradd -r -s /sbin/nologin mysql
chown -R mysql.mysql /usr/local/mysql
cd /usr/local/mysql
yum remove mariadb-libs -y
scripts/mysql_install_db --user=mysql
cp support-files/mysql.server /etc/init.d/mysql
service mysql start
# 追加/usr/local/mysql/bin目录到环境变量
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile
# source mysql.sh
# mysql_secure_installation =>设置密码
添加mysql.service脚本,使用systemctl操纵
# vim /usr/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf
LimitNOFILE = 5000
PrivateTmp=false
[Install]
WantedBy=multi-user.target
注:
Unit代表单元,启动程序需要有单元信息
After代表在某些服务启动后在进行MySQL启动
Service代表服务,最核心配置
User + Group代表将来以哪个用户的身份运行此软件
ExecStart代表启动脚本调用哪个程序(systemctl start mysql.service)
LimitNOFILE代表限制文件大小
PrivateTmp代表是否把MySQL的运行程序放入私有空间,比如pid,socket文件
Install代表安装模式,代表把MySQL服务运行在何种模式下
添加开机启动
# systemctl enable mysql
安装nginx
首先安装依赖库
# mount /dev/sr0 /mnt
# yum -y install pcre-devel zlib-devel openssl-devel
编写安装脚本
# vim nginx.sh
#!/bin/bash
tar -zxf nginx-1.12.2.tar.gz
cd nginx-1.12.2
useradd -r -s /sbin/nologin www
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
make && make install
注:
--prefix:安装路径
--user:以哪个用户身份安装初始化
--group:以哪个用户组身份安装初始化
--with-*:代表安装哪些模块(默认情况下只安装基本模块)
# bash nginx.sh
原生方式启动
# cd /usr/local/nginx
# sbin/nginx -c /usr/local/nginx/conf/nginx.conf
检查80端口
# sbin/nginx -s start/stop/restart
添加nginx.service脚本,使用systemctl操纵
# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=Nginx Web Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
注:
Type=forking,代表后台运行
尝试使用systemctl操作.
安装PHP-FPM
安装依赖库
# yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel
# tar -zxf php-7.2.12.tar.gz
# cd php-7.2.12
# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
# make && make install
注:
--prefix:安装目录
--with-config-file-path:配置文件目录
--enable-fpm:开启php-fpm功能
--with-fpm-user:代表以哪个用户运行php-fpm
--with-fpm-group:代表以哪个用户组运行php-fpm
--with-mysqli:php-mysqli扩展
--with-pdo-mysql:php-pdo-mysql扩展
--with-*:项目所需的额外的扩展库(需要运维工程师安装,但是由程序猿提供文档)
php7.2:从php5.6开始,php链接mysql必须使用mysqli或pdo扩展。
修改nginx-fpm配置文件来管理nginx,默认配置文件已存在,但不能直接使用.
# cp /root/php-7.2.12/php.ini-development /usr/local/php/etc/php.ini
# cd /usr/local/php/etc目录
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# 把php-fpm添加到/etc/init.d目录下
# cp /root/php-7.2.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# service php-fpm start
# 查看9000端口的占用情况(Nginx中的PHP是以独立进程的方式运行,占用计算机的9000端口)
# ss -naltp |grep 9000
将php的bin目录添加到环境变量里
# echo 'export PATH=$PATH:/usr/local/php/bin' >> /etc/profile
# source /etc/profile
启动直接使用service
nginx关联php-fpm
nginx是轻量级的软件,默认不支持解析php代码,所以需要将php代码转发给php-fpm解析
# cd /usr/local/nginx
# grep -Ev '#|^$' conf/nginx.conf > nginx.conf => 去除#号与空白行
# mv conf/nginx.conf conf/nginx.conf.bak => 把nginx.conf备份
# mv nginx.conf conf/ => 移动nginx.conf到conf目录
# vim /usr/local/nginx/conf/nginx.conf
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 server {
# 监听端口
11 listen 80;
# 真实域名,localhost代表本机
12 server_name localhost;
# 项目目录
root html;
# 任意请求
13 location / {
14 # 默认首页(如果在html目录发现默认首页,直接运行)
15 index index.html index.htm;
16 }
# 添加PHP文件支持
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 当访问遇到错误时,返回50x.html页面给用户
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html {
19 root html;
20 }
21 }
22 }
nginx.conf文件,主要由三部分组成:
http {
server {}
server {}
}
一个nginx.conf对应一个http,代表http请求,所以一个页面只有一个http
一个http可以包含多个server区块,每个区块就是一个项目(网站)=> 虚拟主机
配置完以后要重载nginx
# systemctl reload nginx
检查9000端口