阅读(7138)

lnmp架构的安装原理与步骤

最后一次修改 2018年05月07日

LNMP就是Linux+Nginx+MySQL+PHP,Linux作为服务器的操作系统,MySQL即为数据库。是一组搭建动态网站的开源软件架构,本身是各自独立的软件服务,放在一起使用拥有很高的兼容性,共同组成了一个强大的WEB应用程序平台。

Nginx为一款高性能Web服务器,本身是不能处理PHP的,当接收到请求时,判断如果是PHP请求就会将请求交给PHP解释器处理,然后将结果返回给Client。Nginx一般把请求转发给fast-cgi管理进程处理,fast-cgi管理进程再选择cgi子进程处理请求,然后把结果返给Nginx。

LNMP就是Linux+Nginx+MySQL+PHP,Linux作为服务器的操作系统,MySQL即为数据库。

区别(lamp和lnmp) 
Apache一般是把PHP当做自己的一个模块来启动;而Ngnix则是把http请求变量转发给PHP进程,即PHP独立进程,与Ngnix通信,这种方式叫做Fast-CGI运行方式。所以Apache所编译的PHP不能用于Nginx。

lnmp的搭建步骤

一、mysql源码安装

源码编译安装给20G吧,9G是不够的!!!!!!

1.安装包 
yum install -y gcc gcc-c++ ncurses-devel bison cmake-2.8.12.2-4.el6.x86_64.rpm–因为6.5的cmake版本低,需要升级版本————–偏商业化(查看cmake版本yum list cmake)

源码包:mysql-boost-5.7.17.tar.gz

2.解压安装 

tar zxf mysql-boost-5.7.17.tar.gz

在解压目录下: 
cmake -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 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=/root/mysql-5.7.17/boost/boost_1_59_0/

-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 存储引擎
-DDEFAULT_CHARSET=utf8 \    #默认使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \   #校验字符
-DEXTRA_CHARSETS=all \  #安装所有扩展字符集
-DWITH_BOOST=/mnt/mysql-5.7.11/boost/boost_1_59_0/  ##安装过程中指定下载目录123456789

make && make install

在编译过程中出现错误应该清理缓存之后在重新编译 
make clean #清除缓存 
rm -rf CMakeCache.txt #编译出错时,也需要删除缓存

3.数据库的初始化

(1)数据库用户的创建 
groupadd -g 27 mysql 
useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/ mysql 
usermod -s /sbin/nologin mysql #安全设置,禁止交互式登陆

(2)环境的配置 
vim ~/.bash_profile #设置环境变量,方便登陆

PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
cp /usr/local/lnmp/mysql/support-files/mysql.server /etc/init.d/mysqld  #添加启动脚本12

(3)更改配置文件 
首先备份然后复制需要的过去 
cp /etc/my.cnf /etc/my.cnf.bak 
cp /usr/local/lnmp/mysql/support-files/my-default.cnf /etc/my.cnf

vim /etc/my.cnf

[mysqld]
datadir=/usr/local/lnmp/mysql/data
socket=/usr/local/lnmp/mysql/data/mysql.sock
user=mysql1234

(4)数据库的初始化与更改密码 
mysqld –initialize –user=mysql #初始化,复制临时密码 
chown root.mysql /usr/local/lnmp/mysql -R 
chown mysql /usr/local/lnmp/mysql/data -R 
/etc/init.d/mysqld start 
mysql_secure_installation #设置密码 
mysql -p #登陆

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[root@server1 mysql]# mysql_secure_installation 
New password:           -----新密码
Re-enter new password: 
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: ----是否检测密码强度(回车默认no)
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : ----------是否更改root密码
 ... skipping.
 By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.--------删除匿名用户
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y------------取消root用户远程登陆
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y------------删除test库和对test库的访问权限
 - Dropping test database...
Success.
 - Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y---------刷新授权表使修改生效
Success.
All done! 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

二、php源码安装

  1. 依赖包的安装 
    yum install -y libxml2-devel openssl-devel curl-devel libjpeg-turbo-devel gd-devel-2.0.35-11.el6.x86_64.rpm(图形、图片) libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm re2c-0.13.5-1.el6.x86_64.rpm gmp-devel net-snmp-devel libpng-devel freetype-devel

源码包:php-5.6.35.tar.bz2

2.解压安装 
tar jxf php-5.6.35.tar.bz2

在解压目录下: 
./configure –prefix=/usr/local/lnmp/php –with-config-file-path=/usr/local/lnmp/php/etc/ –enable-mysqlnd –with-mysql=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-mcrypt –with-mhash

make && make install

3.php的配置 
(1)查看安装的php大小 
cd /usr/local/lnmp/php/ 
du -sh(一般是这么大109M . 
(2)复制文件便于后续操作 
cd /root/php-5.6.35 
cp php.ini-production /usr/local/lnmp/php/etc/php.ini(一定要用.ini结尾) 
cd /root/php-5.6.35/sapi/fpm/ 
file init.d.php-fpm ##查看文件类型 
cp init.d.php-fpm /etc/init.d/php-fpm ##企业六里面脚本的固定位置 
chmod +x /etc/init.d/php-fpm 
(3)配置文件的更改 
cd /usr/local/lnmp/php/etc/ 
ls 
vim php.ini ##更改时区

  925 date.timezone = Asia/shanghai1

(4)开启php-fpm服务 
cp php-fpm.conf.default php-fpm.conf 
vim php-fpm.conf

25 pid = run/php-fpm.pid1

/etc/init.d/php-fpm start (只有用户创建了才能起来服务,否则没有合法的身份启动) 
(5)nginx用户的创建 
useradd -u 800 -M -d /usr/local/lnmp/nginx nginx

三、nginx的源码安装

1.依赖包的安装 
yum install pcre-devel openssl-devel zlib-devel -y

源码包: 
nginx-1.14.0.tar.gz

2.解压安装 
tar zxf nginx-1.14.0.tar.gz

(1)版本号的隐藏: 
vim /mnt/nginx-1.10.1/src/core/nginx.h

  #define NGINX_VER          "nginx/" #为了安全,不显示版本号1

(2)减少编译内存 
vim /mnt/nginx-1.10.1/auto/cc/gcc

   # debug
    #CFLAGS="$CFLAGS -g"   #关闭DEBUG,减少内存12

在解压目录下: 
./configure –prefix=/usr/local/lnmp/nginx –with-http_ssl_module –with-http_stub_status_module –with-file-aio –with-threads –user=nginx –group=nginx #需要的模块自定义

make && make install

ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/

3.添加php的路径 
cd /usr/local/lnmp/php 
vim ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnm
p/php/bin12

source ~/.bash_profile

4.更改配置文件,打开nginx服务 
vim /usr/local/lnmp/nginx/conf/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;
    }
    server {
            listen 80;
            server_name bbs.xinhao.com;
            location / {
                    root /bbs;
                    index index.php;
            }

检查是否配置成功nginx -t 
开启服务: nginx

测试: 
/usr/local/lnmp/nginx/html/ 
vim index.php ##php测试页的编写 

##经过上述三大步,Lnmp框架基本搭建成功了,下面来我们通过搭建论坛来测试一下

四、简单论坛的搭建 
1. unzip Discuz_X3.2_SC_UTF8.zip ##解压

cd readme/ 
less readme.txt ##查看安装过程

mv upload/ /usr/local/lnmp/nginx/html/ ##将论坛推到nginx默认发布目录

cd /usr/local/lnmp/nginx/html/bbs 
chmod 777 config/ data/ uc_client/ uc_server/ -R

cd /usr/local/lnmp/php/etc/ 
vim php.ini (修改如下)

   1001 pdo_mysql.default_socket= /usr/local/lnmp/mysql/data/mysql.sock    
   1150 mysql.default_socket = /usr/local/lnmp/mysql/data/mysql.sock    1210 mysqli.default_socket = /usr/local/lnmp/mysql/data/mysql.sock123

6.更改完配置文件刷新服务,查看端口

/etc/init.d/php-fpm reload 
查看9000端口: netstat -antlp | grep :9000 
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 7491/php-fpm

7.浏览器测试 
此时进去发现数据库权限被拒:授权 chmod 755 /usr/local/lnmp/mysql/data/ 
此时安装完成。