LNMP就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统。 PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
LNMP
源码编译安装mysql
安装cmake 版本必须为2.8以上
yum install cmake-2.8.12.2-4.el6.x86_64.rpm
下载解压 mysql-boost-5.7.11.tar.gz包
tar zxf mysql-boost-5.7.11.tar.
编译
cd mysql-5.7.11/
make -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql #安装目录
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data #数据库存放目录-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock #Unix socket 文件路径-DWITH_MYISAM_STORAGE_ENGINE=1 #安装 myisam 存储引擎-DWITH_INNOBASE_STORAGE_ENGINE=1 #安装 innodb 存储引擎-DWITH_ARCHIVE_STORAGE_ENGINE=1 #安装 archive 存储引擎-DDEFAULT_CHARSET=utf8 #使用 utf8 字符-DDEFAULT_COLLATION=utf8_general_ci #校验字符-DEXTRA_CHARSETS=all #安装所有扩展字符集
编译中的报错和解决方法整理
错误1:
CMake Error: your C compiler: “CMAKE_C_COMPILER-NOTFOUND” was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: your CXX compiler: “CMAKE_CXX_COMPILER-NOTFOUND” was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
CMake Error at cmake/os/Linux.cmake:41 (MESSAGE):
Unsupported compiler!
缺少gcc 和 gcc-c++ ,安装gcc、gcc-c++ 后再次编译即可
yum install gcc gcc-c++ -y
错误2:
CMake Error at cmake/boost.cmake:81 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=
This CMake script will look for boost in . If it is not there,
it will download and unpack it (in that directory) for you.
If you are inside a firewall, you may need to use an http proxy:
export http_proxy=http://example.com:80
没有指定boost路径,再cmake命令后加一条-DWITH_BOOST=boost/boost_1_59_0/再次编译即可
make -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \-DWITH_MYISAM_STORAGE_ENGINE=1 \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_ARCHIVE_STORAGE_ENGINE=1 \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DEXTRA_CHARSETS=all \-DWITH_BOOST=boost/boost_1_59_0/
错误3:
CMake Error at cmake/readline.cmake:64 (MESSAGE):
Curses library not found. Please install appropriate package,
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
删除之前的编译缓存文件CMakeCache.txt,安装ncurses-devel后再次编译
rm -fr CMakeCache.txt
yum install ncurses-devel -y
警告:
CMake Warning at cmake/bison.cmake:20 (MESSAGE):
Bison executable not found in PATH
Call Stack (most recent call first):
sql/CMakeLists.txt:514 (INCLUDE)
CMake Warning at cmake/bison.cmake:20 (MESSAGE):
Bison executable not found in PATH
编译成功但有Warning,Warning可以跳过,Error不可以跳过,若想解决此条Warning,删除CMakeCache.txt ,安装bison后再次编译即可
rm -fr CMakeCache.txt
yum install bison -y
编译成功
make
make install
编辑配置文件
cd /usr/local/lnmp/mysql/support-files/
cp my-default.cnf /etc/my.cnf
vim /etc/my.cnf
也可以不编辑,使用原始文档即可,编辑文档只是为了让别人可以更好的了解你的mysql配置
复制mysql服务脚本到/etc/init.d/mysqld
cp mysql.server /etc/init.d/mysqld
创建mysql组mysql用户
groupadd -g 27 mysql
useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/data -s /sbin/nologin mysql
修改/usr/local/lnmp/mysql/目录权限
cd /usr/local/lnmp/mysql/
chown -R mysql.mysql .
修改环境变量
cd /root
vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
source .bash_profile
初始化mysql
cd /usr/local/lnmp/mysql/
mysqld --initialize --user=mysql
最后黑色部分为初始化密码
测试mysql服务是否能成功启动
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
修改mysql目录权限,之前给的权限过大
chown root.root . -R
chown mysql data/
启动mysql服务
/etc/init.d/mysqld start
完成 mysql 安全设置,生产环境推荐使用
mysql_secure_installation
测试是否可以登陆
mysql -p
源码编译安装php
解决依赖性
yum install libmcrypt-devel-2.5.8-9.el6.x86_64.rpm \
libmcrypt-2.5.8-9.el6.x86_64.rpm \
re2c-0.13.5-1.el6.x86_64.rpm \
net-snmp-devel freetype-devel \
gmp-devel -y
下载解压php-5.6.35.tar.bz2 包
tar jxf php-5.6.35.tar.bz2
cd php-5.6.35
编译安装
./configure --prefix=/usr/local/lnmp/php \
--with-config-file-path=/usr/local/lnmp/php/etc \
--with-mysql=mysqlnd --enable-mysqlnd \
--with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl \
--with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir \
--with-png-dir --with-jpeg-dir \
--with-freetype-dir --with-pear --with-gettext --with-gmp \
--enable-inline-optimization --enable-soap --enable-ftp \
--enable-sockets --enable-mbstring --enable-fpm \
--with-fpm-user=nginx --with-fpm-group=nginx --with-mhash
make
make install
建立nginx用户
useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin nginx
配置php的配置文件
cd /usr/local/lnmp/php/etc/cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
24 ; Default Value: none
25 pid = run/php-fpm.pid #取消这一行的注释,打开pid
149 user = nginx 150 group = nginx #编译文件时已经写入了用户和组,如果没有写的话在这里更改
配置php.ini配置文件
cd /root/php-5.6.35cp php.ini-production /usr/local/lnmp/php/etc/php.ini
vim /usr/local/lnmp/php/etc/php.ini
date.timezone = Asia/Shanghai #更改时区
将php服务脚本加入/etc/init.d/php-fpm
cd /root/php-5.6.35/sapi/fpm/cp init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm #给脚本加可执行权限
/etc/init.d/php-fpm start #开启php服务
源码编译安装nginx
下载解压nginx和第三方模块sticky算法包
tar zxf nginx-sticky-module-ng.tar.gz
tar zxf nginx-1.10.1.tar.gz
cd nginx-1.10.1
解决依赖性
yum install pcre-devel
对nginx源码文件进行修改,隐藏版本号
cd /root/nginx-1.14.0/src/core/
vim nginx.h
14 #define NGINX_VER "nginx/" #不显示版本号
把debug模式编译去掉
cd /root/nginx-1.14.0/auto/cc/
vim gcc
171 # debug
172 #CFLAGS="$CFLAGS -g"
编译安装
./configure --prefix=/usr/local/lnmp/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-threads \
--with-file-aio \
--user=nginx \
--group=nginx \
make && make install
测试
使nginx支持php
编辑配置文件
cd /usr/local/lnmp/nginx/conf/
vim nginx.conf
location ~ /.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
制作软链接.启动nginx服务
ln -s /usr/local/lnmp/nginx/sbin/nginx /sbin/
nginx -t
nginx
编辑测试页
vim /usr/local/lnmp/nginx/html/index.php
在浏览器中访问172.25.31.1/index.php
搭建论坛
安装unzip工具,解压论坛模版 Discuz_X3.2_SC_UTF8.zip
yum install unzip
unzip Discuz_X3.2_SC_UTF8.zip -d /usr/local/lnmp/nginx/html/
修改权限,确保可以访问
cd /usr/local/lnmp/nginx/html/
mv upload/ bbs
cd bbs/
chmod 777 config data uc_client uc_server -R
在php.ini中写入mysql.sock的位置
cd /usr/local/lnmp/php/etc/
vim php.ini
1013 pdo_mysql.default_socket= /usr/local/lnmp/mysql/data/mysql.sock
1161 mysql.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
1220 mysqli.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
/etc/init.d/php-fpm reload
cd /usr/local/lnmp/mysql/
chmod 755 data/ -R #确保mysql.sock文件可以被访问
开启数据库
/etc/init.d/mysqld start
在浏览器中测试,是否可以登陆论坛
给php增加memcache模块
更改环境变量,使系统可以使用php中的命令
vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
source .bash_profile
编译
tar zxf memcache-2.2.5.tgz cd /root/memcache-2.2.5
phpize
安装
./configure makemake install
在配置文件中增加模块
vim /usr/local/lnmp/php/etc/php.ini
871 ; ... or under UNIX:
872 ;
873 extension=memcache.so
重新加载php-fpm服务,查看模块
/etc/init.d/php-fpm reload
php -m | grep memcache
测试模块是否可以正常使用
yum install memcached
/etc/init.d/memcached start
把测试页面放入nginx默认发布目录
cp memcache.php example.php /usr/local/lnmp/nginx/html/
cd /usr/local/lnmp/nginx/html/
vim memcache.php
22 define('ADMIN_USERNAME','root'); // Admin Username #用户名
23 define('ADMIN_PASSWORD','redhat'); // Admin Password #用户密码
28 $MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array
浏览器访问 172.25.31.1/memcache.php
压测
ab -c 10 -n 1000 http://172.25.31.1/index.php
ab -c 10 -n 1000 http://172.25.31.1/example.php
openresty
OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua
库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
我们用它来给linux增加一个memcache,也可以选择给linux增加一个memcache模块
先停用nginx
nginx -s stop
安装
tar zxf openresty-1.13.6.1.tar.gz
cd openresty-1.13.6.1
./configure --prefix=/usr/local/lnmp/openresty --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --user=nginx --group=nginx
make
make install
编辑配置文件
cd /usr/local/lnmp/openresty/nginx/conf/
vim nginx.conf
17 http {
18
19 upstream memcache {
20 server localhost:11211;
21 keepalive 512;
22 }
54 location /memc {
55 internal;
56 memc_connect_timeout 100ms;
57 memc_send_timeout 100ms;
58 memc_read_timeout 100ms;
60 set $memc_exptime 300;
61 memc_pass memcache;
62 }
81 location ~ \.php$ {
82 set $key $uri$args;
83 srcache_fetch GET /memc $key;
84 srcache_store PUT /memc $key;
85 root html;
86 fastcgi_pass 127.0.0.1:9000;
87 fastcgi_index index.php;
把测试页挪到openresty发布目录中
cp /usr/local/lnmp/nginx/html/example.php /usr/local/lnmp/openresty/nginx/html/
开启openresty中的nginx
/usr/local/lnmp/openresty/nginx/sbin/nginx
压测
ab -c 10 -n 1000 http://172.25.31.1/example.php