Java 實戰開發之Nginx、Mysql環境安裝(二)

本節會安裝Nginx,Mysql.

1、Nginx反向代理服務器

一、Nginx簡介

Nginx是一款輕量級Web服務器、也是一款反向代理服務器.php

做用:
(1)、能夠直接支持Rails和PHP的程序
(2)、可做爲HTTP反向代理服務器
(3)、做爲負載均衡服務器
(4)、做爲郵件代理服務器
(5)、幫助實現前端動靜分離html

特色:
高穩定、高性能、資源佔用少、支持熱部署前端

二、Nginx安裝

安裝環境:centOS 6.8 64位
安裝版本:1.0.2mysql

安裝步驟:nginx

  1. 安裝gcc(命令:yum install gcc,可輸入 gcc -v 查詢系統是否自帶安裝)
  2. 安裝pcre(命令:yum install pcre-devel)
  3. 安裝zlib(命令:yum install zlib zlib-devel)
  4. 安裝openssl(命令:yum install openssl openssl-devel,如需支持ssl,才需安裝openssl)

綜合命令:sql

yum install gcc pcre-devel zlib zlib-devel openssl openssl-devel

5.下載源碼包,選擇穩定版本,解壓縮安裝(http://www.nginx.org/數據庫

wget http://nginx.org/download/nginx-1.10.2.tar.gz

6.解壓縮apache

tar -zxvf nginx-1.10.2.tar.gz

7.進入Nginx目錄以後執行 ./configure
1)、也能夠指定安裝目錄,增長參數 --prefix=/usr/nginx
2)、若是不指定路徑,能夠經過 whereis nginx 來查詢vim

8.繼續執行 make
9.繼續執行 make installtomcat

默認安裝在 /usr/local/nginx

安裝成功以後,能夠測試訪問,默認是80端口:

clipboard.png

三、Nginx經常使用命令

1.測試配置文件

安裝路徑下的 /nginx/sbin/nginx -t

2.啓動命令

安裝路徑下的 /nginx/sbin/nginx

/usr/local/nginx/sbin/nginx

3.中止命令

安裝路徑下的 /nginx/sbin/nginx -s stop
或:nginx -s quit

4.重啓命令

安裝路徑下的 /nginx/sbin/nginx -s reload

5.查看進程命令

ps -ef | grep nginx

6.平滑重啓

kill -HUP [Nginx主進程號(即查看進程命令查到的PID)]

7.增長防火牆訪問權限

sudo vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

重啓防火牆:

sudo service iptables restart

四、Nginx虛擬域名配置及測試

網站域名配置

修改配置文件:

[root@corwien conf]# pwd
/usr/local/nginx/conf

# 添加文件夾
mkdir vhost

編輯nginx配置文件:

vim nginx.conf

clipboard.png

nginx.conf 配置文件

#user  nobody;
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  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

    include vhost/*.conf;
    # 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;
    #    }
    #}

}

編輯hosts配置文件:

vim /etc/hosts

clipboard.png

編輯域名配置:

[root@corwien vhost]# pwd
/usr/local/nginx/conf/vhost
[root@corwien vhost]# vim www.emall.com.conf
server {
     listen 80;
     autoindex on;
     server_name www.emall.com;
     access_log /usr/local/nginx/logs/access.log combined;
     index index.html index.htm index.jsp index.php;
     # root /product/ftpfile/;
     #error_page 404 /404.html;
     if ( $query_string ~* ".*[\;'\<\>].*" ){
         return 404;
      }
      location / {
          proxy_pass http://127.0.0.1:8080;
          add_header Access-Control-Allow-Origin *;
       }

}

驗證:

(1)先開啓Tomcat服務

# 開啓Tomcat服務
/developer/apache-tomcat-7.0.82/bin/startup.sh

(2)使用域名 www.emall.com 訪問:

clipboard.png

咱們能夠看到,能夠正常訪問。

圖片文件夾域名配置

建立配置:

[root@corwien vhost]# pwd
/usr/local/nginx/conf/vhost
[root@corwien vhost]# vim image.emall.com.conf

編輯圖片目錄域名配置文件

server {
     listen 80;
     autoindex off;
     server_name image.emall.com;
     access_log /usr/local/nginx/logs/access.log combined;
     index index.html index.htm index.jsp index.php;
     # root /product/ftpfile/;
     #error_page 404 /404.html;
     if ( $query_string ~* ".*[\;'\<\>].*" ){
         return 404;
      }

     location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* {
         deny all;
      }

      location / {
          root /ftpfile/;
          add_header Access-Control-Allow-Origin *;
       }

}

若是咱們使用 image.emall.com/m.jpg 文件,會出現404的問題,由於咱們的域名配置文件有誤,應該添加後綴名 /usr/local/nginx/conf/vhost/image.emall.com 改成
/usr/local/nginx/conf/vhost/image.emall.com.conf,由於咱們在 nginx.conf 配置文件中這樣加載的 include vhost/*.conf;

靜態資源域名配置

[root@corwien vhost]# vim s.emall.com.conf

編輯 s.emall.com.conf:

server {
     listen 80;
     autoindex off;  # 是否自動建立索引,若是開啓則會列出下面目錄文件的索引
     server_name s.emall.com;
     access_log /usr/local/nginx/logs/access.log combined;
     index index.html index.htm index.jsp index.php;
     # root /product/ftpfile/;
     #error_page 404 /404.html;
     if ( $query_string ~* ".*[\;'\<\>].*" ){
         return 404;
      }

     location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* {
         deny all;
      }

      location / {
          root /product/front/;
          add_header Access-Control-Allow-Origin *;
       }

}

配置以後,須要重啓Nginx服務器:

[root@corwien nginx]# sbin/nginx -s reload

2、MySQL安裝

一、安裝環境

系統環境:CentOS 6.8 64位
MySQL版本:mysql-server-5.1.73(阿里軟件源默認帶的版本)

二、MySQL下載

yum -y install mysql-server

能夠經過 rpm -qa | grep mysql-server 此語句來檢查是否已經安裝mysql-server

mysql 默認配置文件在 /etc/my.cnf

(2)、字符集配置:

vim /etc/my.cnf

添加配置,在[mysqld]節點下添加:

default-character-set=utf8
character-set-server=utf8

:wq保存退出

/etc/my.cnf文件:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 設置字符集[20180107]
default-character-set=utf8
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

關於中文亂碼問題:
在5.1版本,爲了解決中文亂碼問題,my.ini內[mysql]和[mysqld]中都寫:

default-character-set=utf8

在5.5版本,[mysql]內能夠這麼寫,[mysqld]內不能再這麼寫了,而是寫:

character-set-server=utf8

三、開機自啓動配置

(1)、執行 chkconfig mysqld on
(2)、執行 chkconfig --list mysqld查看(若是2-5位啓用on狀態即OK)

clipboard.png

(3)、啓動MySQL

service mysqld start

(4)、登陸mysql
無密碼登陸

[root@corwien corwien]# mysql -u root

四、防火牆配置

(1)、vim /etc/sysconfig/iptables
(2)、-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT 將以上配置添加到防火牆配置中
(3)、sudo service iptables restart 執行命令重啓防火牆

-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

clipboard.png

五、刪除匿名用戶

(1)、查看目前MySQL的用戶

select user,host,password from mysql.user

clipboard.png

咱們能夠看到有兩個匿名用戶,出於安全考慮,咱們把匿名用戶刪掉:

mysql> delete from mysql.user where user = '';

clipboard.png

刪除匿名用戶,執行如下SQL(安全考慮)
查看是否有匿名用戶:select user, host from mysql.user;
刪除匿名用戶:delete from mysql.user where user='';
刷新、使以上操做生效:flush privileges

六、建立用戶

mysql> insert into mysql.user(Host,User,Password) values("localhost", "emall", password("518666"));

七、修改用戶權限

查看全部用戶權限:

mysql> select * from mysql.user \G;

\G 表示對輸出的內容進行格式化

clipboard.png

\G 格式化以後輸出的數據:
clipboard.png

咱們能夠看到,剛纔添加的新用戶 emall 的權限都爲N,因此這裏要對這個用戶權限作開放:

clipboard.png

根據本身實際狀況決定是否開放什麼權限

語法模板:

grant all privileges on 庫名.表名 to '用戶名'@'IP地址' identified by '密碼' with grant option;
flush privileges;
庫名:要遠程訪問的數據庫名稱,全部的數據庫使用「*」 
表名:要遠程訪問的數據庫下的表的名稱,全部的表使用「*」 
用戶名:要賦給遠程訪問權限的用戶名稱 
IP地址:能夠遠程訪問的電腦的IP地址,全部的地址使用「%」 
密碼:要賦給遠程訪問權限的用戶對應使用的密碼

舉例:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'lxh' WITH GRANT OPTION; # 全部的地址均可以使用root用戶,密碼爲lxh遠程訪問全部的數據庫
flush privileges;
GRANT ALL PRIVILEGES ON testdb.* TO 'lxh'@'172.16.52.225'  IDENTIFIED BY 'lxh' WITH GRANT OPTION; # IP爲172.16.52.225的電腦可使用lxh用戶,密碼爲lxh遠程訪問數據庫testdb的全部表

賦予emall這個用戶全部權限:

mysql> grant all privileges on emall.* to emall@'%' identified by '518666' with grant option;

或者只賦予查增改權限,不讓此用戶對數據庫進行刪除操做:

mysql> grant select,update,create on emall.* to emall@'%' identified by 'emall' with grant option;

咱們再查看受權是否成功:

mysql> select * from mysql.user \G;

clipboard.png

已經授過權了,爲何這裏仍是N呢?

緣由是:select * from mysql.user \G;給出的是全局的權限,而不是針對某個DB或者SCHEMA得權限。你賦權語句是grant all privileges on emall.* to 'emall'@'%' identified by '518666' with grant option;也就是針對emall這個數據庫賦權。那麼你天然會得出權限都是‘N’了。

那麼,這是否是錯誤的呢?答案固然不是了。
具體的流程是這樣的:假如你有一個SQL的query,好比是select * from emall.table1。mysql會首先去user表中對比你(emall@localhost)是否有select 權限?發現是N.而後,它並不會直接拒絕這個query,而是會繼續對照Database級別上檢驗權限,會db.user='emall' and db.db='emall' and db.host='localhsot',若是檢驗經過,則經過,不然繼續向下檢驗,host.db='emall' and host.host='localhost',若檢驗經過,則經過,不然繼續向下檢驗table級別,而後是column級別。

這就是mysql的權限檢查模塊的工做過程。

並非你出現了錯誤,而是mysql的內部機制就是這樣的。
建議你仔細看下這篇文章http://tech.it168.com/a2010/0...。這裏面對mysql的權限機制作了詳細的講解。

相關文章:
mysql grant 受權不起做用

因此,這裏咱們給全部庫受權,則這裏的權限便可爲Y:

mysql> grant all privileges on *.* to emall@'%' identified by '518666' with grant option;

clipboard.png

八、修改root密碼

set password for root@localhost=password('yourpassword');

set password for root@127.0.0.1=password('yourpassword');

設置root密碼:

mysql> set password for root@127.0.0.1=password('root');
Query OK, 0 rows affected (0.00 sec)

從新登陸:

mysql -uroot -p

九、建立數據庫

查看全部數據庫:

mysql> show databases;

建立數據庫:

mysql> create database `emall` default character set utf8 collate utf8_general_ci;

十、忘記root密碼解決方法

密碼太多記不住??你是否忘記了MySQL的root密碼? 經過如下4步就可從新設置新密碼:

  1. 中止 mysql server
service mysqld stop

2.打開終端,輸入:

找到mysql安裝目錄:

[root@corwien download]# whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
[root@corwien download]# /usr/bin/mysqld_safe --skip-grant-tables

3.打開另外一個新終端,輸入:

[root@corwien corwien]# /usr/bin/mysql -u root

mysql>  UPDATE mysql.user SET authentication_string=PASSWORD('root') WHERE User='root';
ERROR 1054 (42S22): Unknown column 'authentication_string' in 'field list'
mysql>  UPDATE mysql.user SET password=PASSWORD('root') WHERE User='root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 3  Changed: 2  Warnings: 0

mysql>  FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

4.而後重啓mysql,從新登陸:

[root@corwien corwien]# service mysqld start
[root@corwien corwien]# mysql -uroot -p
Enter password:
相關文章
相關標籤/搜索