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.
相關文章
相關標籤/搜索