基于lnmp架构搭建论坛

                                                                 基于lnmp架构搭建论坛

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。

实验环境:

系统环境: RHEL6 x86-64 selinux and iptables disabled

一、Mysql 安装

1.安装软件包依赖性:

[root@server1 ~]# yum install -y gcc gcc-c++ ncurses-devel bison openssl-devel zlib-devel

[root@server1 ~]# yum install -y cmake-2.8.12.2-4.el6.x86_64.rpm      用cmake[有进度条]


2.编译安装数据库
[root@server1 ~]# tar zxf mysql-boost-5.7.11.tar.gz
[root@server1 ~]# cd mysql-5.7.11/

[root@server1 mysql-5.7.11]# 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 \ #Unix socket 文件路径

> -DWITH_INNOBASE_STORAGE_ENGINE=1 \ #安装 innodb 存储引擎

> -DWITH_MYISAM_STORAGE_ENGINE=1 \ #安装 myisam 存储引擎

> -DDEFAULT_CHARSET=utf8 \  #使用 utf8 字符

> -DDEFAULT_COLLATION=utf8_general_ci \  #校验字符

> -DEXTRA_CHARSETS=all \  #安装所有扩展字符集

> -DWITH_BOOST=/root/mysql-5.7.11/boost/boost_1_59_0/  


[root@server1 mysql-5.7.11]# make && make install


注意:

重新编译时,需要清除旧的对象文件和缓存信息
make clean
rm -f CmakeCache.txt


3.完成mysql初始化 [创建所需的用户及组]

[root@server1 mysql]# groupadd -g 27 mysql

[root@server1 mysql]# useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/ mysql

[root@server1 local]# usermod -s /sbin/nologin mysql
[root@server1 mysql]# chown mysql.mysql . -R

Screenshot from 2018-07-10 18-28-47.png


4.解析mysql/bin的执行脚本


[root@server1 ~]# vim .bash_profile
Screenshot from 2018-07-10 18-32-44.png


[root@server1 ~]# source .bash_profile 


Screenshot from 2018-07-10 18-36-50.png


[root@server1 mysql]# chown root . -R


Screenshot from 2018-07-10 18-36-03.png


5.修改配置文件中的数据库存放目录socket 文件路径与编译时一致


[root@server1 support-files]# vim /etc/my.cnf

Screenshot from 2018-07-10 19-26-43.png

6.拷贝执行脚本到本地执行目录下

[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld

Screenshot from 2018-07-10 19-30-51.png


7.完成初始化 指定用户 并创建数据库


[root@server1 mysql]# mysqld --initialize --user=mysql

Screenshot from 2018-07-10 19-41-43.png


[root@server1 mysql]# chown mysql data -R   更改数据目录所有人为mysql


Screenshot from 2018-07-10 19-42-21.png


开启数据库 并修改密码

[root@server1 mysql]# /etc/init.d/mysqld start

Screenshot from 2018-07-10 19-44-53.png


[root@server1 mysql]# mysql_secure_installation    #按提示完成 mysql 安全设置,生产环境推荐使用



二、Php 安装

1.解决安装所需依赖性

下载包

php-5.6.20.tar.bz2

安装依赖性

[root@server1 ~]# yum install libjpeg-turbo-devel-1.2.1-1.el6.x86_64
[root@server1 php-5.6.20]# yum install -y libxml2-devel
[root@server1 php-5.6.20]# yum install -y openssl-devel
[root@server1 php-5.6.20]# yum install -y curl-devel
[root@server1 ~]# yum install -y gd-devel-2.0.35-11.el6.x86_64.rpm
[root@server1 ~]# yum install -y gmp-devel
[root@server1 ~]# yum install libmcrypt-devel-2.5.8-9.el6.x86_64.rpm libmcrypt-2.5.8-9.el6.x86_64.rpm -y
[root@server1 ~]# yum install -y net-snmp-devel
[root@server1 ~]# yum install re2c-0.13.5-1.el6.x86_64.rpm -y

2.编译安装

[root@server1 ~]# tar jxf php-5.6.20.tar.bz2   解包

[root@server1 php-5.6.20]# ./configure --help  寻找帮助
--with-openssl   指定驱动 加密

--with-gd   图

--with-zlib       php网页压缩(防盗链)

--with-pear     功能模块的组件(安装)

执行

[root@server1 php-5.6.20]# ./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


输出的信息

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| 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.


[root@server1 php-5.6.20]# make && make install


3.配置php解析文件/配置php-fpm配置文件

[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default
[root@server1 etc]# pwd
/usr/local/lnmp/php/etc

[root@server1 php-5.6.20]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini   
[root@server1 fpm]# pwd
/root/php-5.6.20/sapi/fpm
[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm       启动脚本
[root@server1 fpm]# chmod +x /etc/init.d/php-fpm         加执行权限


4.修改php配置文件 [pid][timezone]


[root@server1 etc]# vim php-fpm.conf

[root@server1 etc]# vim php.ini

Screenshot from 2018-07-10 21-38-16.png

添加php配置文件中指定的用户 并启动php

[root@server1 etc]# useradd -u 800 -M -d /usr/local/lnmp/nginx nginx
[root@server1 etc]# /etc/init.d/php-fpm start
Screenshot from 2018-07-10 21-41-02.png


三、Nginx 安装

1.安装依赖性并编译

[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@server1 ~]# cd nginx-1.10.1
[root@server1 core]# pwd
/root/nginx-1.10.1/src/core
[root@server1 core]# vim nginx.h
Screenshot from 2018-07-10 21-47-58.png

#define NGINX_VER "nginx" (修改此行, 去掉后面的 “ NGINX_VERSION”,为了安全,这样编译后外界无法获取程序的版本号)

[root@server1 cc]# pwd
/root/nginx-1.10.1/auto/cc
[root@server1 cc]# vim gcc

#CFLAGS=”$CFLAGS -g” (注释掉这行,去掉 debug 模式编译,编译以后程序只有几百 k)


安装依赖

[root@server1 nginx-1.10.1]# yum install -y pcre-devel

编译

./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-threads --user=nginx --group=nginx

安装

[root@server1 nginx-1.10.1]# make && make install

做软链接

[root@server1 sbin]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/

[root@server1 sbin]# nginx -t      检测语法

[root@server1 ~]# nginx     开启
Screenshot from 2018-07-10 22-00-38.png


四、解决nginx和php的依赖关系

编辑nginx配置文件 修改tcp zip php

[root@server1 conf]# pwd
/usr/local/lnmp/nginx/conf
[root@server1 conf]# vim nginx.conf

Screenshot from 2018-07-10 22-06-44.png

Screenshot from 2018-07-10 22-07-20.png

Screenshot from 2018-07-10 22-09-16.png

[root@server1 conf]# nginx -t     检测语法
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload    重新加载


创建nginx访问php主页的页面


[root@server1 html]# pwd
/usr/local/lnmp/nginx/html
[root@server1 html]# vim index.php
[root@server1 html]# cat index.php
<?php
phpinfo()
?>

设置index.php为访问ip时默认访问的网页

[root@server1 conf]# vim nginx.conf

Screenshot from 2018-07-10 22-16-26.png


[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

浏览器访问

Screenshot from 2018-07-10 22-17-34.png


五、解决php和mysql的依赖关系

[root@server1 etc]# pwd
/usr/local/lnmp/php/etc
[root@server1 etc]# vim php.ini

Screenshot from 2018-07-10 22-35-03.png

Screenshot from 2018-07-10 22-37-03.png

[root@server1 etc]# /etc/init.d/php-fpm reload           平滑加载


六、搭建基于lnmp的论坛


下载 Discuz_X3.2_SC_UTF8.zip

[root@server1 ~]# yum install -y unzip
[root@server1 ~]# unzip Discuz_X3.2_SC_UTF8.zip   解压

修改解压出的目录名字及位置

[root@server1 ~]# mv upload/ /bbs
Screenshot from 2018-07-10 22-50-56.png


ngnx设置虚拟主机 实现基于域名访问nginx

[root@server1 conf]# vim nginx.conf
Screenshot from 2018-07-10 23-16-53.png

[root@server1 conf]# nginx -s reload

注意:浏览器访问主机做好解析,/etc/hosts


访问:bbs.cara.org/install

Screenshot from 2018-07-10 23-18-23.png


Screenshot from 2018-07-10 23-19-09.png


根据提示 改权限

[root@server1 bbs]# chmod 777 config/ data
[root@server1 bbs]# chmod 777 config/ data -R

[root@server1 bbs]# chmod 777 uc_client/ uc_server/ -R
[root@server1 mysql]# chmod 755 data


[root@server1 ~]# /etc/init.d/mysqld restart

Screenshot from 2018-07-10 23-30-25.png


Screenshot from 2018-07-10 23-32-06.png

如下,已经完成!


Screenshot from 2018-07-10 23-35-47.png


查看数据库:

Screenshot from 2018-07-10 23-59-16.png


七、memcache 基于php的cache


1.安装memcache

修改环境变量

[root@server1 bin]# vim ~/.bash_profile

Screenshot from 2018-07-11 21-29-32.png

[root@server1 bin]# source ~/.bash_profile    


[root@server1 ~]# tar zxf memcache-2.2.5.tgz

Screenshot from 2018-07-11 21-33-37.png


编译安装memcache

[root@server1 memcache-2.2.5]# ./configure --prefix=/usr/local/lnmp/php/memcache
[root@server1 memcache-2.2.5]# make && make install

Screenshot from 2018-07-11 21-36-55.png


[root@server1 etc]# pwd
/usr/local/lnmp/php/etc
[root@server1 etc]# vim php.ini


[root@server1 etc]# /etc/init.d/php-fpm reload     重新加载


拷贝文件

[root@server1 memcache-2.2.5]# cp example.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-2.2.5]# cp memcache.php /usr/local/lnmp/nginx/html/


2.搭建一个memcache服务器


[root@server2 ~]# yum install -y memcached
[root@server2 ~]# /etc/init.d/memcached start      开启服务

[root@server2 ~]# yum install -y telnet   

telnet命令用于登录远程主机,对远程主机进行管理。telnet采用明文传送报文,安全性不好;

Screenshot from 2018-07-11 21-45-44.png



[root@server1 html]# vim example.php


Screenshot from 2018-07-11 21-53-53.png

[root@server1 html]# vim memcache.php

Screenshot from 2018-07-11 21-53-40.png

(client -> nginx -> php-fpm -> php -> memcached -> mysql)

Screenshot from 2018-07-11 21-59-17.png


猜你喜欢

转载自blog.51cto.com/13362895/2140637