本文共 5700 字,大约阅读时间需要 19 分钟。
一、LAMP搭建前的准备
LAMP是四套软件的缩写,分别指的是L-Linux,A-Apache,M-Mysql,P-php,利用这四套软件搭建的web的运行环境。 搭建前需要需要下载好软件 #cd /home/softapache :http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.31.tar.gz
mysql:32位 :http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.31-linux-glibc2.5-i686.tar.gz
64位 :http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-x86_64-glibc23.tar.gzphp :wget 'http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror' -O php-5.4.45.tar.bz2
软件镜像下载地址http://mirrors.sohu.com/,搜狐镜像,阿里镜像,网易镜像可以下载各种开源软件 安装扩展库yum install -y epel-release 安装库文件 yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libtiff-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel fontconfig-devel zlib zlib-devel libevent-devel gcc gcc-c++ flex bison bzip2-devel libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel关闭selinux暂时关闭:setenforce 0
永久关闭:vim /etc/selinux/config 修改SELINUX=disabled ##需要重启生效关闭防火墙
iptables -F
service iptables stop
二、Apache安装1 )编译
# 解压软件包
[root@localhost soft]# tar -zxvf httpd-2.2.31.tar.gz# 进入软件目录
[root@localhost soft]# cd httpd-2.2.31# 执行编译参数
[root@localhost httpd-2.2.16]#./configure \--prefix=/usr/local/apache2 \--with-included-apr \--enable-so \--enable-deflate=shared \--enable-expires=shared \--enable-rewrite=shared \--with-pcre# 开始编译安装软件
[root@localhost httpd-2.2.16]# make
[root@localhost httpd-2.2.16]# make install
输入 echo$? 显示0则编译安装无误
2 )修改配置
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
..................上面省略..................... # at a local disk. If you wish to share the same ServerRoot for multiple # httpd daemons, you will need to change at least LockFile and PidFile. # ServerRoot "/usr/local/apache2" # apache 程序目录 #Listen 12.34.56.78:80 Listen 80 # 监听端口 ServerAdmin you@example.com # 管理员邮箱地址 ServerName localhost:80 # 默认ServerName www.example.com:80DocumentRoot "/usr/local/apache2/htdocs/" # 网站文件存储的位置,文件监听目录
Options FollowSymLinks AllowOverride None Order deny,allow Allow from all# 默认为Deny from all,拒绝访问,修改为Allow from all,允许访问,否则访问网站时会403提示
3 )启动apache# 修改配置 ,检测是否OK
[root@localhost ~]# /usr/local/apache2/bin/apachectl -t Syntax OK [root@localhost ~]# /usr/local/apache2/bin/apachectl start[root@localhost ~]# ps aux|grep httpd #检测httpd的启动
root 25675 0.0 0.2 5680 2148 ? Ss 10:20 0:00 /usr/local/apache2/bin/httpd -k start daemon 25880 0.0 0.1 5680 1568 ? S 10:20 0:00 /usr/local/apache2/bin/httpd -k start root 25886 0.0 0.0 4622 769ts/4 S+ 10:23 0:00 grep httpd [ [root@localhost ~]# netstat -nlp |grep httpd # 端口检测tcp 0 0 :::80 :::* LISTEN 25675/httpd
[root@localhost ~]# curl -I localhost # 网站状态访问检测HTTP/1.1 200 OK
Date: Tue, 28 Apr 2016 12:52:42 GMT Server: Apache/2.2.16 (Unix) DAV/2 Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT ETag: "12a8-2c-3e9564c23b600" Accept-Ranges: bytes Content-Length: 44 Content-Type: text/html 在浏览器中输入IP地址,如果显示 It works! 代表成功!三、mysql安装# 解压mysql
[root@localhost soft]# tar -zxvf mysql-5.1.73-linux-x86_64-glibc23.tar.gz # 文件程序移动到指定的安装路径 [root@localhost soft]# mv mysql-5.1.73-linux-x86_64-glibc23 /usr/local/mysql # 创建mysql用户,shell状态为/sbin/nologin [root@localhost mysql]# useradd -s /sbin/nologin mysql # 创建数据库存储目录,更改目录权限 [root@localhost mysql]#cd /usr/local/mysql [root@localhost mysql]# mkdir -p /data/mysql [root@localhost mysql]# chown -R mysql:mysql /data/mysql/# 进入/usr/local/mysql,初始化mysql库,当有2个OK,代表初始化成功# 这里要注意,需要修改/etc/hosts,将主机名字,添加到文件
[root@localhost mysql]# vi /etc/hosts 加入 192.168.1.6 localhost [root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql Installing MySQL system tables... OK Filling help tables... OK# 拷贝启动脚本,更改权限 [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@localhost mysql]# chmod 755 /etc/init.d/mysqld# 修改启动脚本 [root@localhost mysql]# vim /etc/init.d/mysqld basedir=/usr/local/mysql datadir=/home/data/mysql# 拷贝mysql配置文件 [root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf cp: overwrite `/etc/my.cnf'? y# 将mysqld加到服务列表里 [root@localhost mysql]# chkconfig --add mysqld [root@localhost mysql]# chkconfig mysqld on [root@localhost mysql]# service mysqld start Starting MySQL... SUCCESS! 启动成功 用/usr/local/mysql/bin/mysql 进入数据库四、php安装# 解压软件
[root@localhost php-5.3.28]# tar -jxvf php-5.4.45.tar.bz2 [root@localhost php-5.3.28]#cd php-5.4.45# 进入php文件目录,编辑参数 [root@localhost php-5.3.28]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif --disable-ipv6# 执行编译安装 [root@localhost php-5.3.28]# make [root@localhost php-5.3.28]# make install五、php与apache的组合修改apache的配置文件
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf DirectoryIndex index.html index.htm index.php 添加index.htm和index.php AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType application/x-httpd-php .php 加上这一行,增加对php的解析 #说明:要想支持php脚本解析,必须要加上对应的类型LoadModule php5_module modules/libphp5.so 开启php5_module
保存后 检测 /usr/local/apache2/bin/apachectl -t 启动 /usr/local/apache2/bin/apachectl restart 查看是否启动: [root@localhost ~]# netstat -lnp |grep httpdtcp 0 0 :::80 :::* LISTEN 7867/httpd
# 编写php解析测试文件
[root@localhost ~]# vim /usr/local/apache2/htdocs/test.php <?php phpinfo(); ?>在浏览器里输入192.168.1.6/test.php地址,显示php界面,代表解析php成功