APP下载

架构稳定的LAMP系统及性能优化

2019-10-21周玉双

科学导报·学术 2019年36期

周玉双

摘要:LAMP(Linux+Apache+MySQL+PHP)网站架构是性能非常稳定的Web框架,该框架包括:Linux操作系统、Apache网络服务器、MySQL数据库、PHP编程语言,所有组成产品均是开源软件。LAMP具有轻量、通用、跨平台、高性能、低价格、资源丰富、快速开发的优势,因此LAMP无论是性能、质量还是价格都是企业搭建网站的首选平台。

关键词:Linux;Apache;PHP;Mysql;GD;http

正文

1 安装Linux系统

1.1 启动字符界面

为了调试方便,修改linux的启动进程,以便直接启动到字符界面下。

#vi inittab

修改:id:5:initdefault:为id:3:initdefault:

1.2 编译内核启动配置文件

# vi /etc/sysctl.conf

编辑/etc/sysctl.conf文件,增加三行:

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

2 设置Mysql数据库

2.1 修改数据库密码

#mysql –u root –p(进入mysql数据库,开始密码为空)

#use mysql

#update user set password=password(yourpassword)where user=root;

#flush privileges;

2.2 修改数据库最大连接数

#vi my.cnf

在[Mysqld]中添加

max_connections=20000(设置mysql数据库最大连接数,默认为100)

#mysqladmin –u root –p variables|grep max_connections

显示:| max_connections|20000|

2.3 禁止远程连接数据库

在命令行netstat -ant下看到3306端口是打开的,为了禁止该功能,启动skip-networking,不监听sql的任何TCP/IP的连接,切断远程访问的权利,保证安全性。

2.4 用户目录权限限制

默认的mysql是安装在/usr/local/mysql,而对应的数据库文件在/usr/local/mysql/var目录下,因此,必须保证该目录不能让未经授权的用户访问后把数据库打包拷贝走了,所以要限制对该目录的访问。确保mysqld运行时,只使用对数据库目录具有读或写权限的linux用户来运行。

3 安装apache和php

3.1 编译Apache

#./configure --prefix=/usr/local/apache --enable-module=so --enable-module=rewrite --enable-shared=max --enable-module=most

3.2 编译apache的限制IP并发数的模块

#/usr/local/apache/bin/apxs –c –i –a mod_limitipconn.c

3.3 卸载GD库

重新编译GD库,编译时必须加上相应参数

#./configure –prefix=/usr/local/gd –with-png=/usr/lib –with-jpeg=/usr/lib –with-freetype=/usr/lib –with-xpm=/usr/lib

3.4 編译php

#./configure --prefix=/usr/local/php --with-mysql --with-apxs=/usr/local/apache/bin/apxs --with-xml --enable-ftp --enable-force-cgi-redirect --enable-trans-sid --enable-track-vars --enable-url-includes --enable-sockets --with-gd=/usr/local/gd --with-zlib-dir=/usr/lib --with-gdbm-dir=/usr/lib

3.5 COPY PHP的配置文件

cp../php4.3.4/php.ini.dist /usr/local/php/lib/php.ini

修改php.ini文件

register_globals = On

3.6 编辑httpd.conf文件

修改DocumentRoot "/yourdir

查找

把#ExtendedStatus On这一行注释掉,

添加

#this is my new mod

MaxConnPerIP 1(每个IP用户的最大连接数)

这里的 / 代表所有目录

ok!重新启动一下apache服务器

/usr/local/apache/bin/apachectl restart

4 测试

写个php测试页info.php:内容如下

<?php

phpinfo();

?>

正常的话,应该能看到php的信息了,恭喜你的Apche+Mysql +PHP安装成功。

如果不正常,有Cannot load /usr/local/apache/libexec/libphp4.so into server等提示,那就是系统的SELINUX没有关闭。

修改/etc/selinux/config文件中的SELINUX="" 为 disabled,然后重启。

到此,LAMP系统架构及性能优化全部完成。

参考文献:

[1] Michael D Bauer.Linux Server Security[M].O'Reilly,2005.

[2] Russel Dyer.MySQL in a Nutshell[M].O'Reilly,2005.

[3] Ivan Ristic.Apache Security[M].O'Reilly,2005.

[4] John Coggeshall,ncy Malcolm.PHP Security Collection[M].O' Reilly,2004.

(作者单位:吉化集团信息网络技术有限公司)