开发者社区> 问答> 正文

Linux 环境搭建教程 Linux+Apache+Mysql+PHP (已测试阿里云CentOS5.4安装成功)

终于入手了阿里云CentOS 5.4 32位,环境搭建成功,本人属于LINUX菜鸟级的,只是为了方便整理下整个安装过程分享给大家。还希望高手多多赐教。(同样适用LINUX 64位的安装,根据需要更换相应的兼容程序包)

. 系统设定

软件源代码包存放位置:/usr/local/src
源码包编译安装位置(prefix) :/usr/local/software_name
脚本以及维护程序存放位置:/usr/local/sbin
MySQL 数据库位置:/var/lib/MySQL (可按情况设置)
Apache 网站根目录:/home/www/wwwroot (可按情况设置)
Apache 虚拟主机日志根目录:/home/www/logs (可按情况设置)
Apache 运行账户:www:www




二. 前期准备

1. 修改服务器密码 # passwd
# 填写你要重设的密码
# 再次输入




2. 卸载自带的httpd (阿里云的Linux自带httpd,我们需要卸载它。) # rpm -e gnome-user-share
# rpm -e httpd




3. 关闭不需要的服务 # ntsysv
以下仅列出需要启动的服务,未列出的服务一律推荐关闭:(按空格键选择/取消)
atd
crond
irqbalance
microcode_ctl
network
ntpd
sendmail
sshd
syslog
按Tab确认,退出。




4. 更换快速源(国内163源) # cd /etc/yum.repos.d/
# mv CentOS-Base.repo CentOS-Base.repo.old
# wget http://mirrors.163.com/.help/CentOS-Base-163.repo
# mv CentOS-Base-163.repo CentOS-Base.repo
# yum makecache //生成缓存
# yum update



5. 下载程序包,脚本和配置文件
以下列出的程序最新稳定版下载列表
# cd /usr/local/src
# wget http://c.sihost.net/lamp_tools.list
# wget -i lamp_tools.list

下载Apache 控制脚本 # wget http://c.sihost.net/jb/httpd
下载Apache 配置文件
# wget http://c.sihost.net/jb/httpd.conf




创建web运行用户 # groupadd www
# useradd -g www www
# mkdir -p /home/www/wwwroot
# chmod +w /home/www/wwwroot
# chown www:www /home/www/wwwroot -R

重新启动 # init 6



三. 编译安装L.A.M.P环境
1. 安装MySQL # cd /usr/local/src
# wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.58-linux-i686-glibc23.tar.gz/from/ftp://ftp.jaist.ac.jp/pub/mysql/
# tar zxvf mysql-5.1.58-linux-i686-glibc23.tar.gz
# mv mysql-5.1.58-linux-i686-glibc23 /usr/local/
# ln -s /usr/local/mysql-5.1.58-linux-i686-glibc23/ /usr/local/mysql
# groupadd mysql
# useradd -g mysql mysql
# chown -R mysql:mysql /usr/local/mysql
# chown -R mysql:mysql /usr/local/mysql-5.1.58-linux-i686-glibc23/
# cd /usr/local/mysql
# ./scripts/mysql_install_db --user=mysql
# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig --level 3 mysqld on
# cp ./support-files/my-huge.cnf /etc/my.cnf
# mv /usr/local/mysql/data /var/lib/mysql
# chown -R mysql:mysql /var/lib/mysql

修改 my.cnf (具体修改操作请搜索相关LINUX vi命令) vi /etc/my.cnf

在 [mysqld] 段 增加 datadir = /var/lib/mysql
skip-innodb
wait-timeout = 10
max_connections = 512
max_connect_errors = 10000000

在 [mysqld] 段 修改 max_allowed_packet = 16M
thread_cache_size = CPU个数x2
将 log-bin 前加#注释掉

保存!

生成数据库 启动服务进程
# service mysqld start
修改数据库密码 # /usr/local/mysql/bin/mysqladmin -u root password "填写你的密码" -p
进入mysql 查看状态( 可以不做) # /usr/local/mysql/bin/mysql -uroot -p你的密码
status
exit


2、编译安装Apache
# cd /usr/local/src/
# tar zxvf httpd-2.2.19.tar.gz
# cd httpd-2.2.19/
# ./configure --prefix=/usr/local/apache --enable-module=rewrite --disable-access --disable-auth --disable-charset-lite --disable-include --disable-log-config --disable-env --disable-setenvif --disable-mime --disable-status --disable-autoindex --disable-asis --disable-cgid --disable-cgi --disable-negotiation --disable-dir --disable-actions --disable-userdir --disable-alias --enable-so --enable-mods-shared=' access auth auth_anon auth_dbm auth_digest dav dav_fs actions alias asis autoindex cache cern_meta cgi charset_lite deflate dir disk_cache env expires file_cache headers include info log_config logio mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias'
# make
# make install
# cd /usr/local/src/
# mv /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.old
# cp -f /usr/local/src/httpd.conf /usr/local/apache/conf/httpd.conf
# cp -f /usr/local/src/httpd /etc/init.d/httpd
# chmod 755 /etc/init.d/httpd
# chkconfig --add httpd
# chkconfig httpd on

3、编辑安装PHP所需的支持库

Libiconv # cd /usr/local/src/
# tar zxvf libiconv-1.14.tar.gz
# cd libiconv-1.14/
# ./configure --prefix=/usr/local
# make
# make install
# cd ..

Libmcrypt
# tar zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8
# ./configure
# make
# make install
# /sbin/ldconfig
# cd libltdl/
# ./configure --enable-ltdl-install
# make
# make install
# cd /usr/local/src/

Mhash
# tar zxvf mhash-0.9.9.9.tar.gz
# cd mhash-0.9.9.9/
# ./configure
# make
# make install
# cd ..
# ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
# ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
# ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
# ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
# ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
# ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
# tar zxvf mcrypt-2.6.8.tar.gz
# cd mcrypt-2.6.8
# /sbin/ldconfig
# ./configure
# make
# make install



4、编译安装PHP
# cd /usr/local/src
# tar zxvf php-5.2.17.tar.gz
# cd php-5.2.17
# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/etc --enable-mbstring --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --enable-magic-quotes --with-mysql=/usr/local/mysql --with-pear --enable-sockets --with-ttf --with-freetype-dir=/usr --enable-gd-native-ttf --with-zlib --enable-sysvsem --enable-sysvshm --with-libxml-dir=/usr --with-apxs2=/usr/local/apache/bin/apxs --with-iconv-dir=/usr/local --with-xmlrpc --enable-xml --enable-shmop --enable-zip --with-mhash --with-mcrypt --enable-discard-path --enable-bcmath --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --with-openssl
# make ZEND_EXTRA_LIBS='-liconv'
# make install
# ./libtool --finish /usr/local/src/php-5.2.17/libs
# cp php.ini-dist /usr/local/etc/php.ini
# echo 'ulimit -SHn 65535' >> /etc/rc.local
# service httpd start

5、安装PHP扩展模块
Memcache
# cd /usr/local/src
# tar zxvf memcache-2.2.6.tgz
# cd memcache-2.2.6/
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config
# make
# make install

修改php.ini文件,路径/usr/local/etc/
# vi /usr/local/etc/php.ini
查找:
extension_dir = "./"
修改成:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"在此句下面一行添加:
extension = "memcache.so"
查找:
output_buffering = Off修改:
output_buffering = On
保存!

Memcached # cd /usr/local/src
# tar zxvf libevent-1.4.13-stable.tar.gz
# cd libevent-1.4.13-stable
# ./configure --prefix=/usr/lib
# make
# make install
# cd ../
# tar zxvf memcached-1.4.6.tar.gz
# cd memcached-1.4.6
# ./configure --prefix=/usr/local --with-libevent=/usr/lib/
# make
# make install
# /usr/local/bin/memcached -d -u www -m 128


安装Zend Optimizer
# cd /usr/local/src
# wget http://files.directadmin.com/services/custombuild/ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
# tar zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
# cd ZendOptimizer-3.3.3-linux-glibc23-i386
# ./install
Please specify the location for installing
Zend Optimizer:
这里填写Zend 的安装路径,按下面写即可
/usr/local/Zend

Confirm the location of your
php.ini file:
这里填写的是php.ini 的路径,按下面写即可
/usr/local/etc

Are you using Apache Web server?
这里选YES

Apache 的控制组件路径
/usr/local/apache/bin/apachectl

是否重启Apache ,YES ,回车



三.查看确认L.A.M.P环境信息、提升 PHP 安全性

保存以下内容为info.php 到/home/www/wwwroot/ ,检查phpinfo 中的各项信息是否正确。
[code]<?
phpinfo()
?>[/code]
确认 PHP 能够正常工作后,编辑/usr/local/etc/php.ini 进行设置提升PHP 安全性。
[code]# vi /usr/local/etc/php.ini[/code]
找到:
[code]disable_functions =[/code]
在等号后添加下面的函数名字
[code]disable_functions = exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source syslog,readlink,symlink,popepassthru,stream_socket_server[/code]




四、服务器安全性设置

设置系统防火墙
[code]# vi /usr/local/sbin/fw.sh[/code]
将以下脚本命令粘贴到 fw.sh 文件中。  
[code]#!/bin/bash
# Stop iptables service first
service iptables stop
# Load FTP Kernel modules
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_nat_ftp
# Inital chains default policy
/sbin/iptables -F -t filter
/sbin/iptables -P INPUT DROP
/sbin/iptables -P OUTPUT ACCEPT
# Enable Native Network Transfer
/sbin/iptables -A INPUT -i lo -j ACCEPT
# Accept Established Connections
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# ICMP Control
/sbin/iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
# WWW Service
/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# FTP Service
/sbin/iptables -A INPUT -p tcp --dport 21 -j ACCEPT
# SSH Service
/sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# chmod 755 /usr/local/sbin/fw.sh
# echo '/usr/local/sbin/fw.sh' >> /etc/rc.local
# /usr/local/sbin/fw.sh[/code]



展开
收起
gaoqi125 2011-08-17 18:12:26 35435 0
5 条回答
写回答
取消 提交回答
  • ReLinux环境搭建教程LinuxApacheMysqlPHP(已测试阿里云CentOS5.4安装成功)
    有个东东叫“XAMPP(有WIN版 的有linux版的)”以前叫LAMP(基于linux的)。自身集成了apache php mysql phpmyadmin 。不过看上面的过程,我比你菜多了。
    今天刚申请个免费的ubuntu,update upgrade完了之后,来了一个install gnome。现在还在那黑着呢。不知道怎么搞了。VNC接上就一个黑屏,按啥都没反应。外网又SSH不上。唉。。。
    2014-09-11 13:25:22
    赞同 展开评论 打赏
  • ReLinux环境搭建教程LinuxApacheMysqlPHP(已测试阿里云CentOS5.4安装成功)
    四年前玩过linux,现在重新拿起,感觉还是很难。
    2012-07-19 19:32:31
    赞同 展开评论 打赏
  • 这个是LAMP。。。Nginx教程论坛有。。
    2011-08-19 22:30:44
    赞同 展开评论 打赏
  • lnmp怎么安排,替换哪一步?
    2011-08-17 20:56:13
    赞同 展开评论 打赏
  • 有相关的视频的,不过今天我还是会选择windows系统的,因为我会在云主机上搭载其它应用,而用linux我觉得很难实现
    2011-08-17 18:39:50
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像