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

Ubuntu Server 11.10安装配置lamp(Apache+MySQL+PHP)

程序员文章站 2022-07-11 16:02:48
准备篇: 1、配置防火墙,开启80端口、3306端口 说明:ubuntu默认安装是没有开启任何防火墙的,为了服务器的安全,建议大家安装启用防火墙设置,这里推荐使用ipt...

准备篇:

1、配置防火墙,开启80端口、3306端口

说明:ubuntu默认安装是没有开启任何防火墙的,为了服务器的安全,建议大家安装启用防火墙设置,这里推荐使用iptables防火墙。
whereis iptables #查看系统是否安装防火墙
iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz #表示已经安装iptables
apt-get install iptables #如果默认没有安装,请运行此命令安装防火墙
iptables -l #查看防火墙配置信息,显示如下:

#####################################################
chain input (policy accept)
target prot opt source destination

chain forward (policy accept)
target prot opt source destination

chain output (policy accept)
target prot opt source destination
#####################################################
nano /etc/iptables.default.rules #添加以下内容
##################################################################################################
*filter
# allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-a input -i lo -j accept
# accepts all established inbound connections
-a input -m state --state established,related -j accept
# allows all outbound traffic
# you could modify this to only allow certain traffic
-a output -j accept
# allows http and mysqlconnections from anywhere (the normal ports for websites)
-a input -p tcp --dport 80 -j accept
-a input -p tcp --dport 3306 -j accept
# allows ssh connections for script kiddies
# the -dport number is the same one you set up in the sshd_config file
-a input -p tcp -m state --state new --dport 22 -j accept
# now you should read up on iptables rules and consider whether ssh access
# for everyone is really desired. most likely you will only allow access from certain ips.
# allow ping
-a input -p icmp -m icmp --icmp-type 8 -j accept
# log iptables denied calls (access via 'dmesg' command)
-a input -m limit --limit 5/min -j log --log-prefix "iptables denied: " --log-level 7
# reject all other inbound - default deny unless explicitly allowed policy:
-a input -j reject
-a forward -j reject
commit
##################################################################################################
ctrl+o #保存
ctrl+x #退出


备注:80是指web服务器端口、3306是指mysql数据库链接端口、22是指ssh远程管理端口
iptables-restore < /etc/iptables.default.rules #使防火墙规则生效
nano /etc/network/if-pre-up.d/iptables #创建文件,添加以下内容,使防火墙开机启动
##########################################################
#!/bin/bash
/sbin/iptables-restore </etc/iptables.default.rules
##########################################################
chmod +x /etc/network/if-pre-up.d/iptables #添加执行权限

安装篇

一、安装apache

apt-get install apache2 #安装apache,根据提示输入y安装
service apache2 start #启动apache
service apache2 restart #重启
apt-get install chkconfig #安装chkconfig
chkconfig apache2 on #开机启动apache

二、安装mysql

apt-get install mysql-server #安装mysql,根据提示输入y安装
安装过程中,会跳出输入root密码的界面
输入2次密码,继续自动安装

Ubuntu Server 11.10安装配置lamp(Apache+MySQL+PHP)

Ubuntu Server 11.10安装配置lamp(Apache+MySQL+PHP)

service mysql start #启动
chkconfig mysql on #开机启动mysql
service mysql restart #重启

三、安装php

1、apt-get install php5 #安装php5,根据提示输入y安装
2、安装php组件,使php支持 mysql
apt-get install php5-mysql php5-gd libjpeg8-dev php5-imap php5-ldap php5-odbc php*-pear php*-xml php5-xmlrpc php5-mcrypt php5-mhash libmcrypt* libmcrypt-dev php-fpdf
/etc/init.d/mysql restart #重启mysql
/etc/init.d/apache2 restart #重启apache2
ln -s /etc/php5/apache2/php.ini /etc/php.ini #把php配置文件链接到系统默认位置

配置篇

一、apache配置
cp /etc/apache2/apache2.conf /etc/apache2/apache2.confbak #备份
nano /etc/apache2/apache2.conf #编辑
servertokens prod #在出现错误页的时候不显示服务器操作系统的名称
serversignature off #在在错误页中不显示apache2的版本
maxkeepaliverequests 1000 #修改为1000(默认为100,增加同时连接数)
servername localhost   #添加apache2默认服务名
ctrl+o #保存
ctrl+x #退出
nano /etc/apache2/sites-enabled/000-default #编辑
options multiviews followsymlinks #不在浏览器上显示树状目录结构
ctrl+o #保存
ctrl+x #退出
nano /etc/apache2/mods-enabled/dir.conf   #编辑,设置默认主页顺序
directoryindex index.html  index.php  index.htm
ctrl+o #保存
ctrl+x #退出
a2enmod rewrite #激活apache2伪静态模块mod rewrite

二、配置php

nano /etc/php5/apache2/php.ini #编辑
date.timezone = prc #在946行 把前面的分号去掉,改为date.timezone = prc
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
#在386行 列出php可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
expose_php = off #在432行 禁止显示php版本的信息
magic_quotes_gpc = on #在745行 打开magic_quotes_gpc来防止sql注入
open_basedir = .:/tmp/ #在380行,设置表示允许访问当前目录(即php脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题,可注销此行,或者直接写上程序目录路径/var/www/www.osyunwei.com/:/tmp/
ctrl+o #保存
ctrl+x #退出

测试篇
cd /var/www #进入默认站点目录
nano index.php #新建测试文件

<?php
phpinfo();
?>

ctrl+o #保存
ctrl+x #退出
chown www-data.www-data -r /var/www #添加目录所有者
chmod 700 -r /var/www #设置目录权限
在客户端浏览器输入服务器ip地址,可以看到相关的配置信息!

备注:

apache2默认站点目录是:/var/www
权限设置:chown www-data.www-data -r /var/www
apache2虚拟主机配置文件:/etc/apache2/sites-enabled/000-default #虚拟主机配置文件
mysql数据库目录是:/var/lib/mysql
权限设置:chown mysql.mysql -r /var/lib/mysql