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]# make && make install
[[email protected] nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[[email protected]
[[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]# 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();
}
?>
```