centos 安装 apache nginx php mariadb

前端之家收集整理的这篇文章主要介绍了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
systemctl start 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
{
    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,将

@H_378_404@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;

所以清爽了不少.

原文链接:https://www.f2er.com/centos/379011.html

猜你在找的CentOS相关文章