#### 安装nginx

```
[root@localhost ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.10.2/
[root@localhost ~]# mount /dev/sr0 /media/cdrom
[root@localhost nginx-1.10.2]# yum -y install pcre-devel openssl-devel
[root@localhost nginx-1.10.2]# useradd -s /sbin/nologin -M www
[root@localhost nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.10.2]# make && make install
[root@localhost nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost conf]# cd /usr/local/nginx/conf/
[root@localhost conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
[root@localhost conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
[root@localhost conf]# /usr/local/nginx/sbin/nginx
[root@localhost conf]# netstat -antup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4187/nginx
# nginx编译完毕
```
#### 安装MySQL

```
[root@localhost ~]# ls
anaconda-ks.cfg install.log.syslog
install.log mysql-5.5.32-linux2.6-x86_64.tar.gz
[root@localhost ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# useradd -s /sbin/nologin -M mysql
[root@localhost ~]# echo "192.168.214.168 LNMP" >> /etc/hosts #MySQL服务器IP地址
[root@localhost ~]# ln -s /usr/local/mysql-5.5.32-linux2.6-x86_64/ /usr/local/mysql
# 初始化MySQL配置文件my.conf
[root@localhost ~]# cd /usr/local/mysql-5.5.32-linux2.6-x86_64/
[root@localhost mysql-5.5.32-linux2.6-x86_64]# /bin/cp support-files/my-small.cnf /etc/my.cnf
# 初始化mysql
[root@localhost mysql]# chown -R mysql.mysql /usr/local/mysql
[root@localhost mysql]# mount /dev/sr0 /media/cdrom
[root@localhost mysql]# yum -y install libaio
[root@localhost mysql]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/local/mysql/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/local/mysql/scripts/mysqlbug script!

```
#### 配置并启动MySQL数据库

```
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chmod +x /etc/init.d/mysqld
[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@localhost mysql]# netstat -antup | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4688/mysqld
# 设置MySQL开机自启动
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@localhost mysql]# which mysqladmin
/usr/local/bin/mysqladmin
[root@localhost mysql]# mysqladmin -uroot password '123456'
[root@localhost mysql]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, 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>

```
#### PHP(fastcgi方式)服务的安装和准备

```
[root@localhost ~]# mount /dev/sr0 /media/cdrom
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost mysql]# yum -y install openssl-devel openssl
[root@localhost mysql]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
[root@localhost mysql]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
[root@localhost ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
zlib-devel-1.2.3-29.el6.x86_64
libxml2-devel-2.7.6-14.el6.x86_64
libjpeg-turbo-devel-1.2.1-1.el6.x86_64
#这里仅缺少libiconv-devel包
[root@localhost ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
freetype-devel-2.3.11-14.el6_3.1.x86_64
libpng-devel-1.2.49-1.el6_2.x86_64
libcurl-devel-7.19.7-37.el6_4.x86_64
libxslt-devel-1.1.26-2.el6_3.1.x86_64
gd-2.0.35-11.el6.x86_64
[root@localhost ~]# tar xf libiconv-1.14.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/libiconv-1.14/
[root@localhost libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install

[root@localhost ~]# rpm -ivh mhash-0.9.9.9-3.el6.x86_64.rpm
warning: mhash-0.9.9.9-3.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:mhash ########################################### [100%]
[root@localhost ~]# rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm
warning: libmcrypt-2.5.8-9.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:libmcrypt ########################################### [100%]
[root@localhost ~]# rpm -ivh libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
warning: libmcrypt-devel-2.5.8-9.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:libmcrypt-devel ########################################### [100%]
[root@localhost ~]# rpm -ivh mcrypt-2.6.8-10.el6.x86_64.rpm
warning: mcrypt-2.6.8-10.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:mcrypt ########################################### [100%]

```
5. 开始安装PHP(fastcgi方式)服务
(1)解压配置PHP
```
[root@localhost ~]# tar xf php-5.3.28.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/php-5.3.28/
[root@localhost php-5.3.28]# ./configure \
> --prefix=/usr/local/php5.3.28 \
> --with-mysql=mysqlnd \
> --with-iconv-dir=/usr/local/libiconv \
> --with-freetype-dir \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib \
> --with-libxml-dir=/usr \
> --enable-xml \
> --disable-rpath \
> --enable-safe-mode \
> --enable-bcmath \
> --enable-shmop \
> --enable-sysvsem \
> --enable-inline-optimization \
> --with-curl \
> --with-curlwrappers \
> --enable-mbregex \
> --enable-fpm \
> --enable-mbstring \
> --with-mcrypt \
> --with-gd \
> --enable-gd-native-ttf \
> --with-openssl \
> --with-mhash \
> --enable-pcntl \
> --enable-sockets \
> --with-xmlrpc \
> --enable-zip \
> --enable-soap \
> --enable-short-tags \
> --enable-zend-multibyte \
> --enable-static \
> --with-xsl \
> --with-fpm-user=www \
> --with-fpm-group=www \
> --enable-ftp
+--------------------------------------------------------------------+
| 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.
--------------------------------------------------------------
快捷方式:
[root@localhost php-5.3.28]# ./configure --prefix=/usr/local/php5.3.28 --with-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp

```
(2)编译安装PHP

```
[root@localhost php-5.3.28]# touch ext/phar/phar.phar
[root@localhost php-5.3.28]# make
[root@localhost php-5.3.28]# make install
[root@localhost php-5.3.28]# ln -s /usr/local/php5.3.28/ /usr/local/php
[root@localhost php-5.3.28]# cd /usr/local/php
[root@localhost php]# cd /usr/src/php-5.3.28/
[root@localhost php-5.3.28]# ls php.ini*
php.ini-development php.ini-production
[root@localhost php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini
[root@localhost php-5.3.28]# cd /usr/local/php/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# /usr/local/php/sbin/php-fpm
[root@localhost etc]# netstat -antup | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 129604/php-fpm
[root@localhost etc]# vim php-fpm.conf
151 listen = 192.168.214.167:9000 #分离式部署,改为PHP服务器IP地址

```
#### 配置nginx支持PHP程序请求访问

```
[root@localhost etc]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp nginx.conf nginx.conf.bak
[root@localhost conf]# vim nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.yunjisuan.com;
root /www;
location / {
index index.php index.html index.htm;
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass192.168.214.167:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
# 配置nginx.conf配置文件并启动nginx
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.yunjisuan.com;
root /www;
location / {
index index.php index.html index.htm; #添加一个动态文件
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass 192.168.214.167:9000; #PHPIP地址
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.214.169 www.yunjisuan.com
[root@localhost /]# mkdir -p www
[root@localhost /]# chown -R www.www /www/
[root@localhost /]# echo "123456" > index.php # index.php文件的位置必须与PHP服务器的一致,同时存在
#本机/我的电脑/磁盘C/Windows/system32/drivers/etc/hosts
192.168.214.169 www.yunjisuan.com

```
配置PHP服务器的配置文件

```
[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf

; will be used.
user = www
group = www

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 192.168.214.167:9000

; Set listen(2) backlog. A value of '-1' means unlimited.
; Default Value: 128 (-1 on FreeBSD and OpenBSD)
;listen.backlog = -1

[root@localhost ~]# mkdir -p /www
[root@localhost ~]# cd /www/
[root@localhost www]# chown -R www.www /www/
[root@localhost www]# echo "daisy" > index.php
[root@localhost www]# vim test_mysql.php
<?php
//$link_id=mysql_connect('主机名','用户','密码');
$link_id=mysql_connect('192.168.214.168','yunjisuan','123456'); # IP地址为MySQLIP地址
if($link_id){
echo "mysql successful by daisy\n";
}else{
echo mysql_error();
}
?>

```

配置MySQL基本数据

```
[root@lnmp ~]# mysql -uroot -p123456
mysql> grant all on *.* to 'yunjisuan'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

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

mysql> select user,host from mysql.user;
+-----------+-----------------------+
| user | host |
+-----------+-----------------------+
| yunjisuan | % |
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
+-----------+-----------------------+
7 rows in set (0.00 sec)

mysql> exit
Bye

```
查看nginx与PHP的动态数据连接情况
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181128001358713.)
查看mysql与PHP连接情况
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181128001249347.?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzMwNDgwNA==,size_16,color_FFFFFF,t_70)

用fastcgi方式实现负载均衡
1. 克隆服务器PHP2(用PHP服务器克隆)
(PHP2端)
```
[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf

; will be used.
user = www
group = www

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 192.168.214.170:9000 #修改为本服务器IP地址

; Set listen(2) backlog. A value of '-1' means unlimited.
; Default Value: 128 (-1 on FreeBSD and OpenBSD)
;listen.backlog = -1
[root@localhost ~]# /usr/local/php/sbin/php-fpm
[root@localhost ~]# ss -antup | grep 9000
tcp LISTEN 0 128 192.168.214.170:9000 *:* users:(("php-fpm",1518,7),("php-fpm",1519,0),("php-fpm",1520,0))
[root@localhost www]# vim index.php
daisy 2

```
(nginx端)

```
[root@blog ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www{
server 192.168.214.167:9000; #反向代理的IP地址
server 192.168.214.170:9000;

}
server {
listen 80;
server_name www.yunjisuan.com;
root /www;
location / {
index index.php index.html index.htm;
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass www; # 目录名
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
[root@blog ~]# /usr/local/nginx/sbin/nginx -s reload

```
测试PHP访问MySQL
(PHP端)
```
[root@localhost www]# vim test_mysql.php
<?php
//$link_id=mysql_connect('主机名','用户','密码');
$link_id=mysql_connect('192.168.214.168','yunjisuan','123456'); # IP地址为MySQLIP地址
if($link_id){
echo "mysql successful by daisy\n";
}else{
echo mysql_error();
}
?>
```
(PHP2端)
```
[root@localhost www]# vim test_mysql.php
<?php
//$link_id=mysql_connect('主机名','用户','密码');
$link_id=mysql_connect('192.168.214.168','yunjisuan','123456'); # IP地址为MySQLIP地址
if($link_id){
echo "mysql successful by daisy\n";
}else{
echo mysql_error();
}
?>
```

Linux系统——LNMP分离式部署的更多相关文章

  1. linux系统LNMP环境部署

    源码安装 nginx# yum -y install gcc openssl-devel# useradd -s /sbin/nologin nginx# tar xf nginx-1.14.0.ta ...

  2. linux系统tomcat项目部署和tomcat访问日志

    一.只用ip地址访问 先把端口号改成80,然后用 <Host name="localhost"  appBase="webapps"    137     ...

  3. Windows环境安装Linux系统及JDK部署

    前言 由于我的笔记本有点问题,所以这周系统包括所有硬盘全部重装了,原来的Linux虚拟机都没了,因此才有了这篇文章和各位朋友们分享. 由于Linux环境的优越性(开源.低成本.安全性好.网络功能强大) ...

  4. Linux系统批量化安装部署之Cobbler

    说明: Cobbler服务器系统:CentOS 5.10 64位 IP地址:192.168.21.128 需要安装部署的Linux系统: eth0(第一块网卡,用于外网)IP地址段:192.168.2 ...

  5. Django项目在linux系统中虚拟环境部署

    1.在linux系统下,安装virtualenv 命令:pip install virtualenv 2.项目部署前的准备 1. Django web project deployment 1.1.  ...

  6. 学习神器!本机安装虚拟机,并安装Linux系统,并部署整套web系统手册(包含自动部署应用脚本,JDK,tomcat,TortoiseSVN,Mysql,maven等软件)

    1.   引言 编写目的 本文档的编写目的主要是在Linux系统上部署mis_v3员工管理系统,方便测试,并为以后开发人员进行项目部署提供参考 准备工作 软件部分 软件项 版本 备注 Mysql 5. ...

  7. 使用URLOS在linux系统中极速部署NFS共享存储服务

    如何在linux系统里搭建NFS服务?其实我们只需要安装一个URLOS面板,然后就能在3分钟内将NFS服务部署完成.近日,URLOS在应用市场中上架了一款NFS应用,它可以让我们的节点主机在3分钟内极 ...

  8. Linux系统(控制节点)部署环境

    环境部署 重点说明:安装ansible时去控制Windows机器,由于需要在Linux系统上安装pywinrm插件,而使用yum安装锝ansible是无法调用pywinrm插件,所以整体使用pip工具 ...

  9. LNMP分离式部署

    #### LNMP组合工作流程 在LNMP组合工作时,首先是用户通过浏览器输入域名请求Nginx Web服务,如果请求是静态资源,则由Nginx解析返回给用户:如果是动态请求(.php结尾),那么Ng ...

随机推荐

  1. Unity性能优化之Draw Call(转)

    Unity(或者说基本所有图形引擎)生成一帧画面的处理过程大致可以这样简化描述:引擎首先经过简单的可见性测试,确定摄像机可以看到的物体,然后把这些物体的顶点(包括本地位置.法线.UV等),索引(顶点如 ...

  2. border-image使用过程中遇到的几个问题

    这次年货,为了增添气氛,很多地方都用了边框,由于边框高度的不固定,给构建着实带了不小的麻烦. 通常我们处理边框的方法,无非以下两种方案: 1.切整张图片,做背景. 2.切上中下,进行拼接处理. 但是遇 ...

  3. node项目部署相关问题

    process.env process.env属性返回一个对象,包含了当前Shell的所有环境变量. 通常的做法是,新建一个环境变量NODE_ENV,用它确定当前所处的开发阶段,生产阶段设为produ ...

  4. undefined类型

    undefined类型 只有一个特殊的值 undefined   在使用var声明变量但未对其加以初始化,这个变量的值就是undefined 值是undefined的情况: 1.显示声明并初始化变量值 ...

  5. iOS - 开源框架、项目和学习资料汇总(其他篇)

    数据库 1. FMDB – sqlite的工具, 多线程FMDatabaseQueue实例,FMDB数据库的使用演示和封装工具类.GXDatabaseUtils – 在FMDB基础上的工具.2. re ...

  6. 爬取博主的所有文章并保存为PDF文件

    继续改进上一个项目,上次我们爬取了所有文章,但是保存为TXT文件,查看不方便,而且还无法保存文章中的代码和图片. 所以这次保存为PDF文件,方便查看. 需要的工具: 1.wkhtmltopdf安装包, ...

  7. ibatitsnet 因为会Dao.config 配置数据版本太低导致的问题

    ProjectReview.Test.SqlMapTest.TestSqlMap:IBatisNet.Common.Exceptions.ConfigurationException : - The ...

  8. hdu5266 pog loves szh III 【LCA】【倍增】

    Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of ...

  9. JAVA补充-接口

    1.接口详解 package com.neusoft.interfaced; /** * 接口: * 语法:interface 接口名{ * public static final 变量1=值1: * ...

  10. Codeforces 752C - Santa Claus and Robot - [简单思维题]

    题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...