web服务之LAMP&LNMP架构

文章目录

四、LAMP&LNMP架构

 

4.1 背景知识

CGI Common Gateway Interface

公共网关接口,是一个Web服务器主机提供信息服务的标准接口。

服务器端 CGI 程序接收信息有三种途径:环境变量、命令行和标准输入

 

4.1.1 CGI的处理流程

web服务之LAMP&LNMP架构插图

 

4.1.2 相关术语释疑

  • cgi:它是一种协议。通过cgi协议,web server可以将动态请求和相关参数发送给专门处理动态内容的应用程序。
  • fastcgi:也是一种协议,只不过是cgi的优化版。cgi的性能较差,fastcgi则在其基础上进行了改进。它是与Http类似的一种应用层通信协议。
  • php-cgi:php-cgi是个CGI程序,fastcgi是一种协议,而php-cgi实现了这种协议。不过这种实现比较烂。它是单进程的,一个进程处理一个请求,处理结束后进程就销毁。
  • php-fpm:是对php-cgi的改进版,它直接管理多个php-cgi进程/线程。也就是说,php-fpm是php-cgi的进程管理器因此它也算是fastcgi协议的实现。在一定程度上讲,php-fpm与php的关系,和tomcat对java的关系是类似的。
  • cgi进程/线程:在php上,就是php-cgi进程/线程。专门用于接收web server的动态请求,调用并初始化zend虚拟机。
  • cgi脚本:被执行的php源代码文件。
  • zend虚拟机:对php文件做词法分析、语法分析、编译成opcode,并执行。最后关闭zend虚拟机。
  • cgi进程/线程和zend虚拟机的关系:cgi进程调用并初始化zend虚拟机的各种环境。

Nginx 与 PHP-FPM 运行原理:https://www.iteye.com/blog/wangzq-phper-2314034

Fastcgi是用来提高CGI程序性能的,那么CGI程序的性能问题在哪呢?“PHP解析器会解析php.ini文件,初始化执行环境”

 

4.1.3 php-fpm转发过程图解

FPM(FastCGI Process Manager)

web服务之LAMP&LNMP架构插图(1)

 

4.1.4 web server和CGI的交互模式

1.cgi模式

httpd接收到一个动态请求就fork一个cgi进程,cgi进程返回结果给httpd进程后自我销毁。

 

2.动态模块模式

将php-cgi的模块(例如php5_module)编译进httpd。在httpd启动时会加载模块,加载时也将对应的模块激活,php-cgi也就启动了。

 

3.php-fpm模式

使用php-fpm管理php-cgi,此时httpd不再控制php-cgi进程的启动。可以将php-fpm独立运行在非web服务器上,实现所谓的动静分离。

PHP-FPM 的主配置文件是 /etc/php7/php-fpm.conf

  • 配置
<VirtualHost *:80>
    DocumentRoot  "/www/web/forum"
    ServerName www.forum.com
  ProxyRequests Off
  ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/web/forum/$1
    <Directory "/www/web/forum">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

LoadModule rewrite_module modules/mod_rewrite.so

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

 

4.2 LAMP

LAMP(Linux+Apache+Mysql+Php/Perl/Python…)

4.2.1 (centos6)mysql的基本用法

1.rpm安装
yum install mysql-server
2.启动服务
service mysqld start

 

3.常用操作

mysql客户端命令

mysql
	-u # 用户名
	-p # 密码
	-h # 主机
	-P # 协议

更改用户密码

mysqladmin实现

mysqladmin -uroot password 'new-password' -p
# or
mysqladmin -u root -h hostname password 'new-password'

修改数据库实现

mysql> SET PASSWORD FOR 'root@host_name' = PASSWORD('new-password');
# or
mysql> UPDATE mysql.user SET PASSWORD=PASSWORD('new-password') where CONDITION;
# or
mysql> flush privillage;

服务端常用命令

常用mysql交互模式语句

show databases;
show tables;
use DB;
select * from TB;
create database DB;
grant all on DB.TB  to 'USER'@'HOST' identified by 'PASS';
flush privileges;

批处理模式(用于将表导入数据库)

mysql < *.sql

 

4.数据库的备份
mysqldump -uroot -p PASSWORD [DB_NAME] > [DUMP_FILE] # 备份指定数据库
mysqldump --no-data --databases mydatabase1 mydatabase2 mydatabase3 > [DdMP_FILE] # 只备份表结构
mysqldump --all-databases > [DUMP_FILE] # 备份所有数据库

 

5.常用的sql语句

 
数据定义语言-DDL(data define language)

数据定义,建立数据库的基本组件

# 创建(CREATE)
CREATE DATABASE db_name;

# 删除(DROP)
DROP DATABASE db_name;
TABLE [IF NOT EXISTS] tb_name;
USER 'username' @ 'host' IDENTIFIED BY 'password';

# 修改(ALTER)
ALTER TABLE tb_name

 

数据操作语言-DML:根据需要插入,修改,删除数据库中的数据

# 插入(INSERT)
INSERT INTO tb_name (col1,col2,...) VALUES | VALUE ("String",NUM,...)
#eg:INSERT INTO tb_name (Name,Age) VALUE (haha,22);

# 更新(UPDATE)
UPDATE tb_name SET column=VALUE; # 更改所有字段的值

# 删除(DELETE)
DELETE FROM tb_name; #删除所有行
DELETE FROM tb_name WHERE expression;
#eg:DELETE FROM student WHERE Name="haha";

# 选择(SELECT)
SELECT * FROM tb_name; # 无where则显示字段的所有行
SELECT * FROM tb_name WHERE expression;

 
数据控制语言-DCL:实现用户的授权和撤权

# 授权(GRANT)
GRANT privillage ON db_name.tb_name TO 'username'@'host' IDENTIFIED BY 'PASSWORD';
# privillage;可以是INSERT,UPDATE,DELETE,SELECT,ALL
# 用户名不存在则会自动创建
GRANT ALL ON db_name.* TO 'username'@'host';
SHOW GRANTS FOR 'username'@'%' # 查看授权信息

# 撤权(REVOKE)
REVOKE privillage ON  ON db_name.tb_name TO 'username'@'host' IDENTIFIED BY 'PASSWORD';

# 刷新权限
flush privileges;

 

6.忘记root密码
# 跳过访问控制权限进入mysql
# 编辑/etc/my.cnf,添加一行:skip-grant-table
[mysqld]
skip-grant-table

# 重启mysqld
service mysqld restart

# 修改密码
UPDATE mysql.user SET PASSWORD=PASSWORD('') where user='root' and host='localhost';
# 编辑/etc/my.cnf,删除skip-grant-table

# 重启服务
service mysqld restsrt

 

4.2.2 LAMP下安装wordpress

 

1.mysql配置
  • 安装mysql
[root@lnmp ~]# yum install -y mariadberver
[root@lnmp ~]# yum install -y mariadb-server

centos7的默认数据库为mariadb。

  • 启动mysql
[root@lnmp ~]# systemctl start mariadb
[root@lnmp ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
  • 登录mysql

默认情况下,新用户是没有密码的,直接回车进入修改密码

[root@lnmp ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> update user set password=PASSWORD("123") where user="root";
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> quit
Bye
[root@lnmp ~]# systemctl restart mariadb
[root@lnmp ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

 

2.apache配置
  • 下载apache rpm包并安装
[root@lnmp ~]# yum install -y httpd
[root@lnmp ~]# systemctl start httpd
[root@lnmp ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

之前是建了虚拟主机的www.haha.com

 

3.PHP配置
  • 安装php
[root@lnmp ~]# yum install -y php
[root@lnmp ~]# systemctl restart httpd
  • 创建检验文件
[root@lnmp ~]# cd /data/haha
[root@lnmp haha]# vim index.php
<?php
        echo "<h1> php </h1>";
?>

使用浏览器打开

web服务之LAMP&LNMP架构插图(2)

  • 安装php中的Mysql组件
[root@lnmp ~]# yum install -y php-mysql

安装完后有很多mysql相关内容

phpinfo();

  • 安装phpMyAdmin(非必装)
[root@lnmp ~]# yum install -y phpmyadmin

phpmyadmin不能直接用浏览器打开要建一个快键方式

[root@lnmp ~]# ln -s /usr/share/phpMyAdmin/ /data/haha/

然后就可以了

 

4.WordPress配置
  • 创建数据库
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.38 sec)

MariaDB [(none)]> 
  • 下载wordpress压缩包并解压,然后编辑一下文件
[root@lnmp ~]# wget https://wordpress.org/wordpress-4.8.tar.gz
[root@lnmp ~]# tar -xzvf wordpress-4.8.tar.gz 
[root@lnmp ~]# vim wordpress/wp-config-sample.php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'root');

/** MySQL database password */
define('DB_PASSWORD', '123');

/** MySQL hostname */
define('DB_HOST', 'localhost');

[root@lnmp ~]# cp -rf wordpress /data/haha/
  • 访问

web服务之LAMP&LNMP架构插图(3)

先创建一个用户并授权

MariaDB [(none)]> grant all on wordpress.* to 'kuangfeng'@'localhost' identified by '123' ;
Query OK, 0 rows affected (0.12 sec)
  • 手工建文件

web服务之LAMP&LNMP架构插图(4)

[root@lnmp ~]# ll /data/haha -d
drwxr-xr-x 3 root root 4096 Jun  3 23:21 /data/haha
[root@lnmp ~]# vim /data/haha/wordpress/wp-config.php

完成

web服务之LAMP&LNMP架构插图(5)

完成

web服务之LAMP&LNMP架构插图(6)

 

5.Discuz

安装解压

[root@lnmp ~]# wget http://download.comsenz.com/DiscuzX/3.3/Discuz_X3.3_SC_GBK.zip
[root@lnmp ~]# yum install -y unzip
[root@lnmp ~]# unzip Discuz_X3.3_SC_GBK.zip 
[root@lnmp ~]# cp -rf upload /data/haha/

第一步

然后用浏览器打开

web服务之LAMP&LNMP架构插图(7)

第二步

检查 Discuz! X3.2 论坛系统的安装环境及目录权限。

web服务之LAMP&LNMP架构插图(8)

确保都可写

第三步

选择“全新安装 Discuz! X(含 UCenter Server)”。

第四步

先创建一个数据库专门放这类数据并授权

MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.03 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| discuz             |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
6 rows in set (0.01 sec)
MariaDB [(none)]> grant all on discuz.* to 'kuangfeng'@'localhost' identified by '123' ;
Query OK, 0 rows affected (0.13 sec)

填写服务器的数据库信息与论坛系统管理员信息。

web服务之LAMP&LNMP架构插图(9)

之前的数据库密码

第五步

等待 Discuz! X3.2 论坛系统安装完毕

web服务之LAMP&LNMP架构插图(10)

 

4.3 LNMP

LNMP(Linux+Nginx+MySQL+Php)

4.3.1 nginx的基本用法

安装

[root@lnmp ~]# yum install -y nginx

源码编译安装

./configure
make
mak install

启动&重启

/usr/local/nginx/sbin/nginx
nginx -t # 测试配置文件
nginx -s reload

添加虚拟主机

[root@lnmp ~]# vim /etc/nginx/nginx.conf
include             /etc/nginx/conf.d/vhost/haha.conf;
[root@lnmp ~]# mkdir /etc/nginx/conf.d/vhost

配置文件 php

[root@lnmp ~]# vim /etc/nginx/conf.d/vhost/haha.conf
server {
        listen 80;
        server_name www.haha.com;
        root /data/haha;
        sendfile        on;
        location / {
                root /data/haha/;
                index index.html index.php;
        }
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name ;
                include fastcgi_params;
        }
}

访问nginx映射的代码目录中的index.php报错502:https://blog.csdn.net/qq_16265383/article/details/88960602

完成

web服务之LAMP&LNMP架构插图(11)

 

4.3.2 编译安装lnmp

1.编译安装mysql

安装编译工具和相关依赖

[root@lnmp ~]# yum install -y apr* autoconf automake bison bzip2 bzip2* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libtifflibtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel 

下载依赖安装包

[root@lnmp ~]# mkdir /tmp/source
[root@lnmp ~]# cd /tmp/source/
[root@lnmp source]# 
[root@lnmp source]# wget http://nginx.org/download/nginx-1.10.3.tar.gz
[root@lnmp source]# wget https://www.php.net/distributions/php-5.4.35.tar.gz
[root@lnmp source]# wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz
[root@lnmp source]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
[root@lnmp source]# wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@lnmp source]# wget http://nchc.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
wget http://iweb.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz

安装libiconv,libmcrypt,mhash,bision

[root@lnmp source]# tar -xzvf libmcrypt-2.5.8.tar.gz
[root@lnmp source]# tar -xzvf libiconv-1.14.tar.gz
[root@lnmp source]# tar -xzvf mhash-0.9.9.9.tar.gz 

[root@lnmp source]# cd libmcrypt-2.5.8
[root@lnmp libmcrypt-2.5.8]# ./configure --prefix=/usr/local
[root@lnmp libmcrypt-2.5.8]# make && make install

[root@lnmp source]# cd libiconv-1.14
[root@lnmp libiconv-1.14]# ./configure --prefix=/usr/local
[root@lnmp libiconv-1.14]# make && make install

[root@lnmp source]# cd mhash-0.9.9.9
[root@lnmp mhash-0.9.9.9]# ./configure --prefix=/usr/local
[root@lnmp mhash-0.9.9.9]# make && make install

问题

[root@lnmp libiconv-1.14]# cd srclib/
[root@lnmp srclib]# sed -i -e '/gets is a security/d' ./stdio.in.h

安装cmake

[root@lnmp source]# wget https://cmake.org/files/v3.6/cmake-3.6.1.tar.gz
[root@lnmp source]# tar -xzvf cmake-3.6.1.tar.gz
[root@lnmp cmake-3.6.1]# ./configure --prefix=/usr
[root@lnmp cmake-3.6.1]# gmake
[root@lnmp cmake-3.6.1]# gmake install 

准备用户,目录

[root@lnmp source]# groupadd mysql
[root@lnmp source]# useradd -g mysql mysql
[root@lnmp source]# groupadd nginx
[root@lnmp source]# useradd -g nginx nginx
[root@lnmp source]# mkdir -p /data/mysql
[root@lnmp source]# chown -R mysql.mysql /data/mysql
[root@lnmp source]# mkdir -p /data/www
[root@lnmp source]# chown -R nginx.nginx /data/www

安装

[root@lnmp source]# tar -xzvf mysql-5.6.17.tar.gz 
[root@lnmp source]# cd mysql-5.6.17
[root@lnmp mysql-5.6.17]# cmake -DCMAKE_INTSALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data/mysql -DENABLE_DOWNLOADS=1
[root@lnmp mysql-5.6.17]# make && make install

注:可能会报错,先执行以下两步再编译

[root@lnmp mysql-5.6.19]# rm CMakeCache.txt
[root@lnmp mysql-5.6.19]# yum install ncurses-devel

CentOS安装MySQL时报Curses library not found:https://blog.csdn.net/u010098331/article/details/51519234

为了让 MySQL 数据库程序正常运转起来,需要先删除/etc 目录中的默认配置文件,然后在 MySQL 数据库程序的保存目录 scripts 内找到一个名为 mysql_install_db 的脚本程序,执行这个脚本程序并使用–user 参数指定 MySQL 服务的对应账号名称(在前面步骤已经创建),使用–basedir 参数指定 MySQL 服务程序的保存目录,使用–datadir 参数指定 MySQL 真实数据库的文件保存目录,这样即可生成系统数据库文件,也会生成出新的 MySQL 服务配置文件。

[root@lnmp mysql-5.6.17]# rm -rf /etc/my.cnf
[root@lnmp mysql-5.6.17]# cd /usr/local/mysql
[root@lnmp mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

把系统新生成的 MySQL 数据库配置文件链接到/etc 目录中,然后把程序目录中的开机程序文件复制到/etc/rc.d/init.d 目录中,以便通过 service 命令来管理 MySQL 数据库服务程序。记得把数据库脚本文件的权限修改成 755 以便于让用户有执行该脚本的权限:

[root@lnmp mysql]# ln -s my.cnf /etc/my.cnf
[root@lnmp mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@lnmp mysql]# chmod 755 /etc/rc.d/init.d/mysqld

编辑刚复制的 MySQL 数据库脚本文件,把第 46、47 行的 basedir 与 datadir 参数分别修改为 MySQL 数据库程序的保存目录和真实数据库的文件内容。

[root@lnmp mysql]# vim /etc/rc.d/init.d/mysqld

basedir=/usr/local/mysql
datadir=/data/mysql

配置好脚本文件后便可以用 service 命令启动 mysqld 数据库服务了。mysqld 是 MySQL数据库程序的服务名称,注意不要写错。顺带再使用 chkconfig 命令把 mysqld 服务程序加入到开机启动项中。

[root@lnmp mysql]# service mysqld start
Starting MySQL. SUCCESS!
[root@lnmp mysql]# chkconfig mysqld on

想要把命令所保存的目录永久性地定义到 PATH 变量中,需要编辑/etc/profile 文件并在末尾追加的命令目录

初始化mysql

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

添加mysql服务到init脚本

[root@lnmp mysql-5.6.17]# cd /usr/local/mysql
[root@lnmp mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@lnmp mysql]# vim /etc/profile
[root@lnmp mysql]# PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
[root@lnmp mysql]# export PATH
[root@lnmp mysql]# source /etc/profile

修改mysql的root用户密码

[root@lnmp mysql]# mysqladmin -u root password '123'

运行安全设置脚本,修改MYSQL用户root的密码,同时可禁止root远程连接,移除test数据库和匿名用户

[root@lnmp mysql]# /usr/local/mysql/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): 回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y
New password: root
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y 
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y 
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...

 

2.安装php

编译

[root@lnmp ~]# mkdir /usr/local/php
[root@lnmp ~]# cd /tmp/source/
[root@lnmp source]# tar -xzvf php-5.4.35.tar.gz 
[root@lnmp source]# cd php-5.4.35
[root@lnmp php-5.4.35]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysql=/usr/local/mysql/ --enable-mbstring --enable-soap --enable-bcmath --enable-zip --enable-calendar --enable-sockets --with-curl=/usr/local/  --with-png-dir=/usr/local/ --with-freetype-dir=/usr/local/ --with-jpeg-dir=/usr/local/ --with-mhash=/usr/local/ --with-zlib-dir=/usr/local/ --with-mcrypt=/usr/local/ --with-iconv=/usr/local/ --with-gd
[root@lnmp php-5.4.35]# make && make install
[root@lnmp php-5.4.35]# cp php.ini-development /usr/local/php/etc/php.ini
[root@lnmp php-5.4.35]# rm -rf /etc/php.ini
[root@lnmp php-5.4.35]# cp php.ini-production /usr/local/php/etc/php.ini
[root@lnmp php-5.4.35]# ln -s /usr/local/php/etc/php.ini /etc/php.ini
[root@lnmp php-5.4.35]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@lnmp php-5.4.35]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf

php-fpm.conf 是 php 服务程序重要的配置文件

启用该配置文件中第 25 行左右的 pid 文件保存目录,然后分别将第 148 和 149 行的 user 与 group 参数分别修改为 www账户和用户组名称:

去掉25行pid前的分号

148和149行的nobody改成www

[root@lnmp php-5.4.35]# vim /usr/local/php/etc/php-fpm.conf

pid = run/php-fpm.pid

user = nginx
group = nginx

配置妥当后便可把用于管理 php 服务的脚本文件复制到/etc/rc.d/init.d 中了。为了能够执行脚本,请记得为脚本赋予 755 权限。最后把 php-fpm 服务程序加入到开机启动项中:

[root@lnmp php-5.4.35]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@lnmp php-5.4.35]# chmod 755 /etc/rc.d/init.d/php-fpm
[root@lnmp php-5.4.35]# chkconfig php-fpm on

配置php-fpm

启动php-fpm

[root@lnmp php-5.4.35]# /usr/local/php/sbin/php-fpm &
[1] 39223

 

3.安装nginx

安装nginx的依赖库

[root@lnmp php-5.4.35]# yum install -y pcre zlib

编译安装nginx

[root@lnmp source]# tar -xzvf nginx-1.10.3.tar.gz 
[root@lnmp source]# cd nginx-1.10.3
[root@lnmp nginx-1.10.3]# ./configure --prefix=/usr/local/nginx 
[root@lnmp nginx-1.10.3]# make && make install

将nginx的执行路径加入PATH

[root@lnmp nginx-1.10.3]# vim /etc/profile
/usr/local/nginx/sbin:$PATH
[root@lnmp nginx-1.10.3]# source /etc/profile

配置nginx脚本

[root@lnmp ~]# vim /etc/rc.d/init.d/nginx
#!/bin/bash
# nginx - this script starts and stops the nginx daemon
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
        if [ -z "`grep $user /etc/passwd`" ]; then
                useradd -M -s /bin/nologin $user
        fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
        if [ `echo $opt | grep '.*-temp-path'` ]; then
                value=`echo $opt | cut -d "=" -f 2`
                if [ ! -d "$value" ]; then
                        # echo "creating" $value
                        mkdir -p $value && chown -R $user $value
                fi
        fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
        rh_status_q && exit 0
        $1
        ;;
stop)
        rh_status_q || exit 0
        $1
        ;;
restart|configtest)
$1
;;
reload)
        rh_status_q || exit 7
        $1
        ;;
force-reload)
        force_reload
        ;;
status)
        rh_status
        ;;
condrestart|try-restart)
        rh_status_q || exit 0
        ;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
[root@lnmp ~]# chmod 755 /etc/rc.d/init.d/nginx
[root@lnmp ~]# /etc/rc.d/init.d/nginx restart
Reloading systemd:  [  OK  ]
Restarting nginx (via systemctl):  [  OK  ]
[root@lnmp ~]# chkconfig nginx on

编辑nginx配置文件,加入解析php相关配置

    server {
        listen       80;
        server_name  www.haha.com;
        root /data/haha;

        location / {
            index index.php;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }                                              
    } 

问题:可能解析不了,就把fastcgi_param SCRIPT_FILENAME d o c u m e n t r o o t document_root documentrootfastcgi_script_name;改为绝对路径fastcgi_param SCRIPT_FILENAME /data/haha$fastcgi_script_name;

编辑文件

[root@lnmp ~]# mkdir /data/haha
[root@lnmp ~]# vim /data/haha/index.php

<?php
        echo "<h1> haha php </h1>";
?>

进入浏览器测试

web服务之LAMP&LNMP架构插图(12)

没有账号? 忘记密码?

社交账号快速登录