|NO.Z.00058|——————————|^^ 部署 ^^|——|Linux&Centos6.x&LNMP.V02|
一、源码软件包安装
### --- Nginx
~~~ Nginx是一款轻量级的web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,
~~~ 在BSD-like协议下发行,其特点是占有内存少,并发能力强。
### --- 下载nginx源码包
[root@server21 ~]# mkdir /lnmp
[root@server21 ~]# cd /lnmp/
[root@server21 lnmp]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@server21 lnmp]# tar -zxvf nginx-1.12.2.tar.gz
### --- 创建用于运行nginx的用户
[root@server21 lnmp]# useradd -r -s /sbin/nologin nginx
### --- 解压缩并安装
[root@server21 lnmp]# cd nginx-1.12.2
[root@server21 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module && make && make install
[root@server21 nginx-1.12.2]# echo $?
0
[root@server21 nginx-1.12.2]# /usr/local/nginx/sbin/nginx
[root@server21 nginx-1.12.2]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4385/nginx
### --- 验证:
~~~ http://20.20.20.21/
~~~ Welcome to nginx!
~~~ 上传编写好的nginx启动管理脚本(见文本尾部)
二、安装MySQL
### --- 安装MySQL
~~~ 下载:https://dev.mysql.com/downloads.mysql/
~~~ 选择:MySQL Community SERVER 5.5 >>
~~~ 选择:Select Version:按照自己要求选择
~~~ Select Operating System:Source Code
~~~ Select OS Version:Generic Linux
### --- 格式:mysql-N.N.NN.tar.gz
[root@server21 nginx-1.12.2]# cd ..
[root@server21 lnmp]# wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.25a.tar.gz
~~~ #OR
[root@server21 ~]# https://downloads.mysql.com/archives/get/p/23/file/mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
### --- 创建用于运行MySQL的用户:
[root@server21 lnmp]# useradd -r -s /sbin/nologin mysql
### --- 解压缩MySQL并安装
[root@server21 mysql-5.5.25a]# tar -zxvf mysql-5.5.25a.tar.gz
[root@server21 mysql-5.5.25a]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
> -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 \
> -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
[root@server21 mysql-5.5.25a]# echo $?
0
[root@server21 mysql-5.5.25a]# make && make install
[root@server21 mysql-5.5.25a]# echo $?
0
[root@server21 mysql-5.5.25a]# ln -s /usr/local/mysql/bin/* /usr/local/bin
### --- 修改安装后的目录权限
[root@server21 mysql-5.5.25a]# cd /usr/local/mysql
[root@server21 mysql]# chown -R root .
[root@server21 mysql]# chown -R mysql data/
### --- 生成MySQL配置文件
[root@server21 mysql]# cp -a /lnmp/mysql-5.5.25a/support-files/my-medium.cnf /etc/my.cnf
cp: overwrite '/etc/my.cnf'? y
### --- 初始化。生成授权表
[root@server21 mysql]# cd /usr/local/mysql/ // 一定要先切换到此目录下,然后在执行下一步
[root@server21 mysql]# ./scripts/mysql_install_db --user=mysql // 初始化成功标志:两个ok
### --- 生成MySQL的启动和自启动管理脚本
[root@server21 mysql]# cd /lnmp/mysql-5.5.25a/support-files/ // 切换到MySQL的源码解压缩目录下的support-files
[root@server21 support-files]# cp -a mysql.server /etc/init.d/mysqld
[root@server21 support-files]# chmod +x /etc/init.d/mysqld
[root@server21 support-files]# chkconfig --add mysqld
[root@server21 support-files]# service mysqld status
ERROR! MySQL is not running
[root@server21 support-files]# service mysqld start
Starting MySQL.. SUCCESS!
[root@server21 support-files]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 14203/mysqld
### --- 给MySQL的root用户设置密码
[root@server21 support-files]# mysqladmin -uroot password 123456
[root@server21 support-files]# mysql -uroot -p
Enter password: 123456
mysql>
三、PHP
### --- 下载:http://www.php.net/
[root@server21 lnmp]# wget http://tw2.php.net/distributions/php-5.6.38.tar.gz
[root@server21 lnmp]# tar -zxvf php-5.6.38.tar.gz
### --- 解压缩PHP并安装
[root@server21 php-5.6.38]# ./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc/ \
> --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-soap --enable-mbstring=all --enable-sockets \
> --with-pdo-mysql=/usr/local/mysql --with-gd --without-pear --enable-fpm
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
### --- 报错信息
If configure fails try --with-vpx-dir=<DIR>
If configure fails try --with-jpeg-dir=<DIR>
configure: error: png.h not found.
### --- 解决方案:
[root@server21 php-5.6.38]# yum install libpng
[root@server21 php-5.6.38]# yum install libpng-devel
### --- 报错信息:若遇到libpeng.so not found.报错(老版本的PHP会出现此问题)
[root@server21 php-5.6.38]# make && make install
### --- 解决方案:
[root@server21 php-5.6.38]# ln -s /usr/lib64/libpng.so /usr/lib
### --- 生成PHP配置文件
[root@server21 php-5.6.38]# cp -a /lnmp/php-5.6.38/php.ini-production /usr/local/php/etc/php.ini #复制源码包内的配置文件到安装目录下,并改名即可
### --- 创建软连接,使用PHP相关命令是更方便
[root@server21 php-5.6.38]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@server21 php-5.6.38]# ln -s /usr/local/php/sbin/* /usr/local/sbin/
四、配置nginx连接PHP(重难点)

### --- 配置nginx连接PHP(重难点)
~~~ 当由用户通过nginx的80端口进行请求,nginx响应并判断用户的请求,
~~~ 若是普通的HTML的页面,nginx会直接返回结果,若是PHP的页面,
~~~ nginx会主动向php-fpm发起请求,告诉它有一个PHP的页面需要解析,你找PHP解析一下。
~~~ nginx连接PHP是借助了PHP-fpm:9000端口来完成连接,
~~~ 而Apache是通过模块来实现连接的。
### --- nginx连接PHP需要启动php-fpm服务
~~~ 启动时开启的进程数、最少空闲进程数、做多空闲进程数(默认值,未修改)
~~~ 修改nginx启动管理脚本,将php-fpm的注释取消掉即可
[root@server21 php-5.6.38]# cd /usr/local/php/etc/
[root@server21 etc]# cp -a php-fpm.conf.default php-fpm.conf // 生成php-fpm的配置文件,并修改制定参数
[root@server21 etc]# vim php-fpm.conf // 修改指定条目的参数:
pid = run/php-fpm.pid
user = nginx // 默认是nobody
group = nginx
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
### --- 修改nginx的配置文件,使其识别.php后缀的文件
[root@server21 ~]# cp -a /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bac
[root@server21 ~]# vim /usr/local/nginx/conf/nginx.conf // 取消下列的注释,并修改include选项的后缀为fastcgi.conf,并注意每一行结尾的分号和大括号
user nginx; // 默认是nobody;默认是注销的,开启即可
location ~ \.php$ { // 默认是注释的,取消注释。不需要进行任何修改,只需要取消注释
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf; // 把fastcgi_params; 修改为fastcgi.conf #默认是fastcgi_params;,建议修改为fastcfi.conf,不然会报错。
}
### --- 测试:
### --- 重启nginx服务,创建PHP测试文件,访问并查看是否解析
### --- 配置启动脚本,见三、附件
[root@server21 ~]# vim /etc/init.d/nginx
vim /etc/init.d/nginx
[root@server21 ~]# chmod +x /etc/init.d/nginx
[root@server21 ~]# service nginx start
nginx service start success!
[root@server21 ~]# service nginx restart
nginx service stop success!
nginx service start success!
[root@server21 ~]# netstat -antp
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 647/php-fpm
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 14203/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 643/nginx
### --- 写入PHP的测试页面
~~~ 可以正常获取PHP 5.6.38版本的信息
~~~ 访问验证:http://20.20.20.21/test.php
[root@server21 ~]# vim /usr/local/nginx/html/test.php
<?php
phpinfo();
### --- 修改nginx配置文件,使其默认自动加载PHP文件
[root@server21 ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.php index.htm; // 加入index.php的解析页面
}
### --- 验证:
~~~ 访问http://20.20.20.21/默认访问的是Apache的页面,因为在配置文件里,
~~~ index.html优先级高于index.php,有index.html就加载index.html,没有就加载index.php
[root@server21 ~]# mv /usr/local/nginx/html/test.php /usr/local/nginx/html/index.php
### --- 验证:
~~~ 访问http://20.20.20.21/显示PHP的解析页面PHP Version 5.6.38,
~~~ 谁在前面就优先加载谁,若是都没有,就显示该目录下的文件列表
[root@server21 ~]# service nginx restart
[root@server21 ~]# rm -rf /usr/local/nginx/html/index.html // 把index.html的配置文件删除
五、附件:建议使用时先复制到文本文件中查看是否有字符集问题
~~~ # 有4个变量,2个是命令,一个是nginx的启动命令,一个是php-fpm的启动命令,
~~~ # 两个pid文件有kiss语句,当执行start/restart/stop同时操作nginx,php-fpm
###################################Nginx自动的管理脚本##############################
[root@server21 ~]# vim /etc/init.d/nginx
#!/bin/bash
#Author: yanqi
#chkconfig: 2345 99 33
#description:nginx server control tools
ngxc="/usr/local/nginx/sbin/nginx"
pidf="usr/local/nginx/logs/nginx.pid"
ngxc_fpm="/usr/local/php/sbin/php-fpm"
pidf_fpm="/usr/local/php/var/run/php-fpm.pid"
case "$1" in
start)
$ngxc -t &> /dev/null
if [ $? -eq 0 ];then
$ngxc
$ngxc_fpm
echo "nginx service start success!"
else
$ngxc -t
fi
;;
stop)
kill -s QUIT $(cat $pidf)
kill -s QUIT $(cat $pidf_fpm)
echo "nginx service stop success!"
;;
restart)
$0 stop
$0 start
;;
reload)
$ngxc -t &> /dev/mull
if [ $? -eq 0 ];then
kill -s HUP $(cat $pidf)
kill -s HUP $(cat $pidf_fpm)
echo "reload nginx config success!"
else
$ngxc -t
fi
;;
*)
echo "please input stop|start|restart|reload."
exit 1
esac
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
cdv007-network
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库