文章目录
- 1.部署LNMP
- LNMP架构拓扑图
- 1.安装Nginx服务😊
- 2.安装MySQL服务🤣
- 3.安装配置PHP解析环境😂
- 2.部署Discuz社区论坛Web应用
- 3. 部署WordPress社区论坛Web应用
- 4. fpm参数优化
1.部署LNMP
LNMP架构拓扑图
1.安装Nginx服务😊
###关闭和禁止防火墙开机自启功能
[root@clr ~]# systemctl stop firewalld
[root@clr ~]# systemctl disable firewalld
[root@clr ~]# setenforce 0
(1)安装依赖包
[root@clr /opt]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
(2)创建运行用户
[root@clr /opt]# useradd -M -s /sbin/nologin nginx
(3)编译安装
[root@clr ~]# cd /opt
[root@clr /opt]# rz -E
[root@clr /opt]# ls
nginx-1.24.0.tar.gz rh
[root@clr /opt]# tar xf nginx-1.24.0.tar.gz
[root@clr /opt]# cd nginx-1.24.0/
[root@clr /opt/nginx-1.24.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@clr /opt/nginx-1.24.0]# make -j2 && make install
(4)优化路径
[root@clr /opt/nginx-1.24.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
(5)添加Nginx系统服务
[root@clr /opt/nginx-1.24.0]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
(6)将nginx服务加入到systemd系统文件中,启动nginx服务并设置为开机自启
[root@clr /opt/nginx-1.24.0]# chmod 754 /lib/systemd/system/nginx.service
[root@clr /opt/nginx-1.24.0]# systemctl start nginx.service
[root@clr /opt/nginx-1.24.0]# systemctl enable nginx.service
[root@clr /opt/nginx-1.24.0]# netstat -lntp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 62585/nginx: master
[root@clr /opt/nginx-1.24.0]# ps aux | grep nginx
root 62585 0.0 0.0 20584 616 ? Ss 12:17 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 62586 0.0 0.0 23112 1376 ? S 12:17 0:00 nginx: worker process
root 62629 0.0 0.0 112824 980 pts/1 S+ 12:19 0:00 grep --color=auto nginx
2.安装MySQL服务🤣
(1)安装Mysql环境依赖包
[root@clr /opt]# yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake
(2)创建运行用户
[root@clr /opt]# useradd -M -s /sbin/nologin mysql
(3)编译安装
[root@clr ~]# cd /opt
[root@clr /opt]# rz -E
[root@clr /opt]# ls
boost_1_59_0.tar.gz mysql-5.7.17.tar.gz nginx-1.24.0 nginx-1.24.0.tar.gz rh
[root@clr /opt]# tar xf boost_1_59_0.tar.gz
[root@clr /opt]# tar xf mysql-5.7.17.tar.gz
[root@clr /opt]# ls
boost_1_59_0 boost_1_59_0.tar.gz mysql-5.7.17 mysql-5.7.17.tar.gz nginx-1.24.0 nginx-1.24.0.tar.gz rh
[root@clr /opt]# mv boost_1_59_0 /usr/local/boost
[root@clr /opt]# cd mysql-5.7.17/
[root@clr /opt/mysql-5.7.17]# cmake \
> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
> -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
> -DSYSCONFDIR=/etc \
> -DSYSTEMD_PID_DIR=/usr/local/mysql \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_EXTRA_CHARSETS=all \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
> -DMYSQL_DATADIR=/usr/local/mysql/data \
> -DWITH_BOOST=/usr/local/boost \
> -DWITH_SYSTEMD=1
[root@clr /opt/mysql-5.7.17]# make -j4 && make install
(4)创建mysql用户
[root@clr /opt/mysql-5.7.17]# useradd -M -s /sbin/nologin mysql
(5)修改mysql配置文件
[root@clr /opt/mysql-5.7.17]# vim /etc/my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
(5)更改mysql安装目录和配置文件的属主属组
[root@clr /usr/local/mysql]# chown -R mysql:mysql /usr/local/mysql/
[root@clr /usr/local/mysql]# chown mysql:mysql /etc/my.cnf
(6)设置路径环境变量
[root@clr /usr/local/mysql]# echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@clr /usr/local/mysql]# source /etc/profile
[root@clr /usr/local/mysql]# echo $PATH
/usr/local/mysql/bin:/usr/local/mysql/lib:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
(7)初始化数据库
[root@clr ~]# cd /usr/local/mysql/bin/
[root@clr /usr/local/mysql/bin]# ./mysqld \
> --initialize-insecure \
> --user=mysql \
> --basedir=/usr/local/mysql \
> --datadir=/usr/local/mysql/data
(8)添加mysqld系统服务
[root@clr /usr/local/mysql/bin]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@clr /usr/local/mysql/bin]# systemctl daemon-reload
[root@clr /usr/local/mysql/bin]# systemctl start mysqld.service
[root@clr /usr/local/mysql/bin]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@clr /usr/local/mysql/bin]# netstat -anpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 82406/mysqld
(9)修改mysql的登录密码
[root@clr /usr/local/mysql/bin]# mysqladmin -u root password "123"
(10)授权远程登录
[root@clr /usr/local/mysql/bin]# mysql -u root -p123
mysql> grant all privileges on *.* to 'root'@'%' identified by '123';
mysql> flush privileges;
3.安装配置PHP解析环境😂
(1)安装环境依赖包
[root@clr /opt/php]# yum -y install gd \
> libjpeg libjpeg-devel \
> libpng libpng-devel \
> freetype freetype-devel \
> libxml2 libxml2-devel \
> zlib zlib-devel \
> curl curl-devel \
> openssl openssl-devel
(2)编译安装
[root@clr /usr/local/mysql]# cd /opt/
[root@clr /opt]# mkdir php
[root@clr /opt]# cd php
[root@clr /opt/php]# rz -E
[root@clr /opt/php]# ls
php-7.1.10.tar.bz2
[root@clr /opt/php]# tar -xf php-7.1.10.tar.bz2
[root@clr /opt/php]# ls
php-7.1.10 php-7.1.10.tar.bz2
[root@clr /opt/php]# cd php-7.1.10/
[root@clr /opt/php/php-7.1.10]# ./configure \
> --prefix=/usr/local/php \
> --with-mysql-sock=/usr/local/mysql/mysql.sock \
> --with-mysqli \
> --with-zlib \
> --with-curl \
> --with-gd \
> --with-jpeg-dir \
> --with-png-dir \
> --with-freetype-dir \
> --with-openssl \
> --enable-fpm \
> --enable-mbstring \
> --enable-xml \
> --enable-session \
> --enable-ftp \
> --enable-pdo \
> --enable-tokenizer \
> --enable-zip
[root@clr /opt/php/php-7.1.10]# make -j4 && make install
(3)路径优化
[root@clr /opt/php/php-7.1.10]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@clr /opt/php/php-7.1.10]# ln -s /usr/local/php/sbin/* /usr/local/sbin/
(4)调整PHP配置文件
php有三个配置文件
配置文件名 | 绝对路径地址 |
---|---|
主配置文件 | /usr/local/php/lib/php.ini |
进程服务配置文件 | /usr/local/php/etc/php-fpm.conf |
扩展配置文件 | usr/local/php/etc/php-fpm.d/www.conf |
调整主配置文件:/usr/local/php/lib/php.ini
[root@clr /opt/php/php-7.1.10]# cp /opt/php/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
[root@clr /opt/php/php-7.1.10]# vim /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock ##--1170行--修改
date.timezone = Asia/Shanghai ##--939行--取消注释,修改
[root@clr /opt/php/php-7.1.10]# php -m #验证安装的模块
调整进程服务配置文件:/usr/local/php/etc/php-fpm.conf
[root@clr /opt/php/php-7.1.10]# cd /usr/local/php/etc/
[root@clr /usr/local/php/etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@clr /usr/local/php/etc]# cp php-fpm.conf.default php-fpm.conf
[root@clr /usr/local/php/etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php-fpm.d
[root@clr /usr/local/php/etc]# vim php-fpm.conf
pid = run/php-fpm.pid #--17行--去掉";"注释
调整扩展配置文件:/usr/local/php/etc/php-fpm.d/www.conf
[root@clr /usr/local/php/etc]# cd /usr/local/php/etc/php-fpm.d/
[root@clr /usr/local/php/etc/php-fpm.d]# ls
www.conf.default
[root@clr /usr/local/php/etc/php-fpm.d]# cp www.conf.default www.conf
[root@clr /usr/local/php/etc/php-fpm.d]# vim www.conf
user = nginx #--23行修改为系统中已存在的用户名
group = nginx #--24行修改为系统中已存在的用户名
listen = 192.168.80.50:9000 #--36行此处修改为php服务器的IP地址(监听php服务器的IP地址)
listen.allowed_clients = 192.168.80.50 #--62行此处修改为nginx服务器的IP地址(接收转发请求的客户端地址)
(5)启动php-fpm
PHP-FPM(FastCGI Process Manager:FastCGI 进程管理器)是一个 PHP FastCGI管理器, 由于Nginx服务器不能处理动态页面,需要由Nginx把动态请求交给php-fpm进程进行解析。
[root@clr /usr/local/php/etc/php-fpm.d]# cd /opt/php/php-7.1.10/sapi/fpm
[root@clr /opt/php/php-7.1.10/sapi/fpm]# pwd
/opt/php/php-7.1.10/sapi/fpm
[root@clr /opt/php/php-7.1.10/sapi/fpm]# cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
[root@clr /opt/php/php-7.1.10/sapi/fpm]# systemctl daemon-reload
[root@clr /opt/php/php-7.1.10/sapi/fpm]# systemctl start php-fpm.service
[root@clr /opt/php/php-7.1.10/sapi/fpm]# systemctl enable php-fpm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@clr /opt/php/php-7.1.10/sapi/fpm]# systemctl enable mysqld
[root@clr /opt/php/php-7.1.10/sapi/fpm]# netstat -lntp | grep 9000
tcp 0 0 192.168.80.50:9000 0.0.0.0:* LISTEN 91087/php-fpm: mast
(6)配置Nginx支持PHP解析
[root@clr /opt/php/php-7.1.10/sapi/fpm]# cd /usr/local/nginx/conf
[root@clr /usr/local/nginx/conf]# mv nginx.conf nginx.conf.bak1
[root@clr /usr/local/nginx/conf]# cp nginx.conf.default nginx.conf
[root@clr /usr/local/nginx/conf]# vim nginx.conf
45 index index.html index.htm index.php; #此处需要添加index.php
65 location ~ \.php$ { #--65行到71行取消注释
66 root html;
67 fastcgi_pass 192.168.80.50:9000; #此处需要修改为Nginx服务器的ip地址
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #$document_root 代表当前请求在root指令中指定的值
70 include fastcgi_params;
71 }
[root@clr /usr/local/nginx/conf]# nginx -t
[root@clr /usr/local/nginx/conf]# systemctl restart nginx
(7)验证PHP测试页
[root@clr /usr/local/nginx/conf]# cd /usr/local/nginx/html
[root@clr /usr/local/nginx/html]# ls
50x.html index.html
[root@clr /usr/local/nginx/html]# vim index.php
<?php
phpinfo();
?>
~
[root@clr /usr/local/nginx/html]# ls
50x.html index.html index.php
[root@clr /usr/local/nginx/html]# mv index.html index.html.bak
[root@clr /usr/local/nginx/html]# ls
50x.html index.html.bak index.php
浏览器访问
http://192.168.80.50/index.php
http://192.168.80.50 #访问这两个地址效果一样
(8)验证数据库工作是否正常
[root@clr /usr/local]# mysql -uroot -p123
mysql> create database gzy; #创建数据库gzy
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| gzy |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> grant all on gzy.* to 'gzyadmin'@'%' identified by '123'; #允许用户gzyadmin从任意主机对gzy数据库的所有执行权限,并设置密码123
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> grant all on gzy.* to 'gzyadmin'@'localhost' identified by '123';#允许用户gzyadmin在本地对gzy数据库的所有执行权限,并设置密码123
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> flush privileges; #刷新权限
Query OK, 0 rows affected (0.00 sec)
[root@clr /usr/local/nginx/html]# vim test.php #配置连接数据库的测试页面
<?php
$link=mysqli_connect('192.168.80.50','gzyadmin','123');
if($link) echo "<h1>connect successfuly!!!</h1>";
else echo "<h1>connect failed!!!</h1>";
?>
浏览器访问
http://192.168.80.50/test.php
2.部署Discuz社区论坛Web应用
(1)复制论坛文件到nginx的网页根目录,并重命名为bbs
[root@clr /opt]# rz -E
[root@clr /opt]# unzip Discuz_X3.4_SC_UTF8.zip
[root@clr /opt]# cd dir_SC_UTF8/
[root@clr /opt/dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs
[root@clr /opt/dir_SC_UTF8]# cd /usr/local/nginx/html
[root@clr /usr/local/nginx/html]# ls
50x.html bbs index.html.bak index.php test.php
浏览器访问
http://192.168.80.50/bbs
(2)调整论坛目录的权限
[root@clr /usr/local/nginx/html]# cd bbs/
[root@clr /usr/local/nginx/html/bbs]# chown -R nginx data/ config/ uc_*
(3)论坛页面访问
数据库服务器:localhost #本地架设就用localhost,如果不是在本机上就要填写IP地址和端口号
数据库名字:gzy
数据库用户名:gzyadmin
数据库密码:123
管理员账号:admin
管理员密码:123
浏览器访问普通用户登录页面;
http://192.168.80.50/bbs/index.php
浏览器访问管理员登录页面;
http://192.168.80.50/bbs/admin.php
3. 部署WordPress社区论坛Web应用
(1)复制论坛文件到nginx的网页根目录/usr/local/nginx/html/
[root@clr /usr/local/nginx/html/bbs]# cd /opt
[root@clr /opt]# rz -E
[root@clr /opt]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@clr /opt]# cp -r wordpress /usr/local/nginx/html/
[root@clr /opt]# cd !$
cd /usr/local/nginx/html/
[root@clr /usr/local/nginx/html]# ls
50x.html bbs index.html.bak index.php test.php wordpress
(2)调整论坛目录的权限
[root@clr /usr/local/nginx/html]# chown -R nginx wordpress/
[root@clr /usr/local/nginx/html]# ll
总用量 24
-rw-r--r--. 1 root root 497 6月 1 12:12 50x.html
drwxr-xr-x. 13 root root 4096 6月 1 18:16 bbs
-rw-r--r--. 1 root root 615 6月 1 16:44 index.html.bak
-rw-r--r--. 1 root root 20 6月 1 16:51 index.php
-rw-r--r--. 1 root root 164 6月 1 18:12 test.php
drwxr-xr-x. 5 nginx root 4096 6月 1 18:40 wordpress
(3)创建word数据库和wordadmin用户,并赋予wordadmin用户对word数据库的执行权限
[root@clr /usr/local/nginx/html]# mysql -uroot -p123
mysql> create database word;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| gzy |
| mysql |
| performance_schema |
| sys |
| word |
+--------------------+
mysql> grant all on word.* to 'wordadmin'@'%' identified by '123'; #允许用户wordadmin从任意主机对word数据库的所有执行权限,并设置密码123
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all on word.* to 'wordadmin'@'localhost' identified by '123'; #允许用户wordadmin在本地对word数据库的所有执行权限,并设置密码123
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> flush privileges; #刷新权限
Query OK, 0 rows affected (0.00 sec)
(3)论坛页面访问
数据库名字:word
数据库用户名:wordadmin
数据库密码:123
4. fpm参数优化
Nginx的PHP解析功能实现是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整,根据服务器的内存与服务负载,调整FPM模块参数.
[root@clr ~]# vim /usr/local/php/etc/php-fpm.d/www.conf
96 pm = dynamic #fpm进程启动方式,动态的
107 m.max_children = 20 #fpm进程启动的最大进程数
112 pm.start_servers = 5 #动态方式下启动时默认开启的进程数,在最小和最大之间
117 pm.min_spare_servers = 2 #动态方式下最小空闲进程数
122 pm.max_spare_servers = 8 #动态方式下最大空闲进程数