Linux系统——LNMP分离式部署

#### 安装nginx

```
[[email protected] ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/nginx-1.10.2/
[[email protected] ~]# mount /dev/sr0 /media/cdrom
[[email protected] nginx-1.10.2]# yum -y install pcre-devel openssl-devel
[[email protected] nginx-1.10.2]# useradd -s /sbin/nologin -M www
[[email protected] nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[[email protected] nginx-1.10.2]# make && make install
[[email protected] nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[[email protected] conf]# cd /usr/local/nginx/conf/
[[email protected] conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
[[email protected] 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;
}
}
}
[[email protected] conf]# /usr/local/nginx/sbin/nginx
[[email protected] conf]# netstat -antup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4187/nginx
# nginx编译完毕
```
#### 安装MySQL

```
[[email protected] ~]# ls
anaconda-ks.cfg install.log.syslog
install.log mysql-5.5.32-linux2.6-x86_64.tar.gz
[[email protected] ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
[[email protected] ~]# useradd -s /sbin/nologin -M mysql
[[email protected] ~]# echo "192.168.214.168 LNMP" >> /etc/hosts #MySQL服务器IP地址
[[email protected] ~]# ln -s /usr/local/mysql-5.5.32-linux2.6-x86_64/ /usr/local/mysql
# 初始化MySQL配置文件my.conf
[[email protected] ~]# cd /usr/local/mysql-5.5.32-linux2.6-x86_64/
[[email protected] mysql-5.5.32-linux2.6-x86_64]# /bin/cp support-files/my-small.cnf /etc/my.cnf
# 初始化mysql
[[email protected] mysql]# chown -R mysql.mysql /usr/local/mysql
[[email protected] mysql]# mount /dev/sr0 /media/cdrom
[[email protected] mysql]# yum -y install libaio
[[email protected] 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数据库

```
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# chmod +x /etc/init.d/mysqld
[[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[[email protected] mysql]# netstat -antup | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4688/mysqld
# 设置MySQL开机自启动
[[email protected] mysql]# chkconfig --add mysqld
[[email protected] mysql]# chkconfig mysqld on
[[email protected] mysql]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[[email protected] mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[[email protected] mysql]# which mysqladmin
/usr/local/bin/mysqladmin
[[email protected] mysql]# mysqladmin -uroot password ‘123456‘
[[email protected] 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方式)服务的安装和准备

```
[[email protected] ~]# mount /dev/sr0 /media/cdrom
mount: block device /dev/sr0 is write-protected, mounting read-only
[[email protected] mysql]# yum -y install openssl-devel openssl
[[email protected] mysql]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
[[email protected] mysql]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
[[email protected] ~]# 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包
[[email protected] ~]# 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
[[email protected] ~]# tar xf libiconv-1.14.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/libiconv-1.14/
[[email protected] libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install

[[email protected] ~]# 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%]
[[email protected] ~]# 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%]
[[email protected] ~]# 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%]
[[email protected] ~]# 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
```
[[email protected] ~]# tar xf php-5.3.28.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/php-5.3.28/
[[email protected] 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.
--------------------------------------------------------------
快捷方式:
[[email protected] 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

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

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

```
[[email protected] etc]# cd /usr/local/nginx/conf/
[[email protected] conf]# cp nginx.conf nginx.conf.bak
[[email protected] 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
[[email protected] ~]# 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;
}
}
}
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] ~]# 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
[[email protected] /]# mkdir -p www
[[email protected] /]# chown -R www.www /www/
[[email protected] /]# echo "123456" > index.php # index.php文件的位置必须与PHP服务器的一致,同时存在
#本机/我的电脑/磁盘C/Windows/system32/drivers/etc/hosts
192.168.214.169 www.yunjisuan.com

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

```
[[email protected] ~]# 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

[[email protected] ~]# mkdir -p /www
[[email protected] ~]# cd /www/
[[email protected] www]# chown -R www.www /www/
[[email protected] www]# echo "daisy" > index.php
[[email protected] 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基本数据

```
[[email protected] ~]# 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端)
```
[[email protected] ~]# 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
[[email protected] ~]# /usr/local/php/sbin/php-fpm
[[email protected] ~]# 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))
[[email protected] www]# vim index.php
daisy 2

```
(nginx端)

```
[[email protected] ~]# 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;
}
}
}
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

```
测试PHP访问MySQL
(PHP端)
```
[[email protected] 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端)
```
[[email protected] 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();
}
?>
```

原文地址:https://www.cnblogs.com/daisy118/p/10086441.html

时间: 2024-11-04 00:40:31

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

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

说明: Cobbler服务器系统:CentOS 5.10 64位 IP地址:192.168.21.128 需要安装部署的Linux系统: eth0(第一块网卡,用于外网)IP地址段:192.168.21.160-192.168.21.200 eth1(第二块网卡,用于内网)IP地址段:10.0.0.160-10.0.0.200 子网掩码:255.255.255.0 网关:192.168.21.2 DNS:8.8.8.8  8.8.4.4 所有服务器均支持PXE网络启动 实现目的:通过配置Cobb

LNMP分离式部署(7)

1.LNMP之MySQL数据库 1.1 安装步骤介绍 (1)创建mysql用户的账号 [[email protected] ~]# groupadd mysql [[email protected] ~]# useradd -s /sbin/nologin -g mysql -M mysql [[email protected] ~]# tail -1 /etc/passwd mysql:x:501:501::/home/mysql:/sbin/nologin [[email protected

企业级LNMP分离式部署

安装MySQL数据库 安装步骤介绍 本例采用MySQL二进制安装包进行安装演示 (1) 创建mysql用户的账号 [[email protected] ~]# groupadd mysql [[email protected] ~]# useradd -s /sbin/nologin -g mysql -M mysql [[email protected] ~]# tail -1 /etc/passwd mysql:x:500:501::/home/mysql:/sbin/nologin [[e

LNMP分离式部署实例

很多人在练习部署LNMP环境的时候,大都数是部署在同一个虚拟机上面的.但是实际工作中,我们一般都是分离部署的. 今天我就用3台虚拟机,部署下LNMP环境.以供参考! 网络拓扑图: 首先准备3台虚拟机: nginx:192.168.1.214 php:192.168.1.202 mysql:192.168.1.217 首先安装ngix(192.168.1.214):  #解决依赖   yum install -y gcc,openssl-devel,pcre-devel,zilb-devel  p

Linux系统postfix邮件部署

提示:请先部署LAMP 上图,安装软件包. 上图,修改DNS主配置文件. 上图,修改区域配置文件. 上图,重启后测试域名解析是否正常. 上图,修改inet_interfaces = all. 上图,将myhostname的值修改为mail.winsnet.com 上图,将mydomain的值修改为winsnet.com 上图,修改myorigin的值 上图,修改mydestination的值 上图,去掉home前面的#号. 上图,启动Postfix. 上图,安装telnet软件包. 上图,使用t

LNMP分离式部署搭建

一.前期准备 1.准备三台虚拟机,分别为: Nginx Web:192.168.200.142 PHP:192.168.200.128 Mysql:192.168.200.143 二.服务搭建 一.Nginx Web搭建 yum install -y pcre-devel openssl-devel #用本地yum仓库安装依赖包 wget -q http://nginx.org/download/nginx-1.10.2.tar.gz #下载软件源码包 useradd -s /sbin/nolo

LNMP分离式部署步骤详解

1 .nginx编译安装参考nginx的2 .mysql编译安装参考mysql的3 .php安装要加一条安装支持yum -y install openssl-devel openssl 1. 开始部署web端 1) 在根目录下创建一个目录wwwmkdir /www递归给www属主和属组为wwwchown  -R www.www /www2) vim /usr/local/nginx/conf/nginx.confworker_processes  1;events {    worker_con

Linux系统下项目部署常用命令

1. 远程传送文件: scp xxx.zip [email protected]:/usr/local/src 远程服务器用户:root ip:10.74.165.114 :后面的传递文件保存路径 2. 解压zip文件: unzip xxx.zip -d aaa 解压xxx.zip文件置于当前路径下的aaa文件夹下: 3. vi xxx ---- 打开文件,并可以对文件进行修改 vi 打开一个不存在的文件a时,在没有输入保存命令时,a文件是不会存储到机器磁盘上的. vi打开一个文件时,进入的是阅

Linux系统Ubuntu下部署Tomcat

相比于window下安装tomcat,Ubuntu下相对也挺简单,但是网上大都写的太复杂,下面是我安装的过程,记下来供大家参考. 其实在Ubuntu和window下安装大同小异,不需要你make编译安装,只需解压启动就行. 1. 安装JDK配置环境变量(见上文,不在啰嗦) 2. 下载tomcat(官网下载tar.gz格式的),一般下载到了ubuntu中 /home/andy/下载 中(当前用户) 3. 解压到/usr/local目录中 $ sudo  tar zxvf  apache-tomca