centos7經常使用環境搭建

每次搭各類環境都很痛苦,遇到各類問題,所以寫下此篇記錄下centos7下一些環境的搭建過程、遇到的問題及解決方法,有其餘環境的搭建也會進行更新。本文不值得看,但能夠收藏下,已備不時之需。本文主要參考了 從0開始 獨立完成企業級Java電商網站開發服務端

修改軟件源

修改成國內的yum源能夠加快下載速度php

參考文章html

對於騰訊雲的雲服務器沒必要改, 默認是騰訊的源java

linux學習資料

jdk安裝

  1. jdk下載
wget https://download.oracle.com/otn/java/jdk/8u231-b11/5b13a193868b4bf28bcb45c792fce896/jdk-8u231-linux-x64.rpm

(不能這樣下載, 由於有個什麼破協議,須要先下載而後傳上去)mysql

上傳到服務器能夠用scp命令linux

scp root@107.172.27.254:/home/test.txt .   //下載文件

scp test.txt root@107.172.27.254:/home  //上傳文件

scp -r root@107.172.27.254:/home/test .  //下載目錄

scp -r test root@107.172.27.254:/home   //上傳目錄
  1. 卸載默認jdk, 若是有的話
  • 判斷是否有jdk
rpm -qa | grep jdk
  • 刪除
yum remove xxx
  1. 對下載的jdk rpm文件賦予權限
chmod 777 jdk.xxx.rpm
# 這裏直接給了最高權限,你們慎重
  1. 安裝
rpm -ivh jdk.xxx.rpm
  1. 默認安裝路徑 /usr/java
  2. 配置環境變量
  • 配置文件 /etc/profile
  • 配置PATH ,CLASSPATH, JAVA_HOME
  • source /etc/profile

在/etc/profile加的變量以下nginx

# 這裏 JAVA_HOME爲java的安裝目錄
export JAVA_HOME=/usr/java/jdk1.8.0_202-amd64
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/tools.jar
  1. 驗證jdk
java -version

tomcat安裝

  1. 下載(歷史版本地址https://tomcat.apache.org/dow...
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.50/bin/apache-tomcat-8.5.50.tar.gz
  1. 解壓縮
  2. 配置環境變量
# 這個我以前沒配過,貌似用處不大
export CATALINA_HOME=/devlib/apache-tomcat-8.5.50
  1. 配置utf-u字符集,支持中文,不是必須的
# ${CATALINA_HOME}/conf/server.xml URIEncoding="UTF-8"

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>
  1. 驗證
# 進入 ${CATALINA_HOME}/bin
./startup.sh
# 訪問http://${ip}:8080/
  1. 經常使用命令
  • 啓動 ${CATALINA_HOME}/bin/startup.sh
  • 關閉 ${CATALINA_HOME}/bin/shutdown.sh

maven安裝

  1. 下載
wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
  1. 解壓縮
  2. 配置環境變量
  • MAVEN_HOME=安裝目錄
  • 將bin目錄加到PATH中
  1. 驗證
mvn -version
  1. 經常使用命令
  • 清除: mvn clean
  • 編譯: mvn compile
  • 打包: mvn package
  • 跳過單元測試: mvn clean package -Daven.test.skip=true

ftp服務搭建(vsftpd)

  1. 安裝
yum -y install vsftpd
  1. 配置文件位於/etc/vsftpd/vsftpd.conf
  2. 必須將/sbin/nologin 加到/etc/shells中 (這個卡了好久,請必定注意,不然nologin的用戶連不上)
  3. 建立虛擬用戶
  • 建立一個目錄做爲ftp目錄, 如/ftpfile
  • 添加匿名用戶 useradd -M -s /sbin/nologin ftpuser -M表示不要家目錄
  • 修改權限: chown -R ftpuser.ftpuser /ftpfile
  • 重設ftpuser密碼 passwd ftpuser
  1. 配置
cd /etc/vsftpd
vim chroot_list

將新增的ftpuser加到配置文件中sql

  1. vim /etc/selinux/config 修改SELINUX=disable

若是遇到550則執行 setsebool -P ftp_home_dir 1shell

  1. vsftpd.conf配置

配置說明參考數據庫

http://learning.happymmall.co...apache

注意幾個點:

  • local_root
  • local_enable
  • anonymous_enable
  • pasv_min_port
  • pasv_max_port
  • 出現vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()錯誤的解決方法

加一行這個配置就能夠

allow_writeable_chroot=YES
  1. 防火牆配置
# 這是centos7, 6是不同的
啓動: systemctl start firewalld
關閉: systemctl stop firewalld
查看狀態: systemctl status firewalld 
開機禁用  : systemctl disable firewalld
開機啓用  : systemctl enable firewalld

開放端口 firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,沒有此參數重啓後失效)
關閉端口 firewall-cmd --zone=public --remove-port=80/tcp --permanent
查看開放的端口 firewall-cmd --zone=public --list-ports
更新防火牆規則: firewall-cmd --reload
  1. 驗證
  • 啓動

·

service vsftpd start
  • 嘗試用客戶端去連
  1. 經常使用命令
  • 啓動: service vsftpd start
  • 中止: service vsftpd restart
  • 關閉: service vsftpd stop
#設置開機啓動
systemctl enable vsftpd.service

#啓動
systemctl start vsftpd.service

#中止
systemctl stop vsftpd.service

#查看狀態
systemctl status vsftpd.service

#重啓
systemctl restart vsftpd.service

nginx

  1. 安裝gcc
yum install -y gcc
gcc -v
  1. 安裝pcre
yum install -y pcre-devel
  1. 安裝zlib
yum install -y zlib zlib-devel
  1. 安裝openssl(支持ssl)
yum install -y openssl openssl-devel
  1. 以上能夠一個命令安裝
yum install -y gcc pcre-devel zlib zlib-devel openssl openssl-devel
  1. 下載nginx

歷史版本http://nginx.org/download/

wget http://nginx.org/download/nginx-1.13.6.tar.gz
  1. 解壓
  2. 進入解壓後的目錄執行
./configure
# 加--prefix=/usr/nginx能夠指定安裝目錄
whereis nginx能夠查詢安裝目錄, 默認在/usr/local/
  1. 執行make 而後make install
  2. 經常使用命令
  • 測試配置文件正確性
/nginx/sbin/nginx -t
  • 啓動
/nginx/sbin/nginx
  • 中止
/nginx/sbin/nginx -s stop
或
nginx -s quit
  • 重啓
/nginx -s reload
  • 進程查看
ps -ef|grep nginx
  • 平滑重啓
kill -HUP 進程號
  1. 配置
  • 分解配置文件

在/usr/local/nginx/conf/nginx.conf增長 include vhost/*.conf 這樣方便每一個域名配搞個配置文件

指向端口

server {
    default_type 'text/html';
    charset utf-8;
    listen 80;
    # 開索引
    autoindex on; 
    server_name learning.happymmall.com;
    access_log /usr/local/nginx/logs/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }

    # 注意這個
    location / {
        proxy_pass   http://127.0.0.1:81/aa;
        # 真實環境別這麼寫
        add_header Access-Control-Allow-Origin *;
    }
}

指向目錄

server {
    listen 80;
    # 注意這裏禁止索引了
    autoindex off;
    server_name img.happymmall.com;
    access_log /usr/local/nginx/logs/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }

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

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

mysql

  1. 安裝
yum install -y mysql-server

(centos7下這樣不行, 軟件源裏邊沒有這個,須要下載源再裝,以下)

參考文章https://www.cnblogs.com/codin...

  1. 配置文件位置/etc/my.cnf
  2. 配置mysql自啓動

用上面配置vsftpd自啓動的方法也能夠

chkconfig mysqld on
# 2-5須要時on
chkconfig --list mysqld
  1. 防火牆開放3306
  2. 啓動驗證
啓動:systemctl start mysqld.service
中止:systemctl stop mysqld.service
重啓:systemctl restart mysqld.service
查看服務狀態:systemctl status mysqld.service
  • 出現Job for mysqld.service failed because the control process exited with error code問題

刪除/var/lib/mysql 再啓動

  • 登陸(能登陸進去就證實能夠了)
mysql -u root
  • 登陸出錯

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

修改/etc/my.cnf 在[mysqld]下面加一行skip-grant-tables
登陸後改root密碼, 而後去掉這一行

改密碼的方法

alter user'root'@'localhost' IDENTIFIED BY '你的密碼';
  1. mysql用戶和權限相關配置

https://www.cnblogs.com/gycho...

建立用戶

create user 'test1'@'localhost' identified by '‘密碼';

flush privileges;刷新權限

 

其中localhost指本地纔可鏈接

能夠將其換成%指任意ip都能鏈接

也能夠指定ip鏈接


修改密碼

Alter user 'test1'@'localhost' identified by '新密碼';

flush privileges;

 

受權

grant all privileges on *.* to 'test1'@'localhost' with grant option;

 

with gran option表示該用戶可給其它用戶賦予權限,但不可能超過該用戶已有的權限

好比a用戶有select,insert權限,也可給其它用戶賦權,但它不可能給其它用戶賦delete權限,除了select,insert之外的都不能

這句話可加可不加,視狀況而定。

 

all privileges 可換成select,update,insert,delete,drop,create等操做

如:grant select,insert,update,delete on *.* to 'test1'@'localhost';

 

第一個*表示通配數據庫,可指定新建用戶只可操做的數據庫

如:grant all privileges on 數據庫.* to 'test1'@'localhost';

 

第二個*表示通配表,可指定新建用戶只可操做的數據庫下的某個表

如:grant all privileges on 數據庫.指定表名 to 'test1'@'localhost';


查看用戶受權信息

show grants for 'test1'@'localhost';

 
撤銷權限

revoke all privileges on *.* from 'test1'@'localhost';

用戶有什麼權限就撤什麼權限

刪除用戶

drop user 'test1'@'localhost';

參考資料

(持續更新...)

相關文章
相關標籤/搜索