linux学习lesson48



1 lnmp结构介绍

和LAMP不同的是,提供web服务的是Nginx 并且php是作为一个独立服务存在的,这个服务叫做php-fpm Nginx直接处理静态请求,动态请求会转发给php-fpm
在这里插入图片描述


2 mysql安装

下载mysql5.6包:

[root@linux01 ~]# cd /usr/local/src
[root@linux01 src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.39-linux-glibc2.5-x86_64.tar.gz

解压mysql压缩包:

[root@linux01 src]# tar zxvf mysql-5.6.39-linux-glibc2.5-x86_64.tar.gz

将源码包移动到/usr/local下并改名字:

[root@linux01 src]# mv mysql-5.6.39-linux-glibc2.5-x86_64 /usr/local/mysql

进入mysql目录下,创建mysql用户和data目录:

[root@linux01 mysql]#cd /usr/local/mysql
[root@linux01 mysql]#useradd -s /sbin/nologin mysql
[root@linux01 mysql]#mkdir /data/

初始化数据库:

[root@linux01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql

拷贝配置模板文件:

[root@linux01 mysql]#cp support-files/my-default.cnf /etc/my.cnf

修改配置文件:

[root@linux01 mysql]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d

将mysql加入系统服务:

[root@linux01 mysql]#cp support-files/mysql.server /etc/init.d/mysqld

编译mysql服务文件:

[root@linux01 mysql]# vim /etc/init.d/mysqld (定义basedir和datadir)
basedir=/usr/local/mysql
datadir=/data/mysql

启动mysql服务:

[root@linux01 mysql]# vi /etc/init.d/mysqld
[root@linux01 mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

安装过程中出现问题:
1)
[root@worker1 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
解决办法:

[root@worker1 mysql]# yum install -y perl-Module-Install
[root@worker1 mysql]# yum install -y perl-Data-Dumper

2)
[root@worker2 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql
Installing MySQL system tables…/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决办法:

[root@worker2 mysql]# yum install -y libaio-devel


3 php安装

和LAMP安装PHP方法有差别,没有用到apxs自动配置工具加载模块,因为没有用到Apache,需要开启php-fpm服务,与nginx服务无关
下载php5.6包:

[root@linux01 ~]# cd /usr/local/src/
[root@linux01 src]# wget http://cn2.php.net/distributions/php-5.6.32.tar.gz

解压php包:

[root@linux01 src]# tar zxf php-5.6.32.tar.gz

创建php的用户:

[root@linux01 src]# useradd -s /sbin/nologin php-fpm

configure编译:

[root@linux01 src]# cd php-5.6.32
[root@linux01 php-5.6.32]# ./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
[root@linux01 php-5.6.32]# echo $?
0

编译成功

进入makemake install编译

[root@linux01 php-5.6.32]# make && make install
[root@linux01 php-5.6.32]# echo $?
0

编译成功

查看安装目录:

[root@linux01 php-5.6.32]# ls /usr/local/php-fpm/
bin  etc  include  lib  php  sbin  var

查看安装的信息:

[root@linux01 php-5.6.32]# /usr/local/php-fpm/bin/php -i | head //安装的信息
phpinfo()
PHP Version => 5.6.32

System => Linux linux01 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64
Build Date => Nov 18 2018 17:52:41
Configure Command =>  './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'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/php-fpm/etc
Loaded Configuration File => (none)

查看安装模块:

[root@linux01 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -m | head
[PHP Modules]
cgi-fcgi
Core
ctype
curl
date
dom
ereg
exif
fileinfo
[root@linux01 php-5.6.32]# /usr/local/php-fpm/bin/php -m | head
[PHP Modules]
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter

检测php-fpm.conf的语法

[root@linux01 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -t
[18-Nov-2018 17:58:22] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[18-Nov-2018 17:58:22] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[18-Nov-2018 17:58:22] ERROR: FPM initialization failed

出现报错,因为还没加配置文件

拷贝php的配置文件

[root@linux01 php-5.6.32]# cp php.ini-production /usr/local/php-fpm/etc/php.ini 

(线上用php.ini-production,测试环境用php.ini-development,两者其中的错误日志提示不一样)

编辑php的配置文件:

[root@linux01 php-5.6.32]# vi /usr/local/php-fpm/etc/php-fpm.conf //写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/php-fpm.conf,可以使用模板修改)
(监听方式可以是socket或者是tcp/ip
listen = /tmp/php-fcgi.sock
listen = 127.0.0.1:9000)
[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 以sock方式监听(二选一)
#listen = 127.0.0.1:9000 以ip方式监听
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

拷贝php启动服务到系统:

[root@linux01 php-5.6.32]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

检查配置文件语法:

[root@linux01 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -t
[18-Nov-2018 18:04:24] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

设置php服务的权限:

[root@linux01 php-5.6.32]# chmod 755 /etc/init.d/php-fpm

添加php服务到系统管理服务里:

[root@linux01 php-5.6.32]# chkconfig --add php-fpm

php服务开机启动:

[root@linux01 php-5.6.32]# chkconfig php-fpm on

启动php服务:

[root@linux01 php-5.6.32]# /etc/init.d/php-fpm start
Starting php-fpm  done

查看php服务进程:

[root@linux01 php-5.6.32]# ps aux | grep php
root     54995  0.0  0.2 227268  4988 ?        Ss   18:08   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm  54996  0.0  0.2 227268  4704 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  54997  0.0  0.2 227268  4704 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  54998  0.0  0.2 227268  4708 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  54999  0.0  0.2 227268  4708 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55000  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55001  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55002  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55003  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55004  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55005  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55006  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55007  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55008  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55009  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55010  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55011  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55012  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55013  0.0  0.2 227268  4712 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55014  0.0  0.2 227268  4716 ?        S    18:08   0:00 php-fpm: pool www
php-fpm  55015  0.0  0.2 227268  4716 ?        S    18:08   0:00 php-fpm: pool www
root     55020  0.0  0.0 112704   972 pts/0    S+   18:08   0:00 grep --color=auto php

查看监听的文件权限:

[root@linux01 php-5.6.32]# ll /tmp/php-fcgi.sock
srw-rw-rw- 1 root root 0 Nov 18 18:11 /tmp/php-fcgi.sock

安装过程出现的问题:(yum install -y epel-release)
1)
checking for xml2-config path…
configure: error: xml2-config not found. Please check your libxml2 installation.
解决办法:

[root@worker1 php-5.6.32]# yum install -y libxml2-devel

2)
checking for pkg-config… /usr/bin/pkg-config
configure: error: Cannot find OpenSSL’s <evp.h>
解决办法:

[root@worker1 php-5.6.32]# yum install -y openssl-devel

3)
configure: error: Please reinstall the libcurl distribution -
easy.h should be in /include/curl/
解决办法:

[root@worker1 php-5.6.32]# yum install -y libcurl-devel

4)
If configure fails try --with-vpx-dir=<DIR
configure: error: jpeglib.h not found.
解决办法:

[root@worker1 php-5.6.32]# yum install -y libjpeg-devel

5)
checking for jpeg_read_header in -ljpeg… yes
configure: error: png.h not found.
解决办法:

[root@worker1 php-5.6.32]# yum install -y libpng-devel

6)
checking for png_write_image in -lpng… yes
If configure fails try --with-xpm-dir=<DIR
configure: error: freetype-config not found.
解决办法:

[root@worker1 php-5.6.32]# yum install -y freetype-devel

7)
checking for mcrypt support… yes
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决办法:

[root@worker1 php-5.6.32]# yum install -y libmcrypt-devel


4 nginx介绍

  1. Nginx官网 nginx.org,最新版1.13,最新稳定版1.12
  2. Nginx应用场景:web服务、反向代理、负载均衡 Nginx应用场景:web服务、反向代理、负载均衡
  3. Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并
  4. Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928
  5. 现在的java网站架构java+tomcat+nginx,所以nginx越来越多人使用现在的java网站架构java+tomcat+nginx,所以nginx越来越多人使用
  6. 应用场景:web服务器,反向代理,负载均衡应用场景:web服务器,反向代理,负载均衡



扩展
Nginx为什么比Apache Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html
apache和nginx工作原理比较 http://www.server110.com/nginx/201402/6543.html
mod_php 和 mod_fastcgi以及php-fpm的比较 http://dwz.cn/1lwMSd
概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM http://www.nowamagic.net/librarys/veda/detail/1319/ https://www.awaimai.com/371.html

猜你喜欢

转载自blog.csdn.net/InfiniteIdea_Go/article/details/84371280
今日推荐