# 在Linux安装部署Nginx+MySQL+PHP(LNMP )
## 安装Nginx
```
cd /opt
mkdir src
```
下载阿里云yum源更新脚本
```
wget http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/41177/cn\_zh/1504061676920/update\_source.sh
```
为该脚本添加执行权限
```
chmod +x update_source.sh
```
开始执行更新yum源脚本
```
sh update_source.sh
```
使用yum安装一些基础库
```
yum install -y make cmake gcc gcc-c++ autoconf automake libpng-devel libjpeg-devel zlib libxml2-devel ncurses-devel bison libtool-ltdl-devel libiconv libmcrypt mhash mcrypt libmcrypt-devel pcre-devel openssl-devel freetype-devel libcurl-devel
```
下载nginx源码
```
cd /opt/src
wget http://nginx.org/download/nginx-1.8.0.tar.gz
```
解压文件
```
tar -zxvf nginx-1.8.0.tar.gz
```
添加一个叫做www的用户
```
cd nginx-1.8.0
useradd www -M -s /sbin/nologin
```
配置nginx安装参数
```
./configure --prefix=/opt/nginx --user=www --group=www --with-http\_stub\_status\_module --with-http\_ssl\_module
```
开始编译源代码
```
make
```
安装nginx
```
make install clean
```
启动nginx
```
/opt/nginx/sbin/nginx
```
## 安装php
```
cd /opt/src
```
下载php源代码
```
wget http://cn2.php.net/distributions/php-5.6.10.tar.gz
```
解压
```
tar -zxvf php-5.6.10.tar.gz
```
配置php安装参数
```
cd php-5.6.10
./configure --prefix=/opt/php --with-mysql --with-mysqli --with-iconv-dir --with-zlib --with-libxml-dir --enable-xml --with-curl --enable-fpm --enable-mbstring --with-gd --with-openssl --with-mhash --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-libdir=/usr/lib64 --with-jpeg-dir=/usr/lib64 --with-freetype-dir=/usr/lib64 --with-png-dir=/usr/lib64 --with-fpm-user=www --with-fpm-group=www
make
make install clean
cd /opt/src/php-5.6.10
cp php.ini-development /opt/php/lib/php.ini
cd /opt/php/etc/
cp php-fpm.conf.default php-fpm.conf
cd /opt/src/php-5.6.10/sapi/fpm/
cp init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
service php-fpm start
```
加入开启启动
```
chkconfig --add php-fpm
chkconfig php-fpm on
```
配置 Nginx可以解析php文件
```
cd /opt/nginx/conf/
```
创建网站根目录 以后我们的网站全部放在这里
```
mkdir /opt/webroot
vi nginx.conf
```
Nginx配置文件内容 请删除原来的内容 替换下面的
```
{
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
root /opt/webroot;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
try_files $uri =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
```
Nginx配置文件内容结束
关闭nginx进程重新启动
```
killall nginx
/opt/nginx/sbin/nginx
```
创建一个php文件
```
cd /opt/webroot
vi info.php
```
info.php 文件内容
```
<?php
phpinfo();
```
重启Nginx
```
/opt/nginx/sbin/nginx -s reload或/opt/nginx/sbin/nginx
```
为Nginx添加自启动
```
mkdir /opt/shell
cd /opt/shell
vi nginxrun.sh
```
[nginxrun.sh脚本内容](https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/):
```
#!/bin/sh
/opt/nginx/sbin/nginx
```
给这个脚本文件添加执行权限
```
chmod +x /opt/shell/nginxrun.sh
```
加入开启自启动文件里面去
```
vi /etc/rc.local
```
在/etc/rc.local文件的最后面添加一行
```
/opt/shell/nginxrun.sh
```
## 安装kod 云盘应用
```
cd /opt/webroot
mkdir kod
cd kod
```
下载kod程序源码
```
wget http://static.kodcloud.com/update/download/kodexplorer4.21.zip
```
安装解压软件
```
yum install -y unzip
unzip kodexplorer4.21.zip
chmod -R 777 /opt/webroot
```
## 安装mysql
```
cd /opt/src/
```
下载mysql源代码
```
wget http://cdn.markdream.com/ref/sources/lnmp/mysql-5.6.20.tar.gz
```
解压
```
tar -zxvf mysql-5.6.20.tar.gz
cd mysql-5.6.20
配置mysql安装参数
```
配置mysql安装参数
```
cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql
make
make install clean
```
添加mysql用户
```
useradd mysql -M -s /sbin/nologin
cd /opt/mysql/scripts
```
安装数据库
```
./mysql_install_db --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
cd /opt/mysql/support-files
```
复制mysql管理脚本
```
cp mysql.server /etc/rc.d/init.d/mysql
```
复制mysql配置文件
```
cp my-default.cnf /etc/my.cnf
```
添加mysql服务
```
chkconfig --add mysql
```
加入开机启动策略
```
chkconfig mysql on
```
启动mysql
```
service mysql start
```
修改mysql数据库 root的账号密码 并且允许远程登录
进入管理mysql数据库
```
/opt/mysql/bin/mysql
```
之后你会看见类似 mysql> 开头的命令行
表示允许root用户可以在远程任何地方登录 且登录的密码为 123456
```
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
```
刷新mysql权限让用户生效
```
FLUSH PRIVILEGES;
```
退出mysql
```
quit
```
重新启动mysql服务
```
service mysql restart
```
重置KodExplorer 密码
```
cd /opt/webroot/kod
rm -rf data/system/install.lock
```
至此已经完全安装了LNMP,可道云与PHPCMS可以按需安装。