centos中安装并配置nginx+MySQL+Php

本文介绍如何在CentOS系统上配置LNMP环境,包括防火墙设置、安装Nginx、MySQL及PHP等关键步骤,并对配置文件进行了详细说明。

1.配置防火墙

打开80和3306端口

例子:[root@Centos66 ~]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 995 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 993 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9200 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 177 -j ACCEPT
COMMIT

2.启动防火墙

service iptables start

3.关闭SELINUX

例:[root@Centos66 ~]# cat /etc/selinux/config


# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
#SELINUXTYPE=targeted

并用init6命令重启系统

4.安装第三方yum源

4.1.先安装wget:yum install wget

4.2.下载atomic:wget http://www.atomicorp.com/installers/atomic

4.3.安装atomic:sh ./atomic

4.4.更新yum源:yum check-update 

5.安装nginx:yum install nginx -y

5.1.设置nginx 开机启动:chkconfig nginx on

5.2.启动nginx:service nginx start

6.安装MySQL

6.1.yum install mysql mysql-server -y

6.2.service mysqld start

6.3.chkconfig mysqld on

6.4.cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

6.5.设置root密码:mysql_secure_installation

7.安装Php

7.1.yum install php php-fpm -y

7.2.安装php组件以支持mysql

yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt

7.3.chkconfig php-fpm on

7.4.service php-fpm start

7.5.配置nginx以支持php

7.5.1.备份原有配置文件:cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak

7.5.2.修改运行权限:user nginx nginx;

/etc/nginx/nginx.conf文件例子:

user  nginx nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  300;

    gzip  on;

    client_max_body_size 300M;

    upstream mytomcats{
        server  192.168.1.197:8080;
         server  192.168.1.26:8080;
       # server  192.168.1.56:8080;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;
#            proxy_pass http://www.baidu.com;
        }

        #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   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  $document_root$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;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

7.5.3.备份原有配置文件:cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.confbak

7.5.4.添加index.php到缺省首页:index index.php index.html index.htm;

并且放开如下部分

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;
    }

而且改为:

 location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php; 

       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        include        fastcgi_params;
    }

并重启nginx:service nginx restart

8.配置Php

8.1.修改/etc/php.ini:

date.timezone = PRC

expose_php = off

short_open_tag = ON

8.2.配置Php-fpm

备份:cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.confbak

修改用户为nginx:user = nginx

#修改组为nginx:group = nginx

验证php:cd /usr/share/nginx/html并createindex.php使其内容为<? phpinfo(); ?>并chown nginx.nginx /usr/share/nginx/html -R并重启nginx和php-fpm

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值