欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lnmp架构之源码安装

程序员文章站 2022-05-17 18:40:53
...

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

  • Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
  • Mysql是一个小型关系型数据库管理系统。
  • PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。

一、MySQL源码安装

yum安装解决依赖性:yum install -y gcc gcc-c++ make ncurses-devel bison openssl-devel zlib-devel cmake
准备的安装包:    mysql-boost-5.7.11.tar.gz 
[root@server1 mnt]# tar zxf mysql-boost-5.7.11.tar.gz 
[root@server1 mnt]# 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 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
#安装目录
-DMYSQL_DATADIR=/usr/local/mysql/data \
#数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \ #Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \
#安装 myisam 存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
#安装 innodb 存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
#安装 archive 存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
#安装 blackhole 存储引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 \
#安装数据库分区
-DENABLED_LOCAL_INFILE=1 \
#允许从本地导入数据
-DWITH_READLINE=1 \
#快捷键功能
-DWITH_SSL=yes \
#支持 SSL
-DDEFAULT_CHARSET=utf8 \
#使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \
#校验字符
-DEXTRA_CHARSETS=all \
#安装所有扩展字符集
-DMYSQL_TCP_PORT=3306 \
#MySQL 监听端口

make编译时报错

Lnmp架构之源码安装

解决方案:
[root@server1 mysql-5.7.11]# rm -fr CMakeCache.txt     ##移除旧的对象文件
[root@server1 mysql-5.7.11]# mkdir -p /usr/local/boost
[root@server1 mysql-5.7.11]# cd /usr/local/boost/
[root@server1 boost]# wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
##然后再重新编译一遍

##编译时指定boost库
[root@server1mysql-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 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=boost/boost_1_59_0/

开始安装

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

Lnmp架构之源码安装

安装完成后,接下来开始初始化设置。

[root@server1 mysql-5.7.11]# cd /usr/local/lnmp/mysql/support-files/
[root@server1 support-files]# ls
magic  my-default.cnf  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@server1 support-files]# mv /etc/my.cnf /etc/my.cnf.back
[root@server1 support-files]# cp my-default.cnf /etc/my.cnf         ##复制mysql的配置文件
[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld    ##复制mysql的执行脚本
[root@server1 support-files]# chmod +x /etc/init.d/mysqld           ##添加可执行权限
[root@server1 support-files]# groupadd -g 27 mysql                  ##mysql默认UID,GID均为27
[root@server1 support-files]# useradd -u 27 -g 27 -d /usr/local/lnmp/mysql/data -M -s /sbin/nologin mysql                                               

[root@server1 bin]# vim /root/.bash_profile 
======================================================================
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin        ##添加环境变量  
======================================================================

[root@server1 bin]# source /root/.bash_profile    ##运行环境变量
[root@server1 mysql]# vim /etc/my.cnf             ##编辑配置文件
-------------------------------------------------------------------------
 18 basedir = /usr/local/lnmp/mysql                                    
 19 datadir =  /usr/local/lnmp/mysql/data         ##添加mysql数据目录    
 20 port = 3306                                                        
 21 # server_id = .....                                                
 22 socket = /usr/local/lnmp/mysql/data/mysql.sock                     
-------------------------------------------------------------------------
[root@server1 mysql]# chown mysql data/ -R 
[root@server1 mysql]# ll
total 60
drwxr-xr-x  2 root  root  4096 Aug  5 10:18 bin
-rw-r--r--  1 root  root 17987 Feb  2  2016 COPYING
drwxrwxrwx  6 mysql root  4096 Aug  5 15:28 data
drwxr-xr-x  2 root  root  4096 Aug  5 10:17 docs
drwxr-xr-x  3 root  root  4096 Aug  5 10:17 include
drwxr-xr-x  4 root  root  4096 Aug  5 10:18 lib
drwxr-xr-x  4 root  root  4096 Aug  5 10:18 man
drwxr-xr-x 10 root  root  4096 Aug  5 10:19 mysql-test
-rw-r--r--  1 root  root  2478 Feb  2  2016 README
drwxr-xr-x 28 root  root  4096 Aug  5 10:19 share
drwxr-xr-x  2 root  root  4096 Aug  5 10:19 support-files
[root@server1 mysql]# mysql_install_db         ## 初始化命令
2018-06-29 11:01:28 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-06-29 11:01:28 [ERROR]   The data directory needs to be specified.
[aaa@qq.com mysql]# mysqld --initialize --user=mysql   
2018-07-01T10:48:56.964538Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-01T10:48:56.964624Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2018-07-01T10:48:56.964629Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2018-07-01T10:48:59.917842Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-07-01T10:49:00.667612Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-07-01T10:49:00.981175Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5d212cdb-7d1c-11e8-8030-525400b70b5b.
2018-07-01T10:49:01.124832Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
##注意,最后一行设置了mysql的初始密码(<hm#wI&4d4H)
2018-07-01T10:49:01.126243Z 1 [Note] A temporary password is generated for aaa@qq.com: <hm#wI&4d4H

启动mysql

[aaa@qq.com mysql]# /etc/init.d/mysqld start
Starting MySQL SUCCESS! 
[aaa@qq.com mysql]# mysql -p
Enter password: (<hm#wI&4d4H)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| discuz             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.04 sec)

修改密码

[aaa@qq.com mysql]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 
New password:
Re-enter new password:
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

二、php源码安装

解决依赖性:yum install -y libxml2-devel openssl-devel curl-devel libjpeg-turbo-devel libpng-devel freetype-devel gmp-devel net-snmp-devel
安装包:libmcrypt-2.5.8-9.el6.x86_64.rpm 
       libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
[aaa@qq.com mnt]# tar jxf php-5.6.35.tar.bz2 
[aaa@qq.com mnt]# cd php-5.6.35
[aaa@qq.com php-5.6.35]#  ./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
## mcrypt mhash  php 加密算法扩展库
##安装
[root@server1 php-5.6.35]# make && make install 
##添加并更改配置
[root@server1 php-5.6.35]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
[root@server1 php-5.6.35]# cd /usr/local/lnmp/php/           
[root@server1 php]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@server1 php]# vim etc/php-fpm.conf
-------------------------------------------------------------------------
 21 [global]
 22 ; Pid file
 23 ; Note: the default prefix is /usr/local/lnmp/php/var
 24 ; Default Value: none
 25 pid = run/php-fpm.pid            
 -------------------------------------------------------------------------
##修改时区
[aaa@qq.com php]# vim etc/php.ini 
 933 [Date]
 934 ; Defines the default timezone used by the date functions
 935 ; http://php.net/date.timezone
 936 date.timezone = Asia/Shanghai
##添加启动脚本
[root@server1 php]# cd /mnt/php-5.6.35/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 
[root@server1 fpm]# /etc/init.d/php-fpm start
Starting php-fpm  done

Lnmp架构之源码安装

三、nginx源码安装

Yum:yum install pcre-devel openssl-devel gcc -y
nginx包:nginx-1.10.1.tar.gz 
需添加模块:nginx-sticky-module-ng.tar.gz 
[root@server1 fpm]# useradd -u 800 -M -d /usr/local/lnmp/nginx nginx   ##创建nginx用户,为了使得“权限最小”,仅允许nginx使用和访问nginx服务
[root@server1 mnt]# tar zxf nginx-1.10.1.tar.gz 
[root@server1 mnt]# tar zxf nginx-sticky-module-ng.tar.gz 
[root@server1 mnt]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# vim src/core/nginx.h 
///////////////////////////////////////////////////////
 12 #define nginx_version      1010001
 13 #define NGINX_VERSION      "1.10.1"
 14 #define NGINX_VER          "nginx/"     ##对外隐藏版本
///////////////////////////////////////////////////////
##取消C调试编译,可以有效提升效率
[root@server1 nginx-1.10.1]# vim auto/cc/gc
178 # debug
179 #CFLAGS="$CFLAGS -g"

[root@server1 fpm]# cd /mnt/nginx-1.10.1
[root@server1 nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
[root@server1 nginx-1.10.1]# make & make install
[root@server1 nginx-1.10.1]# vim /usr/local/nginx/conf/nginx.conf

 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 70             include        fastcgi.conf;
 71         }
[aaa@qq.com nginx-1.10.1]# chmod +x /etc/init.d/nginx 
[aaa@qq.com html]# vim /usr/local/lnmp/nginx/conf/nginx.conf
***************************************************************************
 43         location / {
 44             root   html;
 45             index  index.php index.html index.htm;
 46         }
***************************************************************************
[aaa@qq.com nginx-1.10.1]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]

Lnmp架构之源码安装

[root@server5 nginx]# ln -s /usr/local/lnmp/nginx/sbin/nginx /sbin/
[root@server5 nginx]# nginx -t              ##检测nginx配置语法
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@server5 nginx]# vim /usr/local/lnmp/nginx/html/index.php
<?php
phpinfo()
?>
[root@server5 nginx]# nginx                  ##启动nginx
[root@server5 nginx]# /etc/init.d/php-fpm start 
Starting php-fpm  done
[root@server5 nginx]# nginx -s reload         ##nginx重载

测试

Lnmp架构之源码安装

四、Discuz论坛搭建

[aaa@qq.com bbs]# cd /usr/local/nginx/html/
[aaa@qq.com html]# unzip Discuz_X3.2_SC_UTF8.zip 
[aaa@qq.com html]# chmod -R 777 /usr/local/nginx/html/
[aaa@qq.com html]# mkdir bbs
[aaa@qq.com html]# mv upload/ bbs
[aaa@qq.com bbs]# ls
admin.php  connect.php      forum.php  member.php  search.php  uc_server
api        cp.php           group.php  misc.php    source      upload
api.php    crossdomain.xml  home.php   plugin.php  static      userapp.php
archiver   data             index.php  portal.php  template
config     favicon.ico      install    robots.txt  uc_client
[aaa@qq.com bbs]# pwd
/usr/local/lnmp/nginx/html/bbs

[aaa@qq.com html]# vim /usr/local/lnmp/php/etc/php.ini
===========================================================================
1013 pdo_mysql.default_socket=/usr/local/lnmp/mysql/data/mysql.sock
1162 mysql.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
1221 mysqli.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
===========================================================================
1、bbs下文件目录权限不足,修改权限
[aaa@qq.com bbs]# chmod 777 uc_server/ config/ data/ uc_client/ -R
2、目录不存在
[root@server5 bbs]# vim /usr/local/lnmp/php/etc/php.ini
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1013 pdo_mysql.default_socket=/usr/local/lnmp/mysql/data/mysql.sock
1162 mysql.default_socket =/usr/local/lnmp/mysql/data/mysql.sock
1221 mysqli.default_socket =/usr/local/lnmp/mysql/data/mysql.sock
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Lnmp架构之源码安装
Lnmp架构之源码安装

登录进入论坛

Lnmp架构之源码安装

五、php添加memcache缓存模块

1、安装memcache

[root@server5 mnt]# tar zxf memcache-2.2.5.tgz     
[root@server5 ~]# vim ~/.bash_profile 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[root@server5 ~]# source ~/.bash_profile 
[root@server5 ~]# cd memcache-2.2.5
[root@server5 memcache-2.2.5]# phpize 
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[root@server5 memcache-2.2.5]# ./configure --prefix=/usr/local/lnmp/php/memcache 
[root@server5 memcache-2.2.5]# make && make install 

2、memcache配置

[aaa@qq.com memcache-2.2.5]# vim /usr/local/lnmp/php/etc/php.ini
---------------------------------------------------------------------
 871 ; ... or under UNIX:
 873 extension=memcache.so
 874 ;

---------------------------------------------------------------------
[root@server5 memcache-2.2.5]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@server5 memcache-2.2.5]# php -m | grep memcache   ##php -m 查看加载的模块
memcache
[root@server5 memcache-2.2.5]# cp example.php /usr/local/lnmp/nginx/html/
[root@server5 memcache-2.2.5]# cp memcache.php /usr/local/lnmp/nginx/html/
[root@server5 memcache-2.2.5]# vim /usr/local/lnmp/nginx/html/memcache.php 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 22 define('ADMIN_USERNAME','memcache');    // Admin Username   (默认用户名:memcache)
 23 define('ADMIN_PASSWORD','password');    // Admin Password   (默认密码:password)
 24 define('DATE_FORMAT','Y/m/d H:i:s');
 25 define('GRAPH_SIZE',200);
 26 define('MAX_ITEM_DUMP',50);
 27 
 28 $MEMCACHE_SERVERS[] = '172.25.18.5:11211'; // add more as an array
 29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[root@server5 memcache-2.2.5]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@server5 memcache-2.2.5]# nginx -s reload
[root@18 ~]# ab -c 100 -n 100 http://172.25.18.5/memcache.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.25.18.5 (be patient).....done


Server Software:        nginx/
Server Hostname:        172.25.18.5
Server Port:            80

Document Path:          /memcache.php
Document Length:        101 bytes

Concurrency Level:      100
Time taken for tests:   0.150 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Non-2xx responses:      100
Total transferred:      31600 bytes
HTML transferred:       10100 bytes
Requests per second:    665.41 [#/sec] (mean)
Time per request:       150.284 [ms] (mean)
Time per request:       1.503 [ms] (mean, across all concurrent requests)
Transfer rate:          205.34 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   0.4      2       3
Processing:     4   78  40.2     79     144
Waiting:        2   78  40.3     79     144
Total:          4   80  40.0     81     146

Percentage of the requests served within a certain time (ms)
  50%     81
  66%    103
  75%    114
  80%    121
  90%    135
  95%    142
  98%    146
  99%    146
 100%    146 (longest request)
example.php的页面是直接访问了服务器,而memcache的页面是缓存

六、遇到的错误及解决办法:

错误1:PHP安装过程中,编译成功完成,结果在make的时候报错!!
libtool: link: `ext/opcache/ZendAccelerator.lo' is not a valid libtool object

解决办法:

1.# make distclean
或者 make clean
2.重新编译./configure..   ->make

错误2:配置好php和nginx之后,访问index.php时无法显示页面

原因:php是动态页面,nginx默认不支持。所以需要在配置文件中添加:

解决办法:

[root@server5 bbs]# 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;                                         
        }
…………………………………………………………………………………………………………………………………………………………………………………………………………
相关标签: lnmp