centos 安装 apache nginx php mariadb

1 安装前准备
为了安装额外的扩展库, 最好安装EPEL额外源, 我的系统是centos7

rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2 安装 nginx

yum instal nginx
  • 开启nginx服务
systemctl start nginx
  • 查看nginx状态
systemctl status nginx

有如下信息表示服务正常开启

Active: active (running) since Wed 2017-01-18 05:37:48 UTC; 19h ago

设置开启启动

systemctl enable nginx
  • 开启防火墙
firewall-cmd --add-service=http --permanent

查看下端口有没打开

iptables -n -L

如果看到如下信息, 表示服务开启正常

ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 ctstate NEW

3 安装apache

yum install httpd
  • 因为nginx占用了80端口, 给apache换个88端口
vi /etc/httpd/conf/httpd.conf 

将Listen 的端口改成 88
开启apache服务

systemctl start httpd

查看apache状态

systemctl status httpd

看到这个表明服务器开启正常

Active: active (running) since Tue 2017-01-17 02:48:38 UTC; 1 day 23h ago

然后你就可以看到apache的欢迎页面了.
设置开机启动

systemctl enable httpd

3 安装mariadb
mariadb是mysql的一个开源分支

yum install mariadb mariadb-server

开启mariadb服务

systemctl start mariadb

查看服务器状态

systemctl status mariadb

设置root密码

mysql_secure_installation

按照提示设置就可以了, 没什么特别
然后重启mariadb服务

systemctl restart mariadb

4 安装 php 及其扩展

yum install -y php php-fpm php-cli php-apc php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-mbstring php-mcrypt php-mssql php-soap libjpeg* php-bcmath php-mhash php-pecl-memcache

安装了一堆插件, 看自己想要增减

  • 安装完之后, apache可以直接解析php了, 主要是在 /etc/httpd/conf.d 多了php.conf的配置文件, 然后照例写个测试
<?php
phpinfo();
?>

5 用php-fpm解析php

  • 首先开启php-fpm服务
systemctl start php-fpm
  • 配置nginx
{
    listen 80;
    server_name www.foo.com;
    root /data/www/foo;
    index index.html index.php;

    location / {
        if(!-e $request_filename) {
            rewrite . /index.php last;
        }
    }

    location ~\.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
    }

    location ~/.ht {
        deny all;
    }
    access_log /data/logs/foo.access.log;
}

使用 nginx -t, 发现出现如下错误

nginx: [emerg] unknown directive "if(!-e" in /etc/nginx/conf.d/foo.conf:9

度娘一下发现, if 和 ( 之间必须有个空格, oh, may god.
重新修改了一下

{
    listen 80;
    server_name www.foo.com;
    root /data/www/foo;
    index index.html index.php;

    location / {
        if (!-e $request_filename) {
            rewrite . /index.php last;
        }
    }

    location ~\.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
    }

    location ~/.ht {
        deny all;
    }
    access_log /data/logs/foo.access.log;
}

重启nginx, 没再发现错误了, 打开网站, 竟然出现502 502 502

502 Bad Gateway

再次度娘, 发现很多都说连接数不够的之类, 我网页都没打开过怎么可能出现连接数的问题~~
直到找到这位仁兄的文章, 才明了, 再次感谢这个困扰我那么旧的问题
http://www.tuicool.com/articles/jURRJf

竟然是yum安装时, 只允许127.0.0.1的地址接入, 而转发的是公网地址, 导致直接被deny, 可以通过日志查看

tail -f /var/log/php-fpm/*.log

如果看不到错误信息, 还要修改配置www.conf, 将

catch_workers_output = yes

再看看错误信息

WARNING: [pool www] child 22741 said into stderr: "ERROR: Connection disallowed: IP address '172.16.9.59' has been dropped."

只要将配置文件www.conf中的

listen.allowed_clients = 127.0.0.1

注销或者改成你的公网ip即可

  • 其实还可以配置php-fprm使用unix套接字来处理php, 这样就可以避免了tcp的开销, 只需将
listen = 127.0.0.1:9000

改成

listen = /run/php-fpm/php5-fpm.sock

还要更改nginx的配置文件

{
    listen 80;
    server_name www.foo.com;
    root /data/www/foo;
    index index.html index.php;

    location / {
        if (!-e $request_filename) {
            rewrite . /index.php last;
        }
    }

    location ~\.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php-fpm/php5-fpm.sock;
        fastcgi_index index.php;
    }

    location ~/.ht {
        deny all;
    }
    access_log /data/logs/foo.access.log;
}

据说nginx配置文件还可以精简下, 尝试下改成这样

server
{
    listen 80;
    server_name www.foo.com;
    index index.html index.php;
    root /data/www/foo;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~.php$ {
        try_files $uri = 404;
        include fastcgi.conf;
        fastcgi_pass unix:/run/php-fpm/php5-fpm.sock;
    }

    location ~/.ht {
        deny all;
    }
    access_log /data/logs/foo.access.log;
}

是因为fastcgi.conf已经包含了fastcgi_params里面的所有配置, 而且还包含了

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

所以清爽了不少.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值