yum安装Apache、PHP、Mysql及扩展插件
环境:CentOS 6.3 最小化安装
参考 https://www.cnblogs.com/ylnic/archive/2011/03/27/1996839.html
一、yum命令用法(在root用户下,可直接调用命令,但对划分用户权限的情况下,比如在Amazon EC2需要sudo,用户环境变量没配好无法直接调用)
1)同时安装多个软件a
/usr/bin/yum -y install http php mysql-server
2) 同时安装多个软件,且同名类似用*代替
/usr/bin/yum -y install http*
3)卸载软件如下,多个或多个类似同上,用*代替
/usr/bin/yum -y remove httpd
4)安装某个命令的软件包或只记得一部分名称,可用以下命令查找,再用上面方法安装
/usr/bin/yum search iostat
二、安装yum EPEL(yum安装nginx需要,否则跳过)
http://www.androiddev.net/redhat-centos-install-rpm-yum/
很多非常有用的软件都位于EPEL(Extra Packages for Enterpriese Linux)这个repository中, 缺省情况下,这个repository有可能是没有安装的
/usr/bin/yum -y install epel-release
安装完毕后,可以运行 yum repolist来看看epel是否已经启动
/usr/bin/yum repolist
三、安装mysql及扩展
/usr/bin/yum -y install mysql-server mysql-devel
/usr/bin/yum -y install mysql mysql-connector-odbc libdbi-dbd-mysql
#mysql 只作为客户端,安装server会自动装
#mysql-server mysqld数据库
#mysql-devel mysqld开发包,包含mysql.h等头文件,/usr/include/mysql/,proftp连mysql用到;
/sbin/chkconfig mysqld on
/sbin/service mysqld start
四、安装nginx和php-fpm(安装php在下一节)
/usr/bin/yum -y install nginx php-fpm
cd /etc/nginx/conf.d
/bin/cp -a default.conf default.conf.`date +%Y%m%d.%H%M%S`
/sbin/chkconfig nginx on
/sbin/chkconfig php-fpm on
/etc/init.d/nginx start
/etc/init.d/php-fpm start
#修改nginx配置文件,修改网站root路径,
cd /etc/nginx/conf.d
/bin/vi /etc/nginx/conf.d/default.conf
# 首页默认加上index.php
# index index.php index.html index.htm;
#把下面一段的注释去掉,root改为绝对路径,把/scripts改为相对路径$document_root,
#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_params;
#}
#修改完成后重启服务,如出现php文件下载的情况,则可能是没配置好PHP时用chrome访问过页面,会产生缓存,换浏览器试试;
/etc/init.d/nginx restart
#多站点的配置,在virtual.conf文件加上以下部分参数
[root@appbition conf.d]# cat virtual.test.conf
#
# A virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen 80;
# listen somename:8080;
# server_name somename alias another.alias;
server_name test.appbition.com;
location / {
root /var/www/test;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /var/www/test;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 建立服务器密钥(因为一些安全问题,一般现在的第三方 SSL 证书签发机构都要求起码 2048 位的 RSA 加密的私钥。)
cd /etc/ssl
openssl genrsa -out server.key 2048
# 建立服务器公钥
openssl req -new -key server.key -out server.csr
# 会出以下内容
Country Name (2 letter code) [XX]:CN (输入国名,两字母)
State or Province Name (full name) []:GD (输入省名,可用汉语拼音,下同)
Locality Name (eg, city) [Default City]:ZH (输入城市名)
Organization Name (eg, company) [Default Company Ltd]:appbition (输入公司名,或随意)
Organizational Unit Name (eg, section) []: (回车,部门名称或随意)
Common Name (eg, your name or your server's hostname) []:*.appbition.com (域名,这个是最关键的,需要填写ssl证书对应的域名,泛域名一定要写成*.doomain.com的形式。)
Email Address []:felix.wzp@qq.com (联系人邮件地址)
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: (直接回车)
An optional company name []: (直接回车)
# 查看会有对应 .key 和 .csr文件
ls -l
-rw-r--r-- 1 root root 660 Aug 22 09:18 server.csr
-rw-r--r-- 1 root root 887 Aug 22 09:14 server.key
或者,这样直接生成CSR:
cd /etc/ssl
openssl req -nodes -newkey rsa:2048 -nodes -keyout server.key -out server.csr -subj "/C=SG/ST=SINGAPORE/L=SINGAPORE/O=Appbition/OU=Na/CN=*.appbition.com"
# 建立服务器证书(由于只有浏览器或者系统信赖的 CA 才可以让所有的访问者通畅的访问你的加密网站,而不是出现证书错误的提示。所以可跳过自签证书的步骤,直接开始签署第三方可信任的 SSL 证书。)
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
# 对虚拟机进行配置
cd /etc/nginx/conf.d
vi virtual.os.conf
# 增加两部分四行,重启nginx;
listen 443;
ssl on;
ssl_certificate /etc/ssl/server.crt;
ssl_certificate_key /etc/ssl/server.key;
# 检测配置文件没问题后重新读取 Nginx 即可
/etc/init.d/nginx -t && nginx -s reload
五、安装Apache及扩展(安装nginx跳过此节)
安装Apache,php,Mysql,以及php连接mysql库组件(安装顺序Mysql--Apache--PHP),组件按生产用途需要安装,请看注释;安装完之后,再新增PHP扩展,则重启httpd,它就会加载使用,无需重新编译:
/usr/bin/yum -y install httpd
/usr/bin/yum -y install httpd-devel httpd-manual mod_ssl mod_perl mod_auth_mysql
#httpd 软件包包含httpd守护进程和相关工具、配置文件、模块等
#httpd-devel 包含httpd服务器包含文件、头文件和APXS工具,加载额外模块就要用到如php;
#httpd-manual Apach的手册指南说明书
#mod_ssl 包括mod_ssl,提供加密能力, https用到;
/sbin/chkconfig httpd on
/sbin/service httpd start
#通过yum方式安装的apxs(如:yum -y install httpd-devel),则--with-apxs2参数不要加=后面的路径。apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载到Apache服务器中。
#因此,要使用这个扩展机制,你的平台必须支持DSO特性,而且Apache httpd必须内建了mod_so模块。apxs工具能自动探测是否具备这样的条件,你也可以自己用这个命令手动探测:
/usr/sbin/httpd -l
六、安装php及扩展
/usr/bin/yum -y install php php-mysql php-mbstring php-mcrypt php-gd
/usr/bin/yum -y install php-xml php-ldap php-pear php-xmlrpc php-pecl-memcache php-common php-mhash php-imap
#php 安装php软件包
#php-devel 包含phpize
#php-mysql php连接mysql组件,需要
#php-pecl-memcache 安装memcache
#php-mbstring,没有 mbstring 扩展的 phpMyAdmin 不能正确分割字符串,可能产生意想不到的结果。
#php-mcrypt,缺少mcrypt扩展。请检查 PHP 配置。--phpMyAdmin;RHEL不打算添加PHP的mcrypt 的支持
# php-imap, InboundEmail and Campaigns (Email) require the IMAP libraries.
http://blog.163.com/lhy0508@126/blog/static/171649582011112091750212/
#查看版本
/usr/bin/php -v
#查看PHP支持哪些扩展:
/usr/bin/php -m
七、设置mysql数据库root帐号密码
1)第一个方法,用mysql_secure_installation来配置,会一步一步提示
#Enter current password for root (enter for none): ( 回车) (初始密码为空)
#Set root password? [Y/n] (Y) (设置密码,输两次)
#Remove anonymous users? [Y/n](是否移出数据库的默认帐户,如果移出,那么在终端中直接输入mysql是会提示连接错误的,Y)
#Disallow root login remotely? [Y/n](是否禁止root的远程登录)Y
#Remove test database and access to it? [Y/n] Y
#Reload privilege tables now? [Y/n] Y
2)第二个方法,用命令配置,意思同上
/usr/bin/mysqladmin -u root password 'new-password'
# 'new-password'单引号内是你想要设置的密码,新安装的mysql的root根用户密码默认为空,设置密码后可以让mysql数据库更安全;请设置一个符合安全要求的新密码,并记好
# PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands:
# /usr/bin/mysqladmin -u root password 'new-password'
# /usr/bin/mysqladmin -u root -h localhost password 'new-password'
mysql -u root -p
#此时会要求你输入刚刚设置的密码,输入后回车即可
# 进入mysql后
mysql> use mysql;
#进入mysql库
mysql> DROP DATABASE test;
#可考虑,删除test数据库
mysql> DELETE FROM mysql.user WHERE user = '';
#可考虑,删除匿名帐户
mysql> FLUSH PRIVILEGES;
#重载权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.13' IDENTIFIED BY 'new-password' WITH GRANT OPTION;
#允许root用户从192.168.1.13用密码new-password连接到mysql服务器,如果不限制ip则用%代替ip;这句待确认。
mysql> exit
#退出数据库
八、新开数据库和用户
#用root登录数据库
/usr/bin/mysql -u root -p
#为website建webdb库
CREATE DATABASE webdb;
#新增数据库用户webuser,密码为12345678,不推荐不设密码,那样只可在localhost登录但不安全;对数据库webdb进行查询、修改的操作如下;
#格式grant select,insert,update,delete on 数据库.* to 用户名@登录主机 identified by “密码”,登录主为'%'表示远程访问等无限制;
#查询、插入、修改、删除为select,insert,update,delete,所有权限是all privileges,
#修改用户密码格式SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
#会出现“#1045 无法登录 MySQL 服务器”,他的主机号是% ,登录就出现上面的问题,如果改为localhost就可以登陆了;
#(有些版本)'%'不包括localhost,分别对'%'和'localhost'授权解决;
GRANT all privileges on webdb.* to 'webuser'@'localhost' identified by '12345678';
GRANT all privileges on webdb.* to 'webuser'@'%' identified by '12345678';
#刷新系统权限表
flush privileges;
http://www.zzxj.net/blog/fxs_2008/archive/2010/06/29/179.html
九、/etc/my.cnf的优化
#CentOS系统的确很好用,但是还是很多地方需要我们进行正确的设置,进行优化的。在Apache, PHP, MySQL的体系架构中,MySQL对于性能的影响最大,也是关键的核心部分。
/etc/my.cnf
#这是Mysql的配置文件,包括您想要进行mysql数据库的最佳化,或者是正对mysql进行一些额外的参数指定,都可以在这个文件里实现
/var/lib/mysql
#这个目录是MySQL数据库放置的位置,当启动任何MySQL的服务器时,请务必记得在备份时,用命令备份或可将此目录完整的备份下来。
#MySQL在Linux下默认是:数据库名与表名\表的别名\变量名是严格区分大小写(windows不分);列名、SQL语句命令不区分大小写,大写是方便阅读;
#如果在/etc找不到my.cnf的话,需要从/usr/share/mysql/下找到my-*.cnf文件,拷贝其中一个到/etc/并改名为my.cnf;区别如下
#my-small.cnf是为了小型数据库而设计的(内存小于64MB)。
#my-medium.cnf是为中等规模的数据库而设计的(内存32--64MB,系统大于128MB)。
#my-large.cnf是为专用于一个SQL数据库的计算机而设计的(系统512MB)。
#my-huge.cnf是为企业中的数据库而设计的(内存1--2GB)。
#my-innodb-heavy-4G.cnf (DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries)
#MySQL 默认的最大连接数为 100,MySQL服务器允许的最大连接数16384,可以在 mysql 客户端使用以下命令查看
mysql -u root -p
mysql> show variables like 'max_connections';
#查看当前所有的连接
mysql> show full processlist;
#要对 mysql 的最大连接数进行修改,只需要在 my.cnf 配置文件里面修改 max_connections 的值,然后重启 mysql 就行。如果 my.cnf 文件中没有找到 max_connections 条目,可自行添加以下条目:
max_connections = 2000
#查看Mysql状态:
mysql> show status;
含义如下:
aborted_clients 客户端非法中断连接次数
aborted_connects 连接mysql失败次数
com_xxx xxx命令执行次数,有很多条
connections 连接mysql的数量
Created_tmp_disk_tables 在磁盘上创建的临时表
Created_tmp_tables 在内存里创建的临时表
Created_tmp_files 临时文件数
Key_read_requests The number of requests to read a key block from the cache
Key_reads The number of physical reads of a key block from disk
Max_used_connections 同时使用的连接数
Open_tables 开放的表
Open_files 开放的文件
Opened_tables 打开的表
Questions 提交到server的查询数
Sort_merge_passes 如果这个值很大,应该增加my.cnf中的sort_buffer值
Uptime 服务器已经工作的秒数
提升性能的建议:
1).如果opened_tables太大,应该把my.cnf中的table_cache变大
2).如果Key_reads太大,则应该把my.cnf中key_buffer_size变大.可以用Key_reads/Key_read_requests计算出cache失败率
3).如果Handler_read_rnd太大,则你写的SQL语句里很多查询都是要扫描整个表,而没有发挥索引的键的作用
4).如果Threads_created太大,就要增加my.cnf中thread_cache_size的值.可以用Threads_created/Connections计算cache命中率
5).如果Created_tmp_disk_tables太大,就要增加my.cnf中tmp_table_size的值,用基于内存的临时表代替基于磁盘的
myisam 表 转换为 innodb 表
1)、备份数据库后,shutdown停止数据库或者service mysql stop。
2)、InnoDB 表不支持全文搜索,将备份出来的数据库sql,删掉有关 Fulltext 的索引。
3)、cd /usr/local/mysql/support-files/ 找寻适合主机内存的设定文件,必将设定文件拷贝到 /etc/my.cnf。
4)、vi /etc/my.cnf ,将以下几项批注取消掉。以 my-large.cnf 为例。
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 256M
innodb_additional_mem_pool_size = 20M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
加上default-storage-engine=innodb
加上这段之后,以后新增的数据表型态都即是 InnoDB,不然每次新增一次数据表,SQL 后面得加上 ENGINE=InnoDB;
5)、备份出来的sql要将ENGINE=MyISAM改成ENGINE=InnoDB。
6)、最后导入数据,或用以下命令,把所有表转换:
mysql>alter table test engine innodb;
有关“InnoDB Error ib_logfile0 of different size”错误
在你修改my.cnf的innodb_log_file_size参数前,请先停止mysql服务程序的运行(mysql的停止没有出现任何错误),并把/var/lib/mysql目录下的ib_logfile0、ib_logfile1、ib_logfile2等之类的文件移除到一个安全的地方(旧日志文件的保留是为了防止意外的出现),以便Mysql重启后可以将新的ib_logfile0之类日志文件生成到/var/lib/mysql目录下。如果没有什么意外,旧的日志文件可以删除。
7)、ubuntu下mysql不能远程连接数据库的问题,打开 /etc/mysql/my.cnf 文件,找到 bind-address = 127.0.0.1 修改为 bind-address = 0.0.0.0 或者 注释掉
重启mysql : sudo /etc/init.d/mysql restart
十、上述安装方式,配置出来的默认站点目录为/var/www/html/,新建一个php脚本(访问前配置好Firewall):
cd /var/www/html/
/bin/touch index.php
/bin/echo "
phpinfo();
?>
"> /var/www/html/index.php
11、配置HTTPS(默认yum安装完mod_ssl可省下面步骤)
# 要求安装mod_ssl,但上面安装Apache时已经附带安装mod_ssl,所以应该连Openssl也安装上了,检查的命令是:
#rpm -qa |grep openssl
# openssl-1.0.1e-4.53.amzn1.x86_64
# openssl-devel-1.0.1e-4.53.amzn1.x86_64
# 建立服务器密钥(因为一些安全问题,一般现在的第三方 SSL 证书签发机构都要求起码 2048 位的 RSA 加密的私钥。)
cd /etc/httpd/conf
openssl genrsa -out server.key 2048
# 建立服务器公钥
openssl req -new -key server.key -out server.csr
# 会出以下内容
Country Name (2 letter code) [XX]:CN (输入国名,两字母)
State or Province Name (full name) []:GD (输入省名,可用汉语拼音,下同)
Locality Name (eg, city) [Default City]:ZH (输入城市名)
Organization Name (eg, company) [Default Company Ltd]:appbition (输入公司名,或随意)
Organizational Unit Name (eg, section) []: (回车,部门名称或随意)
Common Name (eg, your name or your server's hostname) []:*.appbition.com (域名,这个是最关键的,需要填写ssl证书对应的域名,泛域名一定要写成*.doomain.com的形式。)
Email Address []:felix.wzp@qq.com (联系人邮件地址)
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: (直接回车)
An optional company name []: (直接回车)
# 查看会有对应 .key 和 .csr文件
ls -l
-rw-r--r-- 1 root root 660 Aug 22 09:18 server.csr
-rw-r--r-- 1 root root 887 Aug 22 09:14 server.key
-rw-r--r-- 1 root root 34474 Jul 12 00:49 httpd.conf
-rw-r--r-- 1 root root 13139 Jul 12 01:00 magic
或者,这样直接生成CSR:
cd /etc/ssl
openssl req -nodes -newkey rsa:2048 -nodes -keyout server.key -out server.csr -subj "/C=SG/ST=SINGAPORE/L=SINGAPORE/O=Appbition/OU=Na/CN=*.appbition.com"
# 建立服务器证书(由于只有浏览器或者系统信赖的 CA 才可以让所有的访问者通畅的访问你的加密网站,而不是出现证书错误的提示。所以可跳过自签证书的步骤,直接开始签署第三方可信任的 SSL 证书。)
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
#ls -l查看
-rw-r--r-- 1 root root 908 Aug 22 09:28 54.254.142.138.crt
-rw-r--r-- 1 root root 680 Aug 22 09:28 54.254.142.138.csr
-rw-r--r-- 1 root root 887 Aug 22 09:26 54.254.142.138.key
# 修改SSL的设置文件, 默认安装已经带有免费的证书,
vim /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
三、httpd.conf的基本设置
1、目录和文件说明
/etc/httpd/conf/httpd.conf
最主要的配置文件,不过很多其他的distribution都将这个文件拆成数个小文件,分别管理不同的参数。但是最主要配置文件还是以这个文件名为主。
/etc/httpd/conf.d/*.conf
这个是CentOS的特色之一,如果你不想修改原始配置文件httpd.conf的话,那么可以将你自己的额外参数独立出来,而启动apache时,这个文件就会被读入到主要配置文件。虚拟主机的配置文件,一般包含在主文件的设置为Include conf.d/*.conf,也就是/etc/httpd/conf.d里面。
/usr/lib/httpd/modules
apache支持很多的模块,所以您想要使用的模块默认都放置在此目录
/var/www/html
这里是CentOS默认的“首页”所在目录。
/var/www/error
如果因为主机设置错误,或者是浏览器端要求的数据错误,在浏览器上出现的错误信息就已这个目录的默认信息为主。
/var/www/icons
提供apache的一些小图标
/var/www/cgi-bin
默认给一些可执行的CGI程序放置的目录
/var/log/httpd
默认apache的日志文件都放在这里,对于流量大的网站来说,这个目录要很小心,因为这个文件很容易变的很大,您需要足够的空间哦
/usr/sbin/apachectl
这是Apache的主要执行文件,这个执行文件其实是shell script,它可以主动检测系统上的一些设置值,好让您启动Apache时更简单
/usr/sbin/httpd
这是主要的apache的二进制文件
/usr/bin/htpasswd
当您想登陆某些网页时,需要输入账号与密码。那么Apache本身就提供一个最基本的密码保护方式。该密码的产生就是通过这个命令实现的
2、httpd.conf的基本设置
1)、主文件httpd.conf的设置,先备份:
/bin/cp -a /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.`date +%Y%m%d.%H%M%S`
首先,你需要在/etc/hosts内需要有个一个完整的主机名,否则在重启apache服务时,会提示找不到完整的主机名。
httpd.conf的基本设置是这样的:
<设置项目>
次设置项目内的相关参数
......
例如,你想要针对首页/var/www/html提供额外的功能,那么可以进行如下设置:
<directory "="" var="" www="" html"="">
Options Indexes
......
针对主机环境的设置项目:
vi /etc/httpd/conf/httpd.conf
ServerTokens OS
#这个项目在告诉客户端WWW服务器的版本和操作系统,不需要改编它
#如果你不想告诉太多的主机信息,将这个项目的OS改成Minor
ServerRoot "/etc/httpd"
#这个是设置文件的最顶层目录,通常使用绝对路径,下面某些数据设置使用相对路径时
#就是与这个目录设置值有关的下层目录,不需要更改它;
TimeOut
设定服务器接收至完成的最长等待时间,该参数指定Apache在接收请求或发送所请求内容之前的最长等待时间(秒),若超过该时间Apache则放弃处理该请求,并释放连接。该参数默认值为120,推荐设置为60,对于访问量较大的网站可以设置为30或15。
KeepAlive
设定服务器是否开启连续请求功能,真实服务器一般都要开启
Port
设定http服务的默认端口。
User/Group
设定服务器程序的执行者与属组,这个一般是apache
DocumentRoot "/var/www/html"
设定apache的根目录
<directory "="" var="" www="" html"="">
定义apache /var/www/html这个区域
如果要把apahce的默认路径改掉,上述两个要一起改;
无默认首页时,打开后取消浏览目录功能:
将Options Indexes FollowSymLinks
改为Options None
2)、网站目录权限问题
重启httpd服务后报错找不到目录,主要是由于selinux导致,查看SELinux状态:
getenforce
或
/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态
关闭SELinux的方法如下:
vim /etc/selinux/config
然后将文件中的selinux=""改为disabled,并重启。如果不想重启系统,使用命令 setenforce 0,此命令可以暂时关闭SELinux,重启后失效。
setenforce 0
说明:setenforce 1将SELinux设置成为enforcing模式;setenforce 0将SELinux设置成为permissive模式。
或者更改新Apache新目录的selinux属性,让它匹配httpd这个服务器的要求,怎么改?我们可以复制/var/www/html这个目录的selinux属性
chcon -R --reference /var/www/html /root/website
或装selinux属性递归下去:
restorecon -Rv /srv/www
另一命令
semanage fcontext -a -t httpd_sys_content_t '/srv/www(/.*)?'
然后重启服务;还是出现访问拒绝的话,就查看权限了
#chmod -R 755 /root
3)、启用SELinux后,PHP连接MySQL异常
Could not connect: Can't connect to MySQL server on 'MYSQL.SERVER' (13)
的现象,一个最简单的测试PHP代码,在测试环境很正常,但是在正式环境下,无论用何种方式(tcp/ip、unix socket)都无法连接mysql。
我协助查看了下,确实如此,无论是指定IP、端口的tcp/ip方式连接,或者是用unix socket方式连接,报错信息都类似:
无论如何修改MySQL的授权,或者调整php.ini中关于MySQL的设置,或者修改MySQL的监听网口,都无法解决。
而如果用命令行人工连接MySQL,则一切正常。
问题看起来像是php(with apache)不被允许连接MySQL,但是防火墙也没有限制。想来想去,唯有SELinux的因素会导致这个问题。
getsebool -a|grep -i httpd
getsebool -a|grep -i httpd_can_network_connect
可以看到这里设置了httpd进程的许可模式,再仔细看一下,有一个选项:
httpd_can_network_connect --> off
现在明白了,原来是SELinux限制了httpd对外访问的权限。将其开启即可:
setsebool -P httpd_can_network_connect=1
如果对SELinux不熟悉,也可以直接修改系统配置文件 /etc/sysconfig/selinux,全局关闭:
SELINUX=enforcing
#把设置改为disabled
SELINUX=disabled
然后重启,或临时生效:
setenforce 0
3、虚拟主机部分
基于名称的虚拟主机
需要两个域名解析到你的服务器,对应关系是
/var/www/server server.example.com
/var/www/client client.example.com
当访问这两个域名时,可以分别显示出不同文件里面主页的内容
#echo "server page" >> /var/www/server/index.html
#echo "client page" >> /var/www/client/index.html
然后我们编辑一个配置文件
#vi /etc/httpd/conf.d/virtual.conf //记住conf.d里面的内容也是apache的配置文件
添加如下内容:
NameVirtualHost 192.168.76.133:80
ServerName service.example.com
DocumentRoot /var/www/server
ServerName client.example.com
DocumentRoot /var/www/client
重启一下服务:
service httpd restart
这样基于名称的虚拟主机就配置好了
例子:
NameVirtualHost *
ServerAdmin webmaster@example.com ——管理员邮箱(可以随便写一个)
DocumentRoot "/home/phpok-com" ——网站目录
ServerName example.com —— 要绑定的域名
ServerAlias www.example.com ——要绑定的别名,如果有多个别名就用英文逗号隔开
CustomLog?logs/example.com_custom_log——用户日志格式(这一行也可以为空)
ErrorLog logs/example.com_error_log ——错误日志(也可以为空)
虚拟主机的一般形式诸如(conf.d/httpd-vhosts.conf):
NameVirtualHost *:80
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
4、权限的问题,要好好考虑一下;(要执行请求的操作,WordPress 需要访问您网页服务器的权限)
http://hi.baidu.com/dongs365/item/67c0cc994260bdda7a7f010b
http://hi.baidu.com/lixiang1988/item/bb316b779e7aee5e0c0a07c9
http://www.icebin.cc/?p=56
四、安装phpMyAdmin
1、进入phpMyAdmin官方(http://www.phpmyadmin.net/home_page/downloads.php)下载,phpMyAdmin v3.0开始最少需要 PHP 5.2 and MySQL 5 及以上:
/bin/mkdir /home/src/
cd /home/src/
/usr/bin/wget http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/3.5.8/phpMyAdmin-3.5.8-all-languages.tar.gz .
/usr/bin/wget http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/4.1.11/phpMyAdmin-4.1.11-all-languages.tar.gz .
cd /var/www/html
/bin/tar zxvf /home/src/phpMyAdmin-3.5.8-all-languages.tar.gz
/bin/mv phpMyAdmin-3.5.8-all-languages phpMyAdmin-all
cd /var/www/html
/bin/tar zxvf /home/src/phpMyAdmin-4.1.11-all-languages.tar.gz
/bin/mv phpMyAdmin-4.1.11-all-languages phpMyAdmin-all
#在v3新版本中,默认不用改动就能正常使用;
#直接访问http://192.168.1.100/phpMyAdmin-all,输mysql用户和密码;
#phpmyadmin3中默认首先加载libraries/config.default.php配置文件的内容,如果有/config.inc.php,就会在config.inc.php中找到相同的变量并覆盖;
#建议修改文件是,简单就是将config.sample.inc.php复制一份更名为config.inc.php,然后打开config.inc.php文件,进行修改,
#想修改更多参数,可将libraries/config.default.php配置文件复制为/config.inc.php,上一级目录下,再进行修改;
$cfg['Servers'][$i]['host'] = 'localhost'; //mysql服务器IP地址,本机为localhost,或域名
$cfg['Servers'][$i]['port'] = ''; //mysql连接端口,默认3306时可以留空
$cfg['Servers'][$i]['extension'] = 'mysqli'; //mysql的连接扩展应该为mysqli
$cfg['Servers'][$i]['user'] = 'root'; //mysql数据库用户名为root,可留空
$cfg['Servers'][$i]['password'] = ''; //在此处填写mysql密码,限测试用,不安全
$cfg['LoginCookieValidity'] = 14400; //phpMyAdmin的cookie会话时长,默认为1440(即24分钟),修改为14400,可不改
#旧版本可能要修改以下代码,去掉每行前面的//注释:
// $cfg['Servers'][$i]['controluser'] = ‘pma’;
// $cfg['Servers'][$i]['controlpass'] = ‘pmapass’;
// $cfg['Servers'][$i]['pmadb'] = ‘phpmyadmin’;
// $cfg['Servers'][$i]['bookmarktable'] = ‘pma_bookmark’;
// $cfg['Servers'][$i]['relation'] = ‘pma_relation’;
// $cfg['Servers'][$i]['table_info'] = ‘pma_table_info’;
// $cfg['Servers'][$i]['table_coords'] = ‘pma_table_coords’;
// $cfg['Servers'][$i]['pdf_pages'] = ‘pma_pdf_pages’;
// $cfg['Servers'][$i]['column_info'] = ‘pma_column_info’;
// $cfg['Servers'][$i]['history'] = ‘pma_history’;
// $cfg['Servers'][$i]['designer_coords'] = ‘pma_designer_coords’;
#配置安全方面,推荐用cookie;http利用了php的http认证机制,这种机制只有php以Apache方式模块运行时才有效;config访问不需要用户名和密码;cookie需要用户名和密码的验证;signon类似于跳转url;
$cfg['Servers'][$i]['auth_type'] = 'cookie';
#用于cookie加密的短语,如'2134asf3'可留空
$cfg['blowfish_secret'] = '';
没有找到 PHP 扩展 mbstring,而您现在好像在使用多字节字符集。没有 mbstring 扩展的 phpMyAdmin 不能正确分割字符串,可能产生意想不到的结果。
配置文件现在需要一个短语密码。
缺少 mcrypt 扩展。请检查 PHP 配置。
phpMyAdmin的高级功能:
$cfg['Servers'][$i]['pmadb'] ... 错误 [ 文档 ]
$cfg['Servers'][$i]['relation'] ... 错误 [ 文档 ]
基本功能: 已禁用
$cfg['Servers'][$i]['table_info'] ... 错误 [ 文档 ]
显示功能: 已禁用
$cfg['Servers'][$i]['table_coords'] ... 错误 [ 文档 ]
$cfg['Servers'][$i]['pdf_pages'] ... 错误 [ 文档 ]
创建 PDF: 已禁用
$cfg['Servers'][$i]['column_info'] ... 错误 [ 文档 ]
显示字段注释: 已禁用
浏览器转换: 已禁用
$cfg['Servers'][$i]['bookmarktable'] ... 错误 [ 文档 ]
SQL 查询书签: 已禁用
$cfg['Servers'][$i]['history'] ... 错误 [ 文档 ]
SQL 历史: 已禁用
$cfg['Servers'][$i]['designer_coords'] ... 错误 [ 文档 ]
设计器: 已禁用
$cfg['Servers'][$i]['recent'] ... 错误 [ 文档 ]
持久最近使用的表: 已禁用
$cfg['Servers'][$i]['table_uiprefs'] ... 错误 [ 文档 ]
持久表界面设置: 已禁用
$cfg['Servers'][$i]['tracking'] ... 错误 [ 文档 ]
追踪: 已禁用
$cfg['Servers'][$i]['userconfig'] ... 错误 [ 文档 ]
用户偏好: 已禁用
快速设置高级功能:
通过 examples/create_tables.sql 创建必需的数据表。
创建一个用户并授予其访问上一步操作中创建的数据表的权限。
在配置文件 (config.inc.php) 中启用高级功能,参见 config.sample.inc.php 中的范例。
请重新登录 phpMyAdmin 以加载新配置并使其生效。
六、配置防火墙
1、添加允许访问HTTP、FTP端口或直接修改文件/etc/sysconfig/iptables
#备份
/bin/cp /etc/sysconfig/iptables /etc/sysconfig/iptables.`date +%Y%m%d.%H%M%S`
/bin/cp /etc/sysconfig/iptables-config /etc/sysconfig/iptables-config.`date +%Y%m%d.%H%M%S`
#修改/etc/sysconfig/iptables-config,将前几行的
#IPTABLES_MODULES=""修改为IPTABLES_MODULES="ip_conntrack_ftp",支持Ftp Passive Mode;
#在INPUT后加列号,则插入成为对应号,其余往后移动,不注明默认为插入第一行,可用命令查看
/sbin/iptables -L -n --line-numbers
#从第五行开始插入,一般就是在ssh 22的后一句
/sbin/iptables -I INPUT 5 -m state --state NEW -p tcp --dport 20 -j ACCEPT
/sbin/iptables -I INPUT 6 -m state --state NEW -p tcp --dport 21 -j ACCEPT
/sbin/iptables -I INPUT 7 -m state --state NEW -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT 8 -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
#保存并重启iptables:
service iptables save
service iptables restart
到此为止centos下的php环境基本就配置完成了。
七、用编译的方法安装Apache、PHP、Mysql及扩展插件
1、安装PHP,从http://cn.php.net/downloads.php下载稳定版:
phpMyAdmin,3.0以上需php 5.2,而centos 5.5目前提供php版本为5.1.6,php需要升级到5.2以上(centos 6.3是PHP 5.3.3);
(Since version 3.0.0, phpMyAdmin joined the GoPHP5 initiative and dropped compatibility code for older PHP and MySQL versions; version 3 and later requires at least PHP 5.2 and MySQL 5.)
需用到(--with-mysqli --with-mcrypt -with-mbstring --enable-mbstring=all)
如果用yum安装了PHP,用以下命令删除:
yum remove php
通过yum方式安装的apxs(如:yum -y install httpd-devel),则--with-apxs2参数不要加=后面的路径。apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载到Apache服务器中。因此,要使用这个扩展机制,你的平台必须支持DSO特性,而且Apache httpd必须内建了mod_so模块。apxs工具能自动探测是否具备这样的条件,你也可以自己用这个命令手动探测:
httpd -l
编译用到的:
yum -y install gcc
yum -y install libxml2-devel
yum -y install libpng-devel
yum -y install libmcrypt-devel
yum -y install freetype-devel
wget http://cn.php.net/distributions/php-5.3.6.tar.gz
tar -zxvf php-5.3.6.tar.gz
cd php-5.3.6
./configure --prefix=/usr/local/php --with-apxs2 --with-mysql --with-gd --with-zlib --with-freetype-dir --with-gettext --disable-cgi --with-config-file-path=/usr/local/php/etc --with-mcrypt -with-mbstring --enable-mbstring=all
make
make install
cp php.ini-recommended /usr/local/php/etc/php.ini
出现以下错误解决办法:
libtool: link: `ext/date/php_date.lo\' is not a valid libtool object
make: *** [sapi/cgi/php-cgi] Error 1
解决办法:--enable-force-cgi-redirect --disable-cgi
checking for mysql_close in -lmysqlclient… no
checking for mysql_error in -lmysqlclient… no
configure: error: mysql configure failed. Please check config.log for more information.
查看config.log是:
/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
解决办法: yum install libtool-ltdl-devel
启动apachce出现错误:libphp5.so: cannot restore segment prot after relock: Permission Denied
A: 临时禁用SELinux.执行命令setenforce 0. 永久生效可以执行setup命令,更改防火墙设置。
或 To fix it as indicated type
chcon -t texrel_shlib_t /usr/lib/httpd/modules/*.so
问题:
/usr/bin/ld: skipping incompatible /usr/lib/mysql/libmysqlclient.so when searching for -lmysqlclient
/usr/bin/ld: skipping incompatible /usr/lib/mysql/libmysqlclient.a when searching for -lmysqlclient
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld returned 1 exit status
make: *** [libphp5.la] Error 1
要安装mysql-devel,另外如果是64位的mysql,要ln -s /usr/lib64/mysql /usr/lib/mysql;
或 替换32位的为64位
ln -s /usr/lib64/mysql/libmysqlclient.so /usr/lib/mysql/
修改Apache的配置文件httpd.conf,以支持php
查看PHP支持哪些扩展:
#/usr/local/php/bin/php -m
可以執行 /usr/local/php/bin/php -v 來看看 extensions 載入是否有錯
八、系统服务配置和自动启动:
停掉无用的服务:
# chkconfig --list |grep on
# chkconfig avahi-daemon off (Avahi 是 zeroconf 协议的实现)
# chkconfig avahi-dnsconfd off (Avahi 是 zeroconf 协议的实现)
# chkconfig bluetooth off(蓝牙支持)
# chkconfig cups off(通用UNIX打印守护进程)
# chkconfig firstboot off(用户配置向导)
# chkconfig hidd off(蓝牙H.I.D.服务器)
# chkconfig ip6tables off(用于 IPv6 的软件防火墙)
# chkconfig isdn off(使用 ISDN 猫来上网)
# chkconfig netfs off(用于在系统启动时自动挂载网络中的共享文件空间)
# chkconfig nfslock off(标准文件共享方式)
# chkconfig pcscd off(提供智能卡和智能卡读卡器支持)
# chkconfig portmap off(用来支持RPC连接,RPC被用于NFS以及NIS 等服务)
# chkconfig rpcgssd off(用于 NFS v4)
# chkconfig rpcidmapd off(用于 NFS v4)
# chkconfig sendmail off(邮件服务器)
chkconfig avahi-daemon off
chkconfig avahi-dnsconfd off
chkconfig bluetooth off
chkconfig cups off
chkconfig firstboot off
chkconfig hidd off
chkconfig ip6tables off
chkconfig isdn off
chkconfig netfs off
chkconfig nfslock off
chkconfig pcscd off
chkconfig portmap off
chkconfig rpcgssd off
chkconfig rpcidmapd off
chkconfig sendmail off
九、PHP安装的扩展参考
./configure --prefix=/usr/local/php --with-curl --with-gettext --with-bz2 --with-mysql -enable-shmop --enable-calendar --with-openssl --with-pspell --enable-ftp --with-openssl --with-zlib --enable-exif --with-gmp --enable-sysvmsg --enable-sockets --enable-wddx --with-xsl --with-mcrypt --with-mysqli --with-mime_magic --with-pdo-mysql --with-gd --with-apxs2=/usr/local/apache/bin/apxs --without-sqlite --enable-so
# 安装apache, mysql-server
yum -y install httpd mod_ssl
yum -y install mysql-server
#和PHP相关的依赖
yum -y install mysql-devel
yum -y install perl httpd-devel apr-devel
yum -y install bzip2-devel
yum -y install curl-devel
yum -y install gmp-devel
yum -y install pspell-devel
yum -y install libmcrypt-devel
yum -y install libxslt-devel
yum -y install libjpeg-devel
yum -y install libpng-devel
yum -y install libxml2-devel
yum -y install freetype-devel
# 一些包,和部分工具软件
yum -y install openssl-devel
yum -y install libgssapi-devel
yum -y install krb5-devel
yum -y install make
yum -y install libtool-ltdl-devel
yum -y install sendmail mailx
yum -y install subversion-devel
十、在PHP方面,应该了解以下几个文件:
/usr/lib/httpd/modules/libphp5.so
PHP提供给apache使用的模块,这个关系我们能否在apache网页上面设计php程序语言的最重要文件;
你要不要手动将该模块写入Httpd.conf中呢?不需要,因为系统编译时已经主动将php设置参数写入到这个文件中了(参数--with-apxs2),而这个文件会在apache重新启动时被读入。
/usr/local/php/etc/php.ini
这是PHP的主要配置文件,包括PHP能不能允许用户上传文件,能不能允许某些低安全性的标志等,都在这个配置文件中设置。php.ini-development和php.ini-production,第一个是开发使用的配置文件,第二个是标准的生产环境的配置
/etc/php.d/mysql.ini /usr/lib/php4/mysql.so
PHP能否可以支持MySQL接口就看这两个文件了。这两个文件是由php-mysql软件提供的
/usr/local/php/bin/phpize
/usr/include/php
如果您以后想要安装类似PHP加速器可以让浏览速度加快的话,那么这个文件与目录就需要存在,否则加速器软件没法用。
PHP版本和相关基本安全设置
VC6与VC9
对于VC6还是VC9版本的选择,PHP官方网站有详细的描述,原文如下:
Which version do I choose?
If you are using PHP with Apache 1 or Apache2 from apache.org you need to use the VC6 versions of PHP
If you are using PHP with IIS you should use the VC9 versions of PHP
VC6 Versions are compiled with the legacy Visual Studio 6 compiler
VC9 Versions are compiled with the Visual Studio 2008 compiler and have improvements in performance and stability. The VC9 versions require you to have the Microsoft 2008 C++ Runtime (x86) or the Microsoft 2008 C++ Runtime (x64) installed
Do NOT use VC9 version with apache.org binaries
我该选择哪个版本?
如果你在apache1或者apache2下使用PHP,你应该选择VC6的版本
如果你在IIS下使用PHP应该选择VC9的版本
VC6的版本使用visual studio6编译
VC9使用Visual Studio 2008编译,并且改进了性能和稳定性。VC9版本的PHP需要你安装Microsoft 2008 C++ Runtime
不要在apache下使用VC9的版本
TS和NTS
TS指Thread Safety,即线程安全,一般在IIS以ISAPI方式加载的时候选择这个版本。
NTS即None-Thread Safe,一般以fast cgi方式运行的时候选择这个版本,具有更好的性能。
在WIN7下:IIS7+NTS+FastCGI+vc9 是最佳搭档或者apache+fastcgi+nts+vc6。
在WINXP下:Apache+TS+Apache module +vc6最合适的搭档。
PHP:隐藏 PHP 版本就是隐藏 "X-Powered-By: PHP/5.1.2-1+b1" 这个,开启 php.ini,加入: expose_php = Off 完成以上两个设定后,重新启动 Apache 即可。
另外,讲几个最基本的php的安全设置
disable_functions = system,exec,shell_exec,passthru,popen,dl,phpinfo
display_errors = Off
allow_url_fopen = Off
safe_mode = On
open_basedir = /usr/local/httpd/htdocs/dir1:/usr/local/httpd/htdocs/dir2
#目录权限控制,dir1中的php程序就无法访问dir2中的内容。反过来也不行
Apache:
开启 httpd.conf,加入以下两行:
ServerTokens ProductOnly
ServerSignature Off
安装pdo
pecl install pdo
PHP_PDO_SHARED=1 pecl install pdo_mysql
php.ini中添加
extension=pdo.so
extension=pdo_mysql.so
安装php-gd的依赖有:
(1/9): freetype-2.3.11-6.el6_2.9.x86_64.rpm | 359 kB 00:00
(2/9): libX11-1.3-2.el6.x86_64.rpm | 582 kB 00:00
(3/9): libX11-common-1.3-2.el6.noarch.rpm | 188 kB 00:00
(4/9): libXau-1.0.5-1.el6.x86_64.rpm | 22 kB 00:00
(5/9): libXpm-3.5.8-2.el6.x86_64.rpm | 59 kB 00:00
(6/9): libjpeg-6b-46.el6.x86_64.rpm | 134 kB 00:00
(7/9): libpng-1.2.49-1.el6_2.x86_64.rpm | 182 kB 00:00
(8/9): libxcb-1.5-1.el6.x86_64.rpm | 100 kB 00:00
(9/9): php-gd-5.3.3-14.el6_3.x86_64.rpm | 104 kB 00:00
To Setup Crontab Note: In order to run Sugar Schedulers, add the following line to the crontab file:
* * * * * cd /var/www/html/sugarcrm; php -f cron.php > /dev/null 2>&1
sugarcrm安装注意
1、建数据库,ftp用户(权限700改为755),增加apache虚拟机.conf文件;
/usr/bin/mysql -u root -p
CREATE DATABASE sugarcrmdb;
GRANT all privileges on sugarcrmdb.* to 'sugarcrmuser'@'localhost' identified by '123456=abcd';
flush privileges;
use proftpddb;
INSERT INTO `ftpusers` VALUES (2, 'sugarcrm', password('123456=abcd'), 10002, 10000, '/var/www/html/sugarcrm', '/sbin/nologin', 0, '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
2、php和mysql的版本要符合要求;
http://support.sugarcrm.com/05_Resources/03_Supported_Platforms
3、有几个目录需要777权限,766会有问题;支持php-gd,php-imap,php.ini上传最大6M以上;Mysql用localhost,如未开远程的话;
When you begin the installation process, a system check will be performed on the web server on which the Sugar files are located in order to make sure the system is configured properly and has all of the necessary components to successfully complete the installation.
The system checks all of the following:
PHP version – must be compatible with the application
Session Variables – must be working properly
MB Strings – must be installed and enabled in php.ini
Database Support – must exist for MySQL, SQL Server, Oracle, or DB2
Config.php – must exist and must have the appropriate permissions to make it writeable
The following Sugar files must be writeable:
/custom
/cache
/modules
/upload
If the check fails, you will not be able to proceed with the installation. An error message will be displayed, explaining why your system did not pass the check. After making any necessary changes, you can undergo the system check again to continue the installation.
.htaccess
To Setup Crontab Note: In order to run Sugar Schedulers, add the following line to the crontab file:
* * * * * cd /var/www/html/sugarcrm; php -f cron.php > /dev/null 2>&1
4、编译Proftpd出错:
modules/mod_sql_mysql.o: In function `cmd_checkauth':
mod_sql_mysql.c:(.text+0x3e8): undefined reference to `make_scrambled_password'
collect2: ld returned 1 exit status
make: *** [proftpd] Error 1
解决:1.3.4有这个Bug 3669 - mod_sql_mysql.so: undefined symbol: make_scrambled_password with MySQL 5.5 on Fedora.
libmysqlclient had no make_scrambled_password function required for compiling mod_sql_mysql module. So, he installed an older version of mysql libraries (5.0) and everything went just fine.
http://www.proftpd.org/docs/NEWS-1.3.4
5、
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYPE=MyISAM COMMENT='proftpd group table'' at line 6
这个问题是由于MYSQL查询语句写法 TYPE=MyISAM 太老了,在MYSQL 5.5上已经被禁用,将安装程序包的TYPE=MyISAM 全部改成ENGINE=MyISAM就能正常安装了。一般地,ENGINE 选项是不必要的;除非默认已经被改变了,MyISAM是默认存储引擎。 不同的表类型是:
ISAM 原来的表处理器
MyISAM 全新二进制可移植的表处理器
HEAP 用于该表的数据仅仅存储在内存中
从3.23版本以后,这条语句可以省略,默认都是myisam类型,很多php程序(比如博客程序bo-blog)要兼容各种环境,所以会加上“TYPE=MyISAM”,而在新版本的mysql(5.5以上)又禁用了“TYPE=MyISAM”,采用“ENGINE=MyISAM”,所以就会报错,也只要更改安装文件包里的安装文件就可以了.