LNMP架构
Nginx安装配置
1.安装nginx所需的pcre库,让nginx支持url重写的rewrite功能 yum install pcre pcre-devel -y 2.安装openssl-devel模块,nginx需要支持https [root@web01 opt]# yum install openssl openssl-devel -y 2.1 安装gcc编译器 yum install gcc -y 3.下载nginx源码包 [root@web01 opt]# mkdir -p /home/chaoge/tools [root@web01 opt]# cd /home/chaoge/tools/ [root@web01 tools]# wget http://nginx.org/download/nginx-1.16.0.tar.gz [root@web01 tools]# echo $? 0 4.创建nginx普通用户 [root@web01 tools]# useradd nginx -u 1111 -s /sbin/nologin -M 5.开始解压缩编译nginx [root@web01 tools]# tar -zxf nginx-1.16.0.tar.gz [root@web01 nginx-1.16.0]# ./configure --user=nginx --group=nginx --prefix=/opt/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module [root@web01 nginx-1.16.0]# echo $? 0 [root@web01 nginx-1.16.0]# make && make install [root@web01 nginx-1.16.0]# echo $? 0 6.配置软连接,生产环境常用操作,便于运维、开发、测试使用,以及nginx以后的升级 [root@web01 nginx-1.16.0]# ln -s /opt/nginx-1.16.0/ /opt/nginx [root@web01 nginx-1.16.0]# ll /opt/ 总用量 0 lrwxrwxrwx 1 root root 18 3月 18 22:45 nginx -> /opt/nginx-1.16.0/ drwxr-xr-x 6 root root 54 3月 18 22:43 nginx-1.16.0 7.配置nginx环境变量 [root@web01 ~]# tail -1 /etc/profile export PATH="/opt/nginx/sbin:/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin" [root@web01 ~]# [root@web01 ~]# echo $PATH /opt/nginx/sbin:/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
安装Mysql
1.创建mysql用户 [root@web01 ~]# useradd -s /sbin/nologin mysql [root@web01 ~]# id mysql uid=1000(mysql) gid=1000(mysql) 组=1000(mysql) 2.下载mysql二进制软件包,提前配置好yum源,下载wget命令 [root@web01 ~]# yum install wget -y wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.re wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@web01 ~]# mkdir -p /home/mysql/tools [root@web01 ~]# cd /home/mysql/tools/ # 该mysql文件600M左右,下载时间看网速 [root@web01 tools]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
二进制方式安装mysql
1.解压并且移动mysql二进制软件包路径 [root@web01 tools]# tar -zvxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz [root@web01 tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /opt/mysql-5.7.26 2.生成软连接 [root@web01 opt]# ln -s /opt/mysql-5.7.26/ /opt/mysql [root@web01 opt]# ls -l /opt/ 总用量 0 lrwxrwxrwx 1 root root 18 3月 18 10:15 mysql -> /opt/mysql-5.7.26/ drwxr-xr-x 9 root root 129 3月 18 10:13 mysql-5.7.26 3.卸载centos7自带的mariadb库,防止冲突 [root@web01 mysql]# rpm -e --nodeps mariadb-libs 4.手动创建mysql配置文件 vim /etc/my.cnf [root@web01 mysql]# cat /etc/my.cnf [mysqld] basedir=/opt/mysql/ datadir=/opt/mysql/data socket=/tmp/mysql.sock server_id=1 port=3306 log_error=/opt/mysql/data/mysql_err.log [mysql] socket=/tmp/mysql.sock
初始化mysql数据库
1.卸载系统自带的centos7 mariadb-libs,且安装mysql的依赖环境 rpm -qa mariadb-libs #检查是否存在 [root@web01 mysql]# yum install libaio-devel -y 2.创建mysql数据文件夹且授权 [root@web01 mysql]# mkdir -p /opt/mysql/data [root@web01 mysql]# chown -R mysql.mysql /opt/mysql/ 3.初始化数据库 [root@web01 mysql]# /opt/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ # 参数解释 --user=mysql 指定用户 --basedir 指定mysql安装目录 --datadir=/opt/mysql/data 指定数据文件夹 --initialize-insecure 关闭mysql安全策略 --initialize 开启mysql安全模式
配置mysql客户端
1.配置mysql启动脚本,定义mysqld.service,脚本如下 [root@web01 mysql]# cat /etc/systemd/system/mysqld.service [Unit] Description=MySQL server by chaoge Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql ExecStart=/opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf LimitNOFILE=5000
启动mysql数据库
[root@web01 mysql]# systemctl start mysqld [root@web01 mysql]# systemctl enable mysqld Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /etc/systemd/system/mysqld.service. [root@web01 mysql]# systemctl status mysqld ● mysqld.service - MySQL server by chaoge Loaded: loaded (/etc/systemd/system/mysqld.service; disabled; vendor preset: disabled) Active: active (running) since 三 2020-03-18 11:30:01 EDT; 6s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Main PID: 5315 (mysqld) CGroup: /system.slice/mysqld.service └─5315 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf 3月 18 11:30:01 web01 systemd[1]: Started MySQL server by chaoge. 3月 18 11:30:01 web01 systemd[1]: Starting MySQL server by chaoge...
给mysql账户设置密码
[root@web01 ~]# mysqladmin -uroot password 'chaoge666' mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. # 再次登录,输入密码 [root@web01 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.26 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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>
CGI
CGI(Common Gateway Interface), 中文名是通用网关接口,用于HTTP服务器和其他机器通信的一种工具。
传统CGI程序性能较弱,因此每次HTTP服务器遇到动态程序的时候,都需要重启解析器来执行解析,之后的处理结果才会返回
给HTTP服务器。
这样在高并发场景下访问几乎是太差劲了,因此诞生了FastCGI。
FastCGI是一个可伸缩,高速的在HTTP服务器和动态脚本之间通信的接口(在Linux环境下,FastCGI接口就是socket,这个
socket可以是文件socket,也可以是IP socket,也就意味着本地通信,远程通信两种),主要优点是把动态语言和HTTP服务器
分离开。
多数主流的web服务器都支持FastCGI,如Apache,NGINX,Lighttpd等。
Fast-CGI接口采用C/S架构,可以将HTTP服务器和脚本解析服务器分离开。
当HTTP服务器遇见静态请求,直接返回,遇见动态请求转发给动态服务器,交给FastCGI去处理,实现动静分离,提升
服务器性能。
FastCGI部署(本地部署)
安装部署PHP程序所需的系统库,不要求必须安装,而是安装上之后,可以扩展php更多功能。
yum install gcc gcc-c++ make zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel \ freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y
默认yum源中缺少libiconv-devel软件包,需要编译安装,用于PHP的编码转换。
[root@web01 ~]# wget -P /home/chaoge/tools/ http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz [root@web01 ~]# cd /home/chaoge/tools/ [root@web01 tools]# ls libiconv-1.15.tar.gz nginx-1.16.0 nginx-1.16.0.tar.gz [root@web01 tools]# tar zxf libiconv-1.15.tar.gz [root@web01 libiconv-1.15]# cd libiconv-1.15 [root@web01 libiconv-1.15]# ./configure --prefix=/opt/libiconv [root@web01 libiconv-1.15]# make && make install
安装PHP(FastCGI形式)
1.下载获取php软件包 [root@web01 tools]# wget http://mirrors.sohu.com/php/php-7.3.5.tar.gz 2.解压缩php源码包,编译安装 [root@web01 tools]# tar -zxvf php-7.3.5.tar.gz [root@web01 tools]# cd php-7.3.5 [root@web01 php-7.3.5]# ./configure --prefix=/opt/php7.3.5 \ --enable-mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-iconv-dir=/opt/libiconv \ --with-freetype-dir \ --with-jpeg-dir \ --with-png-dir \ --with-zlib \ --with-libxml-dir=/usr \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --with-curl \ --enable-mbregex \ --enable-fpm \ --enable-mbstring \ --with-gd \ --with-openssl \ --with-mhash \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-soap \ --enable-short-tags \ --enable-static \ --with-xsl \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --enable-ftp \ --enable-opcache=no
部分参数解释
--prefix= 指定php安装路径 --enable-mysqlnd 使用php自带的mysql相关软件包 --with-fpm-user=nginx 指定PHP-FPM程序的用户是nginx,和nginx服务保持统一 --enable-fpm 激活php-fpm方式,以FastCGI形式运行php程序
3.在执行完编译脚本文件后,开始执行编译安装 [root@web01 php-7.3.5]# make && make install [root@web01 php-7.3.5]# $? 0
PHP配置文件
[root@web01 php-7.3.5]# ls php.ini* php.ini-development php.ini-production
俩配置文件,分别默认用于开发环境,生产环境,配置参数有所不同
# 可以用如下命令对比文件区别 [root@web01 php-7.3.5]# vimdiff php.ini-development php.ini-production 开发环境下开起了更多的日志、调试信息,生产环境该参数都关闭了
拷贝php配置文件到php默认目录,且改名
[root@web01 php-7.3.5]# cp php.ini-development /opt/php/lib/php.ini
FastCGI配置文件
1.默认FastCGI的配置文件路径 [root@web01 etc]# pwd /opt/php/etc [root@web01 etc]# ls pear.conf php-fpm.conf.default php-fpm.d 2.生成2个php-frpm的配置文件,先用默认配置,后续可以再后话 [root@web01 etc]# cp php-fpm.conf.default php-fpm.conf [root@web01 etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
启动PHP服务(FastCGI模式)
# 启动服务,并且检查状态 [root@web01 etc]# /opt/php/sbin/php-fpm [root@web01 etc]# netstat -tunlp|grep php tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 109647/php-fpm: mas
修改Nginx支持PHP
1.修改nginx配置文件,在最底行添加 包含文件参数,建议删除nginx.conf原有的server配置 [root@web01 conf]# vim /opt/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; include extra/01_www.conf; include extra/02_bbs.conf; include extra/03_blog.conf; include extra/04_status.conf; }
配置php的解析配置文件
[root@web01 conf]# mkdir extra [root@web01 conf]# vim extra/03_blog.conf [root@web01 conf]# [root@web01 conf]# [root@web01 conf]# cat extra/03_blog.conf server { listen 80; server_name blog.chaoge.com; location / { root html/blog; index index.html; } #添加有关php程序的解析 location ~ .*\.(php|php5)?$ { root html/blog; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } }
检查且启动Nginx
[root@web01 conf]# touch extra/01_www.conf [root@web01 conf]# touch extra/02_bbs.conf [root@web01 conf]# touch extra/04_status.conf [root@web01 conf]# nginx -t nginx: the configuration file /opt/nginx-1.16.0//conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx-1.16.0//conf/nginx.conf test is successful
测试
[root@web01 conf]# mkdir -p /opt/nginx/html/blog [root@web01 conf]# echo "<?php phpinfo(); ?>" > ../html/blog/test_info.php [root@web01 conf]#
测试PHP连接mysql
[root@web01 conf]# cat ../html/blog/test_mysql.php <?php $link_id=mysqli_connect('localhost','root','chaoge666') or mysql_error(); if($link_id){ echo "mysql successful by chaoge.\n"; }else { echo mysql_error(); } ?>
LNMP远程部署
https://xuchen.wang/archives/nginxphp.html 还有注意,修改php-fpm的启动地址,改为0.0.0.0:9000 配置文件夹在安装目录的etc/php-fpm.d/下面的已conf为后缀的文件,一般在安装时我们设置成了www.conf。 把 listen = 127.0.0.1:9000 后面的端口号9000换成你需要的端口,然后重启php
LNMP部署开源博客
环境准备
1.登录数据库创建用于存储数据库的wordpress库,存储博客数据 mysql> create database wordpress; Query OK, 1 row affected (0.02 sec) mysql> show databases like 'wordpress'; +----------------------+ | Database (wordpress) | +----------------------+ | wordpress | +----------------------+ 1 row in set (0.01 sec) 2.创建专用于wordpress的数据库用户 mysql> grant all on wordpress.* to wordpress@'localhost' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.01 sec) 3.刷新权限表 mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) 4.查询用户信息 mysql> select user,authentication_string,host from mysql.user; +---------------+-------------------------------------------+-----------+ | user | authentication_string | host | +---------------+-------------------------------------------+-----------+ | root | *83F7A15725AF362EF5EAFC16E1F3F97FDAB9B411 | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost | | wordpress | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | localhost | +---------------+-------------------------------------------+-----------+ 4 rows in set (0.00 sec) 5.nginx配置,选择blog虚拟主机配置,添加一个index.php参数 [root@web01 conf]# cat extra/03_blog.conf server { listen 80; server_name _; location / { root html/blog; index index.php index.html; #在这里新增 } #添加有关php程序的解析 location ~ .*\.(php|php5)?$ { root html/blog; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } }
wordpress博客程序准备
1.下载获取wordpress程序 wget https://wordpress.org/latest.zip wget https://wordpress.org/latest.tar.gz 2.超哥机器上的资料 [root@web01 conf]# ls /home/chaoge/tools/wordpress-5.3.2.* /home/chaoge/tools/wordpress-5.3.2.tar.gz /home/chaoge/tools/wordpress-5.3.2.zip 3.解压缩zip版代码 [root@web01 tools]# yum install unzip -y [root@web01 tools]# unzip wordpress-5.3.2.zip [root@web01 tools]# mv wordpress /opt/nginx/html/blog/ [root@web01 tools]# cd /opt/nginx/html/blog/ [root@web01 blog]# ls test_mysql.php wordpress 4.移动worpress代码到nginx目录下,且授权 [root@web01 blog]# mv wordpress/* . [root@web01 blog]# ls index.php wordpress wp-comments-post.php wp-includes wp-mail.php xmlrpc.php license.txt wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php readme.html wp-admin wp-content wp-load.php wp-signup.php test_mysql.php wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php [root@web01 blog]# [root@web01 blog]# [root@web01 blog]# chown -R nginx.nginx ../blog/ 5.最后一步,注意别忘记重启nginx nginx -s reload
访问安装wordpress