LNMP编译安装部署文档
系统和软件要求
系统环境
CentOS Linux release 7.4.1708
软件环境
软件包位置(/opt)
nginx/1.12.2
PHP 7.1.1
Mysql5.6
Redis-4.0.9
phpMyAdmin-4.7.0
memcached-1.5.7
构建Nginx+MySQL+PHP平台
(1)安装lnmp所需依赖库
\#yum -y install gcc automake autoconf libtool make gcc-c++ glibc libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel pcre pcre-devel libmcrypt libmcrypt-devel cmake gmp-devel readline-devel
创建www所属组及用户
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
设置系统资源限制
ulimit -SHn 65535
(2)安装配置Nginx
Nginx 源码安装
\#cd /opt/
wget http://nginx.org/download/nginx-1.12.2.tar.gz
\#tar -xvf nginx-1.12.2.tar.gz
\#cd nginx-1.12.2
./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --user=www --group=www --with-http\_ssl\_module --with-http\_gzip\_static\_module --with-http\_stub\_status\_module
\#make && make install
nginx //启动
nginx -s stop// 停止
nginx -s reload // 重新加载
为方便管理设置启动脚本,配置开机启动
vi /etc/rc.d/init.d/nginx
\#! /bin/bash
# chkconfig: 35 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
test -x $DAEMON || exit 0
d\_start(){
$DAEMON || echo -n " already running"
}
d\_stop() {
$DAEMON -s quit || echo -n " not running"
}
d\_reload() {
$DAEMON -s reload || echo -n " counld not reload"
}
case "$1" in
start)
echo -n "Starting $DESC:$NAME"
d\_start
echo "."
;;
stop)
echo -n "Stopping $DESC:$NAME"
d\_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d\_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d\_stop
sleep 2
d\_start
echo "."
;;
\*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
\#chmod +x /etc/rc.d/init.d/nginx (设置可执行权限)
\#chkconfig --add nginx (添加系统服务)
\#service nginx start
\#service nginx stop
\#service nginx restart
\#service nginx reload
浏览器访问:http://公网ip/如能出现nginx页面则表示成功
![](https://img.kancloud.cn/40/9b/409b5ff4809b522b89758adce6709960_609x244.png)
常用查询
查看nginx进程
ps -ef | grep nginx
查看进程个数 去掉首位的
ps -ef | grep nginx | wc -l
查看80端口
netstat -tlupn|grep nginx
(3)安装配置php(多线程)
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 安装多线程扩展PHP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
PHP 7.2 安装 [http://zpblog.cn/linux/PHP7\_pthreads.html](http://zpblog.cn/linux/PHP7_pthreads.html)
\#cd /opt/
\#wget <http://php.net/distributions/php-7.2.11.tar.gz>
\#tar xvzf php-7.2.11.tar.gz
\#cd php-7.2.11
\#groupadd www && useradd -g www www
\#./configure --prefix=/usr/local/php7 --enable-maintainer-zts --with-config-file-path=/usr/local/php7/etc --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip --with-fpm-user=www --with-fpm-group=www
\#make clean && make && make install
\#cp php.ini-production /usr/local/php7/etc/php.ini
\#cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm
\#chmod 755 /etc/init.d/php7-fpm
\#cd /usr/local/php7/etc/
\#cp php-fpm.conf.default php-fpm.conf
\#cd php-fpm.d
\#cp www.conf.default www.conf
\#/etc/init.d/php7-fpm start
pthreads 安装
\#wget https://codeload.github.com/krakjoe/pthreads/zip/master
\#unzip master
\#cd pthreads-master
\#/usr/local/php7/bin/phpize
\#./configure --with-php-config=/usr/local/php7/bin/php-config
\#make && make install
由于pthreads v3只支持cli模式,所以加入php配置文件如下:
\#cd /usr/local/php7/etc/
\#cp php.ini php-cli.ini
编辑 php-cli.ini 加入以下:
extension="pthreads.so"
找到date.timezone选项,删除前面的注释符,并将其值设置为 PRC
查看安装是否成功
运行
\#/usr/local/php7/bin/php -m
\#export LD\_LIBRARY\_PATH=/lib/:/usr/lib/:/usr/local/lib
另外还需要设置环境变量 :
修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码
PATH=$PATH:/usr/local/php7/bin:/usr/local/nginx/sbin
export PATH
最后使用php -m检验安装情况,如果有pthreads模块就满足需求
然后执行 命令 source /etc/profile
php -v 就可以看到PHP版本信息了。
启动php-fpm:
/etc/init.d/php7-fpm start
如果出现错误:ERROR: \[pool www\] cannot get uid for user 'nginx'
则新建www-data 用户组:
useradd nginx
然后再启动php-fpm
php-fpm参数:
\--start 启动
\--stop 强制终止
\--quit 平滑终止
\--restart 重启
\--reload 重新平滑加载php的php.ini
\--logrotate 重新启用log文件
查看php版本
\#php -v
(4)配置nginx支持php解析
在nginx的配置文件中添加
vi /usr/local/nginx/conf/nginx.conf
location ~ \\.php$ {
root /usr/local/nginx/html/;
fastcgi\_pass 127.0.0.1:9000;
fastcgi\_index index.php;
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;
include /usr/local/nginx/conf/fastcgi\_params;
}
注释:/usr/local/nginx/html/ nginx的web目录
/usr/local/nginx/conf/fastcgi\_params fastcgi处理文件
重启nginx
service nginx restart
启动php解析管理器fastcgi
service php-fpm start
编写测试页面
<?php
phpinfo();
?>
测试:
http://47.98.97.98/index.php
![](https://img.kancloud.cn/d5/3a/d53a18fd4d3f7f3e51f1eabe62a77ebe_932x293.png)
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 测试多线程扩展支持 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
编写测试页面:
<?php
/\*\*
\* Created by PhpStorm.
\* User: WJD
\* Date: 2019/3/4
\* Time: 17:21
\*/
class test\_thread\_run extends Thread
{
public $url;
public $data;
public function \_\_construct($url)
{
$this->url = $url;
}
public function run()
{
if(($url = $this->url))
{
$this->data = model\_http\_curl\_get($url);
}
}
}
function model\_thread\_result\_get($urls\_array)
{
foreach ($urls\_array as $key => $value)
{
$thread\_array\[$key\] = new test\_thread\_run($value\["url"\]);
$thread\_array\[$key\]->start();
}
foreach ($thread\_array as $thread\_array\_key => $thread\_array\_value)
{
while($thread\_array\[$thread\_array\_key\]->isRunning())
{
usleep(10);
}
if($thread\_array\[$thread\_array\_key\]->join())
{
$variable\_data\[$thread\_array\_key\] = $thread\_array\[$thread\_array\_key\]->data;
}
}
return $variable\_data;
}
function model\_http\_curl\_get($url,$userAgent="")
{
$userAgent = $userAgent ? $userAgent : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)';
$curl = curl\_init();
curl\_setopt($curl, CURLOPT\_URL, $url);
curl\_setopt($curl, CURLOPT\_RETURNTRANSFER, 1);
curl\_setopt($curl, CURLOPT\_TIMEOUT, 5);
curl\_setopt($curl, CURLOPT\_USERAGENT, $userAgent);
$result = curl\_exec($curl);
curl\_close($curl);
return $result;
}
for ($i=0; $i < 10; $i++)
{
$urls\_array\[\] = array("name" => "baidu", "url" => "http://www.baidu.com/s?wd=".mt\_rand(10000,20000));
}
$t = microtime(true);
$result = model\_thread\_result\_get($urls\_array);
$e = microtime(true);
echo "More Thread used time:".($e-$t)."\\n";
$t = microtime(true);
foreach ($urls\_array as $key => $value)
{
$result\_new\[$key\] = model\_http\_curl\_get($value\["url"\]);
}
$e = microtime(true);
echo "For Loops used time:".($e-$t)."\\n";
?>
以php-cli模式启动:
\#php thread\_test.php
![](https://img.kancloud.cn/2b/61/2b6137da2e5a94a4642e71029d783444_431x70.png)
可见多线程所用时间比完成同样逻辑的for循环快。
(5)nginx配置虚拟主机(参考)
web.test.com
server {
listen 80;
server\_name web.test.com;
location ~ \\.php$ {
root /usr/local/nginx/html/web.test.com;
fastcgi\_pass 127.0.0.1:9000;
fastcgi\_index index.php;
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;
include /usr/local/nginx/conf/fastcgi\_params;
}
location / {
root /usr/local/nginx/html/web.test.com;
index index.php;
}
}
(6)MySQL5.6安装配置
\#cd /opt
\#yum -y install libaio libaio-devel
\#wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.26.tar.gz#groupadd mysql
\#useradd -r -g mysql -s /bin/false mysql 创建mysql用户并加入到mysql组 不允许mysql用户登录系统
\#tar -xvf mysql-5.6.35-linux-glibc2.5-x86\_64.tar.gz
\# cd mysql-5.6.35-linux-glibc2.5-x86\_64
\# cmake -DCMAKE\_INSTALL\_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=/usr/local/mysql/data -DMYSQL\_TCP\_PORT=3306 -DMYSQL\_USER=mysql -DENABLE\_DOWNLOADS=1
\#make && make install
\#mv mysql-5.6.35-linux-glibc2.5-x86\_64 /usr/local/mysql
\#chown -R mysql:mysql /usr/local/mysql
\#rm -rf /etc/my.cnf
\#cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
修改文件为
\[mysqld\]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/mysql.pid
log-error=/usr/local/mysql/data/mysql.err
max\_connections=200
default-storage-engine=INNODB
lower\_case\_table\_names=1
max\_allowed\_packet=20M
初始化数据库
\#cd /usr/local/mysql/scripts
\# ./mysql\_install\_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
\# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
启动数据库
\# service mysqld start
添加环境变量
\#echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
\#source /etc/profile
\#原数据库密码为空 修改数据库密码为admin123@
mysqladmin -uroot password admin123@
Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作:
一、允许root用户在任何地方进行远程登录,并具有所有库任何操作权限,
具体操作如下:
在本机先使用root用户登录mysql: mysql -u root -p"youpassword" 进行授权操作:
mysql>GRANT ALL PRIVILEGES ON \*.\* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION;
重载授权表:
FLUSH PRIVILEGES;
退出mysql数据库:
exit
二、允许root用户在一个特定的IP进行远程登录,并具有所有库任何操作权限,具体操作如下: 在本机先使用root用户登录mysql: mysql -u root -p"youpassword" 进行授权操作: GRANT ALL PRIVILEGES ON \*.\* TO root@"172.16.16.152" IDENTIFIED BY "youpassword" WITH GRANT OPTION; 重载授权表: FLUSH PRIVILEGES; 退出mysql数据库: exit
三、允许root用户在一个特定的IP进行远程登录,并具有所有库特定操作权限,具体操作如下: 在本机先使用root用户登录mysql: mysql -u root -p"youpassword" 进行授权操作: GRANT select,insert,update,delete ON \*.\* TO root@"172.16.16.152" IDENTIFIED BY "youpassword"; 重载授权表: FLUSH PRIVILEGES; 退出mysql数据库: exit
四、删除用户授权,需要使用REVOKE命令,具体命令格式为: REVOKE privileges ON 数据库\[.表名\] FROM user-name; 具体实例,先在本机登录mysql: mysql -u root -p"youpassword" 进行授权操作: GRANT select,insert,update,delete ON TEST-DB TO test-user@"172.16.16.152" IDENTIFIED BY "youpassword"; 再进行删除授权操作: REVOKE all on TEST-DB from test-user; \*\*\*\*注:该操作只是清除了用户对于TEST-DB的相关授权权限,但是这个“test-user”这个用户还是存在。 最后从用户表内清除用户: DELETE FROM user WHERE user="test-user"; 重载授权表: FLUSH PRIVILEGES; 退出mysql数据库: exit
5、 MYSQL权限详细分类: 全局管理权限: FILE: 在MySQL服务器上读写文件。 PROCESS: 显示或杀死属于其它用户的服务线程。 RELOAD: 重载访问控制表,刷新日志等。 SHUTDOWN: 关闭MySQL服务。 数据库/数据表/数据列权限: ALTER: 修改已存在的数据表(例如增加/删除列)和索引。 CREATE: 建立新的数据库或数据表。 DELETE: 删除表的记录。 DROP: 删除数据表或数据库。 INDEX: 建立或删除索引。 INSERT: 增加表的记录。 SELECT: 显示/搜索表的记录。 UPDATE: 修改表中已存在的记录。 特别的权限: ALL: 允许做任何事(和root一样)。 USAGE: 只允许登录--其它什么也不允许做。
6、 centos安装Mariadb
yum -y install mariadb mariadb-server
安装完成MariaDB,首先启动MariaDB
systemctl start mariadb
设置开机启动
systemctl enable mariadb
接下来进行MariaDB的相关简单配置
mysql\_secure\_installation
(7)Redis安装配置
\#cd /opt/
\#wget <http://download.redis.io/releases/redis-4.0.9.tar.gz>
\#tar -xvf redis-4.0.9.tar.gz
\#cd redis-4.0.9
\# /usr/local/php/bin/phpize # php安装后的路径
\# ./configure --with-php-config=/usr/local/php7/bin/php-config
\#make
\#make install
\#cp redis.conf /etc/
配置文件修改
vi /etc/redis.conf
Daemonize yes
pidfile /var/run/redis.pid
编写启动脚本
vi /etc/init.d/redis
\#!/bin/sh
\#
\# redis - this script starts and stops the redis-server daemon
\#
\# chkconfig: - 85 15
\# description: Redis is a persistent key-value database
\# processname: redis-server
\# config: /etc/redis.conf
\# config: /etc/sysconfig/redis
\# pidfile: /var/run/redis.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
redis="/usr/local/bin/redis-server"
prog=$(basename $redis)
REDIS\_CONF\_FILE="/etc/redis.conf"
\[ -f /etc/sysconfig/redis \] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
start() {
\[ -x $redis \] || exit 5
\[ -f $REDIS\_CONF\_FILE \] || exit 6
echo -n $"Starting $prog: "
daemon $redis $REDIS\_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() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
killproc $redis -HUP
RETVAL=$?
echo
}
force\_reload() {
restart
}
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}"
exit 2
esac
配置PHP支持redis
vi php.ini
增加如下内容:
extension\_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
extension=redis.so
重启php-fpm服务,php -m查看是否支持redis扩展
配置开机启动脚本
\#chmod +x /etc/init.d/redis
\#chkconfig --add redis
\#chkconfig --level 35 redis on
\#启动服务
service redis restart
测试
![](https://img.kancloud.cn/cc/0e/cc0e8193bde1208fb3ba062d7ff78eee_751x196.png)
(8)Memcached安装配置
\#yum install gcc libevent libevent-devel
\#wget <http://www.memcached.org/files/memcached-1.5.7.tar.gz>
\#tar -xvf memcached-1.5.7.tar.gz
\#cd memcached-1.5.7
\#./configure --prefix=/usr/local/memcached && make && sudo make install
启动服务
/usr/local/memcached/bin/memcached -d -u root -l 172.16.177.69 -p 11211 -m 10 -c 1000 -P /tmp/memcached.pid
启动参数详解
\#选项说明,这里只列出比较重要的选项,具体选项说明使用memcached -h来查阅
-p TCP端口,默认为11211,可以不设置
-U UDP端口,默认为11211,0为关闭
-l 监听的ip地址
-d 守护进程(daemon)
-u 指定用户,如果当前为 root ,需要使用此参数指定用户
-m 最大内存,单位MB。默认64MB,32位操作系统,每个进程最多只能使用2GB,64位无限制
-M 禁止LRU策略,内存耗尽时返回错误,而不是删除数据
-c 最大连接数,默认是1024
-vv 查看日志
-P memcache的pid文件,结束memcache进程:kill `cat /tmp/memcached\_32054.pid`
-f 增长因子,默认1.25
-n 初始chunk=key+suffix+value+32结构体,默认48字节
-L 启用大内存页,可以降低内存浪费,改进性能
-t 线程数,默认4。由于memcached采用NIO,所以更多线程没有太多作用
-R 每个event连接最大并发数,默认20
-C 禁用CAS命令(可以禁止版本计数,减少开销)
-I 每次申请内存的页的大小(page),默认1M,最小1k,最大128M
-F 禁用flush\_all
(9) **phpadmin安装配置**
#wget <https://files.phpmyadmin.net/phpMyAdmin/4.7.0/phpMyAdmin-4.7.0-all-languages.tar.gz>
\#tar -xvf phpMyAdmin-4.7.0-all-languages.tar.gz
\#mv phpMyAdmin-4.7.0-all-languages /usr/local/nginx/html/phpMyAdmin
测试界面
http://47.98.97.98/phpMyAdmin/
![](https://img.kancloud.cn/59/7c/597cd91426fe8c98dea0d34211cc92f8_807x437.png)
Ubuntu环境下的LNMP搭建
安装Nginx
$ sudo apt-get update`
$ sudo apt-get install nginx
1
2
安装Nginx完成后可查看版本号,输入:
$ /usr/sbin/nginx -v
1
Nginx安装完成,此时访问IP可以看到Welcome Nginx的界面。
安装MySQL
$ sudo apt-get install mysql-server-5.7 mysql-client-5.7
1
中途会提示输入数据库密码,请输入较为复杂的密码并牢记
MySQL安装完成后,输入以下登录验证
mysql -uroot -p
1
密码就是刚才设置的密码
安装PHP及相应扩展
$ sudo apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-curl php7.0-cli php7.0-mcrypt php7.0-mbstring php7.0-dom
1
配置PHP
sudo vim /etc/php/7.0/fpm/php.ini
1
按/输入cgi.fix\_pathinfo寻找该值
解除注释并把值设置为0
启动
sudo phpenmod mcrypt
1
sudo service php7.0-fpm restart
1
配置Nginx
修改user
vim /etc/nginx/nginx.conf
1
将user改为 www-data
因为PHP默认是这个user
端口-代码映射
vim /etc/nginx/sites-available/default
1
键入
server {
#监听端口
listen 81;
server\_name demo;
#相应记录可不填
access\_log /home/www/81.log;
error\_log /home/www/81.error.log;
#项目地址
root /home/www/demo;
index index.php index.html;
#fastcgi\_pass为fpm地址,可查看/etc/php/7.0/fpm/pool.d/www.conf中的listen确定
location ~ \\.php$ {
include fastcgi\_params;
fastcgi\_pass unix:/run/php/php7.0-fpm.sock;
fastcgi\_param PATH\_INFO $fastcgi\_script\_name;
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;
fastcgi\_read\_timeout 10000;
}
}
重启Nginx
nginx -s reload
1
开放端口
sudo ufw allow 81
1
如果是云服务器,记得修改安全配置,以开放相应端口
至此,就可以进行测试了
本地访问 120.0.0.1:81
服务器访问 你的外网IP:81