centos 安装 apache nginx php mariadb

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/kenlong/article/details/54575764
1 安装前准备
为了安装额外的扩展库, 最好安装EPEL额外源, 我的系统是centos7

rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
1
2 安装 nginx

yum instal nginx
1
开启nginx服务
systemctl start nginx
1
查看nginx状态
systemctl status nginx
1
有如下信息表示服务正常开启

Active: active (running) since Wed 2017-01-18 05:37:48 UTC; 19h ago
1
设置开启启动

systemctl enable nginx
1
开启防火墙
firewall-cmd --add-service=http --permanent
1
查看下端口有没打开

iptables -n -L
1
如果看到如下信息, 表示服务开启正常

ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 ctstate NEW
1
3 安装apache

yum install httpd
1
因为nginx占用了80端口, 给apache换个88端口
vi /etc/httpd/conf/httpd.conf 
1
将Listen 的端口改成 88
开启apache服务

systemctl start httpd
1
查看apache状态

systemctl status httpd
1
看到这个表明服务器开启正常

Active: active (running) since Tue 2017-01-17 02:48:38 UTC; 1 day 23h ago
1
然后你就可以看到apache的欢迎页面了.
设置开机启动

systemctl enable httpd
1
3 安装mariadb
mariadb是mysql的一个开源分支

yum install mariadb mariadb-server
1
开启mariadb服务

systemctl start mariadb
1
查看服务器状态

systemctl status mariadb
1
设置root密码

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

systemctl restart mariadb
1
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
1
安装了一堆插件, 看自己想要增减

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

首先开启php-fpm服务
systemctl start php-fpm
1
配置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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
使用 nginx -t, 发现出现如下错误

nginx: [emerg] unknown directive "if(!-e" in /etc/nginx/conf.d/foo.conf:9
1
度娘一下发现, 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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
重启nginx, 没再发现错误了, 打开网站, 竟然出现502 502 502

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

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

tail -f /var/log/php-fpm/*.log
1
如果看不到错误信息, 还要修改配置www.conf, 将

catch_workers_output = yes
1
再看看错误信息

WARNING: [pool www] child 22741 said into stderr: "ERROR: Connection disallowed: IP address '172.16.9.59' has been dropped."
1
只要将配置文件www.conf中的

listen.allowed_clients = 127.0.0.1
1
注销或者改成你的公网ip即可

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

listen = /run/php-fpm/php5-fpm.sock
1
还要更改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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
据说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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
是因为fastcgi.conf已经包含了fastcgi_params里面的所有配置, 而且还包含了

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
1
所以清爽了不少.
————————————————
版权声明:本文为CSDN博主「kenlong」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kenlong/article/details/54575764

发布了6 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/supwlz/article/details/102687556