CentOS6.5下LNMP环境的搭建

#写的很差,大牛勿喷
#其实我很努力
OS CentOS6.5
1. 关闭 SELinux, 关闭防火墙
缘由: 1.SELinux 确实能够提升服务器的安全性,可是对于服务器的性能存在必定的影响,同时它的复杂规则对于管理人员来讲很是头疼,因此暂时关掉吧,非要开启也是能够的;
     2. 关闭防火墙是为了让初学者学习更加方便,对防火墙技术好的人能够开启防火墙。在企业环境中,只有配置了外网 IP 的服务器再会开启防火墙,可是即便是外网 IP ,通常状况下也不轻易的开启防火墙,高并发、高流量的业务服务器仍然不能开启防火墙,防火墙对性能存在必定的损耗,集群环境下的服务器防火墙通常都是关闭的,安全性能够借助硬件防火墙等设备进行提升,并且,如今机房的网络总体架构都依托于物理防火墙。
2.安装完成系统后进行软件升级
#service iptables stop
#chkconfig iptables off
#sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/’/etc/selinux/config
此步骤是快速修改的方法,也能够经过 vi 进行修改, SELINUX 修改完成后咱们须要重启服务器,可是咱们暂时不重启服务器,对服务器软件进行升级完成和启动项作了优化后再重启服务器;
#yum update -y    // 更新软件包,这个选项根据我的喜爱吧
2. 精简系统开机启动项:
通常状况下,服务器刚装完系统后有必要保留的开机自启动服务只有 5 个,具体以下:
1.sshd : 远程链接 ssh ,很少说;
2.rsyslog: 日志相关文件,这是操做系统提供的一种机制
3.network: 服务器要联网,必须开启这个服务啊
4.crond: 这个服务主要用来执行系统及用户配置的任务计划,有周期性执行的任务的时候必须开启,生产环境下必须开启这个服务
5.sysstat: 服务器性能检测工具,收集服务器运行数据,判断运行是否正常
操做命令以下:
[root@localhost ~]# LANG=en
[root@localhost ~]# for root in `chkconfig --list|grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $root off;done
[root@localhost ~]# for root in crond network rsyslog sshd sysstat;do chkconfig --level 3 $root on;done  //sysstat 服务在服务器中极可能没有,若是系统提示,咱们只需把这个服务在这条命令中去掉就能够了
[root@localhost ~]# chkconfig --list|grep 3:on
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
rsyslog            0:off    1:off    2:on    3:on    4:on    5:on    6:off
sshd               0:off    1:off    2:on    3:on    4:on    5:on    6:off
sysstat            0:off    1:on     2:on    3:on    4:on    5:on    6:off

接下来咱们重启服务器就能够了,重启的过程当中,你会发现速度很是快;php

安装配置html

.Nginx 的安装部署
因为 CentOS6.5 默认是没有 Nginx 源的,咱们须要手动安装 nginx yum
1. 先执行下条命令进行nginx源的安装:
2. 查看 yum nginx 信息
[root@LNAP /]# yum info nginx
已加载插件:fastestmirror
Determining fastest mirrors
 * base:
mirrors.yun-idc.com
 * extras: mirrors.neusoft.edu.cn
 * updates: mirrors.neusoft.edu.cn
base                     | 3.7 kB     00:00    
extras                   | 3.4 kB     00:00    
extras/primary_db      |  37 kB     00:00    
nginx                    | 2.9 kB     00:00    
nginx/primary_db        |  22 kB     00:00    
updates                  | 3.4 kB     00:00    
updates/primary_db       | 4.3 MB     00:05    

可安装的软件包
Name        : nginx
Arch        : x86_64
Version     : 1.10.3
Release     : 1.el6.ngx
Size        : 861 k
Repo        : nginx
Summary     : High performance web server
URL         :
http://nginx.org/
License     : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
            : a mail proxy server.
3 ,安装并启动 nignx
[root@LNAP /] #yum install nginx -y
........
.......
.......
[root@LNAP /]# service nginx start
Starting nginx:                                            [  OK  ]
[root@LNAP /]# chkconfig nginx on     // 设置 nginx 开机自启动
4 ,而后进入浏览器,根据你的 Linux 服务器的 IP 输入  http://x.x.x.x  测试,若是看到一下内容,就说明 Nginx 已经安装成功了
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to  nginx.org .
Commercial support is available at  nginx.com .
Thank you for using nginx.
若是不能链接到 nginx ,缘由不少,可是能够先检查  
  1,nginx 服务是否真的起来了
  2,linux 服务器防火墙是否打开
.Nginx 配置 SSL 支持的 HTTPS
默认状况下 ssl 模块并未被安装,若是要使用该模块则须要在编译时指定 –with-http_ssl_module 参数,安装模块依赖于 OpenSSL 库和一些引用文件,一般这些文件并不在同一个软件包中。一般这个文件名相似 libssl-dev
安装 opennssl
[root@LNAP /]# yum install openssl -y
生成证书
能够经过如下步骤生成一个简单的证书:
首先,进入你想建立证书和私钥的目录,
[root@LNAP /]# cd /etc/nginx/
建立服务器私钥,命令会让你输入一个口令:
[root@LNAP /]# openssl genrsa -des3 -out nginx.key 2048
建立签名请求的证书( CSR ):
[root@LNAP /]# openssl req -new -key nginx.key -out nginx.csr
在加载 SSL 支持的 Nginx 并使用上述私钥时除去必须的口令:
[root@LNAP /] # cp nginx.key  nginx.key.org
[root@LNAP /] # openssl rsa -in  nginx.key.org  -out nginx.key
最后标记证书使用上述私钥和 CSR
[root@LNAP /] # openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt
配置 nginx
修改 Nginx 下你的 www 某一个网站的配置文件,让其包含新标记的证书和私钥:
#vim /etc/nginx/conf.d/default.conf  // 例如这个默认的
server {
     listen       443;
     server_name  localhost;
      ssl on;
      ssl_certificate       server.crt;
      ssl_certificate_key   server.key;
#     ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
#     ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#     ssl_prefer_server_ciphers on;
# 这些内容根据本身的需求进行更改
 }
咱们能够复制这个默认的配置文件到 conf.d 目录下面,由于在 nginx nginx.conf 中有这么一项
 include /etc/nginx/conf.d/*.conf;   这样才显得规整一点,容易控制调节,不建议在 nginx.conf 文件中直接进行修改。
也就是说,只要这个配置文件下全部的虚拟主机端口 IP 不冲突,全部运行的网站都是可访问的;因此默认的哪的 default.conf 仅仅是其中一个, nginx 给咱们提供的一个模板,咱们能够对它进行复制成本身想要的另一个修改,用来使用。
重启 nginx
这样就能够经过如下方式访问: https://192.168.34.5
 
有时候咱们还有这种需求,为了防止用户去访问80不安全的网页,咱们在nginx中能够配置端口重定向,在用户访问http://192.168.34.8的时候服务器自动跳转到
https://192.168.34.8,若是咱们仍是存在php访问支持,则按照下面的方法进行设置
server {
        listen       80;
        server_name  www.cloud.com;
       rewrite ^(.*)$ https://$host$1 permanent;
 
        location / {
            root   /data/html/phpwind/;
            index  index.html index.htm index.php;
          }
       location ~ \.php$ {
         root           /data/html/phpwind/;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  /data/html/phpwind/$fastcgi_script_name;
         include        fastcgi_params;
          }
}
 
. 安装 PHP 环境,并配置 Nginx ,支持 PHP 环境
若是 yum 安装不存在问题,能够利用 yum 进行安装,本人曾经安装的时候遇到过问题:
1. 若是 yum install php* -y 这条命令没有问题,直接进行安装就能够,若是不行,依次执行下面的命令:
#yum install php -y
#yum install php-fpm -y
php 开机自启动的命令不是 chkconfig php on, 而是 chkconfig php-fpm on ; 在这里,咱们将 php-fpm 设置未开机自启动;
配置 php-ngin 环境:
#vim /etc/nginx/conf.d/default.conf
server {
----------- 省略部份内容 -----------
location ~ \.php$ {
  root           html;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME /data/html/phpwind/ $document_root$fastcgi_script_name;
  include        fastcgi_params;
  }
----------- 省略部份内容 -----------
}
这里这个 SCRIPT_FILENAM 其实就是 index.php 所在的目录,由于 nginx 的全部网页文件都存放于 /usr/share/nginx/html/ 目录下,在这个目录下能够存放多个网站的网页文件,但都是以目录的形式存放的,好比在这个目录下存放了一个 www 目录,且 www 目录下面有 index.php ,这里的 SCRIPT_FILENAME 就写成
fastcgi_param   /user/share/nginx/html/www/ $document_root$fastcgi_script_name;
若是把网页文件放在了别的目录,如 /data/nginx/html/www/ , 则改为
fastcgi_param  /data/nginx/html /www/ $document_root$fastcgi_script_name;
全部的文件路径建议使用绝对路径
 
因此,一份正常的 www.conf 配置文件应该是这样的:
 
server {
    listen       443;
    server_name  www.tcloud.com;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    ######SSL#############
    ssl on;
    ssl_certificate       /etc/nginx/cert/discuz.crt;
    ssl_certificate_key   /etc/nginx/cert/discuz.key;
    ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers on;     
    ########SSL模块##########
    location / {
        root   /data/html/phpwind/;
        index  index.html index.htm index.php;
    }
     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
   ##########PHP模块###########  
   location ~ \.php$ {
         root           /data/html/phpwind/;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  /data/html/phpwind/$fastcgi_script_name;
         include        fastcgi_params;
    }
    ##########PHP#############
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #location ~ /\.ht {
    #    deny  all;
    #}
}
 
. 安装 MySQL 数据库 :
#yum install mysql* -y
直接安装就能够了,
修改 mysql 默认密码 :
方法 1 SET PASSWORD 命令
   mysql -u root
   mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpasswd');
方法 2 :用 mysqladmin
   mysqladmin -u root password "newpasswd"
  若是 root 已经设置过密码,采用以下方法
   mysqladmin -u root password oldpass "newpasswd"
方法 3 UPDATE 直接编辑 user
   mysql -u root
   mysql> use mysql;
   mysql> UPDATE user SET Password = PASSWORD('newpasswd') WHERE user = 'root';
   mysql> FLUSH PRIVILEGES;
在丢失 root 密码的时候,能够这样
   mysqld_safe --skip-grant-tables&
   mysql -u root mysql
   mysql> UPDATE user SET password=PASSWORD("newpasswd") WHERE user='root';
   mysql> FLUSH PRIVILEGES;
登录Mysql数据库:
mysql -u username -p
输入密码便可登陆
mysql -u root -p
[root@LNAP ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 14Server version: 5.1.73 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
相关文章
相关标签/搜索