搜索
首页数据库mysql教程Apache+Nginx+MySql+PHP配置介绍

Apache+Nginx+MySql+PHP配置介绍,有需要的朋友可参考一下

Apache+Nginx+MySql+PHP配置介绍,有需要的朋友可参考一下

1. 装完系统后开启 sshd, 关闭防火墙 (不然外链接是访问不了 apache) 关闭安全系统 SELinux( 不然报403 访问页面错误 )
   重启后永久性生效
        chkconfig sshd on  ( 开启 sshd)
        chkconfig iptables off ( 关闭防火墙 )
        修改 /etc/selinux/config文件中设置 SELINUX=disabled  ( 关闭SELinux)
   即时生效 , 重启后失效
        #service sshd start  ( 开启 sshd)
        #service iptables stop( 关闭防火墙 )
        #setenforce 0 ( 关闭 SELinux)
2. 默认centos6 装了 mysql+apache 可用rpm -q mysql 或 httpd 查看是否已经装 默认已安装        
chkconfig设成开机启动 ( chkconfig --list 是列出当前 , 如果 list 里面没有mysqld 和 httpd则用 chkconfig --add mysqld 和httpd)
接着chkconfig httpd on 和 chkconfig mysqld on 这只是要重启后才永久生效   如果即时生效 用 service httpd start  service mysqld start
3. 修改apache 的配置文件比如更改网站文档目录或不同域名指向不同的文件夹或开端口等等 默认配置文件在 /etc/httpd/conf 文件夹下的 httpd.conf
    1).修改网站文档目录在 httpd.conf 下修改  DocumentRoot "/var/www"  这二要目录一样
    2).不同域名指向不同目录 在httpd.conf 查找 #Inculde /etc/httpd/conf/httpd-vhosts.conf 去掉前面的 #号如果不存在刚加上这一句且创建 httpd-vhosts.conf 文件( 或者直接在 /etc/httpd/conf.d 目录下建一个 httpd-vhosts.conf因为 http.conf 已经#Inculde /etc/httpd/conf.d/*.conf 了 )              
      # 确保 Apache在监听 80 端口 即 httpd.conf文件里要有下面一句话 其实也可以放在httpd-vhosts.conf里

      Listen 80      修改   httpd-vhost.conf文件格式如下
      # 为虚拟主机在所有 IP 地址上监听 
 

 代码如下 复制代码
NameVirtualHost *:80
     
          ServerAdmin edu1211@163.com
          DocumentRoot /var/www/
          ServerName www.example.com
          # 你可以在这里添加其他指令
     

     
         DocumentRoot /var/www/a
         ServerName www.a.com
         # 你可以在这里添加其他指令
         Errorlog "logs/a.log"
         CustomLog "logs/b.log" common
      
 

 

上面可以在外部电脑更改 /etc/hosts 文件 如在 windows平台更改 C:WindowsSystem32driversetchosts 文件 (假设 192.168.0.110 是我们部署的服务器 )
127.0.0.1       localhost
192.168.0.110  www.example.com
192.168.0.110   www.a.com
192.168.0.110   www.b.com

       则在外部电脑输入 www.example.com      则跳到 /var/www/目录
       则在外部电脑输入 www.a.com            则挑到 /var/www/a目录
       则在外部电脑输入 www.b.com            则挑到 /var/www/目录
       为什么输入 www.b.com 会挑到 /var/www/ 目录下 因为服务器在 httpd-vhosts.conf 找不到虚拟主机名刚默认挑到 80 端口的第一个虚拟目录下
   3). 为网站开不同端口 首先在httpd.conf或httpd-vhosts.conf(建议写在这里面)加上要监听的端口
        Listen 8080 // 添加的要开的断口
      然后 httpd-vhost.conf添加格式跟 80 端口一样
      参考文档   http://blog.csdn.net/edisonlg/article/details/7217153
4. 安装 php 初始系统是没有安装的 yum install php 即可
php配置文件是 php.ini  可以用find / -name php.ini 一般在 /etc/php.ini这个位置
现在要apache 支持 (绑定 )php 脚本语言
  即修改apache 的配置文件 httpd.conf 在#AddType application/x-tar .tgz 下加上以下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.html.var index.php//即加上访问目录时index.php其实上面可以改 /etc/httpd/conf.d目录下的 php.conf 就可以了( 建议这样因为 /etc/httpd/conf/httpd.conf已经有 Include conf.d/*.conf)
在php.conf 里面我们都可以看到 Apache 绑定PHP 脚本语言的代码所以上面的 httpd-vhosts.conf 文件可以直接写在 /etc/httpd/conf.d目录下就可以了
  同时修改php 的配置文件 php.ini  打开mysql 扩展 即去掉 ;extension=mysql.so 的分号
   然后重启 apache 即可  service httpd restart
5. 安装 phpMyAdmin 
  1) 到官网下载最新的 phpMyAdmin包
  2) 解压包 tar zxvf phpMyAdmin-2.113-all-languages.tar.gz
  3) mv phpMyAdmin-2.113-all-languages /var/www/phpMyAdmin
  4) phpMyAdmin的配置文件在 /phpMyAdmin/libraies/config.default.php 文件 (如果是以根目录下 config.ini.php 配安装例外 )
  安装完成 即可通过网页登录 但是默认 mysql 是空密码 而 phpMyAdmin禁止空密码登录 则可通过命令行的方式更改 mysql 密码或改phpMyAdmin 的配置文件让允许空密码登录
  打开配置文件找到
$cfg['Servers'][$i]['nopassword'] =false
$cfg['Servers'][$i]['AllowNoPassword'] =false
把这二行的 false都改成 true 即可
  $cfg['Servers'][$i]['auth_type'] ='cookie';
是默认的, 如果更成
$cfg['Servers'][$i]['auth_type']='config';
用config 模式时需要 user,password 参数,这时登录 PhpMyAdmin不需要输入用户名密码,安全性较低,适合多用户测试开发即
$cfg['Servers'][$i]['user']='root'; 
$cfg['Servers'][$i]['password']=''; 就生效了 即以种默认身份登录
6. 安装 PHP扩展模块
linux下 PHP 扩展安装模块比如打开 phpMyAdmin的话会提示   没有找到 PHP 扩展 mbstring,而您现在好像在使用多字节字符集。没有 mbstring 扩展的 phpMyAdmin 不能正确分割字符串,可能产生意想不到的结果 .
  则在 php.ini 加上extension=mbstring.so 重启 apache(httpd)还是不行 那么运行find / -name mysql.so 找到存放模块的目录一般是 /usr/lib/php/modules/mysql.so 这个位置则找找 /usr/lib/php/modules/目录下有没有 mbstring.so
  很显示没有 那么得安装 yum install php-mbstring 然后再重启 apache(httpd)刚提示错误没有了
7. 安装 Nginx+PHP(要源码安装 )
  1)安装 Nginx
centos默认是安装了 apache 那么用yum remove httpd 然后再安装 ngnix 因为yum 源是不包含 ngnix
  官网有提供 yum安装说明 :

 代码如下 复制代码
CentOS:
To add nginx yum repository, create a file named /etc/yum.repos.d/nginx.repo and paste one of the configurations below:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

其它系统参考 http://wiki.nginx.org/Install
当然也有源码安装但是需要你安装依赖包 ( 有点麻烦)
Nginx的配置文件在   /etc/nginx/nginx.conf
但是文件的最后是包含其它文件的    Include /etc/nginx/conf.d/*.conf;
然后   #service nginx start    #chkconfig nginx on
2)安装 PHP
但是安装PHP 的时候如果直接 yum 安装php 是不行的
因为默认情况下 Nginx和 PHP 他俩之间是一点感觉没有的,在之前,很多朋友都搭建过 Apache+PHP ,Apache+PHP 编译后生成的是模块文件,而 Nginx+PHP 需要PHP 生成可执行文件才可以,所以要利用 fastcgi 技术来实现 N ginx与 PHP 的整合,这个只要我们安装是启用 FastCGI 即可。此次我们安装 PHP不仅使用了 FastCGI ,而且还使用了 PHP-FPM这么一个东东, PHP-FPM 说白了是一个管理 FastCGI的一个管理器,它作为 PHP 的插件纯在,在安装 PHP要想使用 PHP-FPM 时就需要把 PHP-FPM以补丁的形式安装到 PHP 中,而且PHP 要与 PHP-FPM版本一致,这是必须的,切记!
所以 PHP得源码安装的时候把 PHP-FRM 选项带上即解压 PHP 源码包后
先安装gcc 套件这是个编译器 yum install gcc   yum install gcc-c++ (libmcrypt要用到 )
./configure --prefix=/usr/local/php --with-gd --enable-mbstring --with-mysql --with-mysqli --with-jpeg-dir --with-zlib --with-openssl --with-mcrypt --enable-fastcgi --enable-fpm

    此过程会出现以下错误
a)  error: xml2-config not found. Please check your libxml2 installation.
因为php5 是需要 libxml2支持   rpm -qa|grep libxml2
只需要安装 libxml2-dev , yum install libxml2-devel 
b)  同样出会出现 openssl 错误那么yum install openssl-devel
c)  configure: error: jpeglib.h not found. 解决方法是: yum install libjpeg-devel
d)  configure: error: png.h not found   解决方法是: yum install libpng-devel
e)  configure: error: mcrypt.h not found. Please reinstall libmcrypt.      
解决方法yum 码安装不了 所以得百度搜 libmcrypt源码
下载页面 http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/ ( 这个跟php 版本可以不一至 )
libmcrypt./configure  ( 这里不能 --prefix指定安装路径不然下面安装还是会出错 ) 可能会报check ....no 忽视这些 checking for g77... no  什么fastcgi 警告   但是没出现错误比如 configure: error: No F77 compiler found 就可以了所以
接着#make   #make install
跳到php 目录下继续 ./configure  --prefix=/usr/local/php --with-gd --enable-mbstring --with-mysql --with-mysqli --with-jpeg-dir --with-zlib --with-openssl --with-mcrypt --enable-fastcgi --enable-fpm
安装php 过程会遇到的报错可以参考 http://www.cnblogs.com/1110111abc/articles/2528102.html
接着
#make
#make install
#cp php.ini-production   /usr/local/php/etc/php.ini
#mv /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf  //不然下面启动php-fpm会报错找不到php-fpm.conf文件

下面我们就要启动 PHP-FPM
#/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini //如果不加上-c参数指定他的php.ini参数(必须不然以后没法修改他的php.ini文件)    (lsof -i:9000  可以查看9000端口占用详细情况)  网上说后面加上start是不行的(可能以前版本要吧)

vim /etc/rc.local
在这个文件的最后加入如下语句 :
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini   这里你所要启动的服务一定要写成是绝对路径的形式 !! 不然的话, 这个服务是启动不起来的 !!
最后一步关联 Nginx与 PHP

#vim /etc/nginx/conf.d/default.conf
#location ~ .php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
#    include        fastcgi_params;
#}

改成

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;
}       提示:Nginx 自己知道咋找 PHP 了还不行,还需要 PHP知道咋找 Nginx ,这点我们不需要担心, PHP-FPM 已经在配置文件中定义了从哪接受 PHP 请求,我们可以打开配置文件看一下 (Apache 却不同 只要 Apache绑定了 PHP 就行)

#vi /usr/local/php/etc/php-fpm.conf
如果要把9000 端口改成其它的端口那么二个文件的端口要改成一至
最后重启Nginx


#service nginx restart建一个测试页面加上 vim /usr/share/nginx/html/a.php
phpinfo();
?>
执行phpinfo();date 时区不正确 解决方法vim /usr/local/php/etc/php.ini 设置为 date.timezone = Asia/Shanghai
大功告成  参考 http://wenku.baidu.com/view/570b78b765ce050876321377.html
如果要在Nginx 开端口什么的跟 Apache 上一样

 代码如下 复制代码

server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ .php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ .php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #location ~ /.ht {
    #    deny  all;
    #}
}

复制一下server 然后修改一下就可以了

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。