一、介紹
很久沒有寫過了,這回重新的把服務器重裝系統了,本次不安裝lamp環境包,不配做tomcat,也不用寶塔管理面板,就是簡簡單單的使用iptables防火牆和apache的httpd安裝一個服務器環境,其實現在大部分都是使用它來做內部端口映射,我本人不是專做運維的,只懂一點點。感覺各種http服務器折騰下來就這個直接了當。本次也不安裝mysql,裝在一個主機上沒有意義,感覺。而且我的vps太low了。
二、iptables情況
1.更新
不管怎麼樣先更新一下再說。
yum update
2.檢查是否已經安裝iptables
service iptables status
3.如果沒有就安裝
yum install -y iptables
yum install iptables-services
# 如果需要安裝就需要禁用原來的,我本次安裝版本自帶
# 停止firewalld服務
systemctl stop firewalld
# 禁用firewalld服務
systemctl mask firewalld
4.配置iptables
#查看iptables現有規則
iptables -L -n
#先允許所有,不然有可能會杯具
iptables -P INPUT ACCEPT
#清空所有默認規則
iptables -F
#清空所有自定義規則
iptables -X
#所有計數器歸0
iptables -Z
#允許來自於lo接口的數據包(本地訪問)
iptables -A INPUT -i lo -j ACCEPT
#開放22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#開放21端口(FTP)
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#開放80端口(HTTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#開放443端口(HTTPS)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#允許ping
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
#允許接受本機請求之後的返回數據 RELATED,是爲FTP設置的
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#其他入站一律丟棄
iptables -P INPUT DROP
#所有出站一律綠燈
iptables -P OUTPUT ACCEPT
#所有轉發一律丟棄
iptables -P FORWARD DROP
#如果要添加內網ip信任(接受其所有TCP請求)
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
#過濾所有非以上規則的請求
iptables -P INPUT DROP
#要封停一個IP,使用下面這條命令:
iptables -I INPUT -s ***.***.***.*** -j DROP
#要解封一個IP,使用下面這條命令:
iptables -D INPUT -s ***.***.***.*** -j DROP
#保存上述規則
service iptables save
#註冊iptables服務
#相當於以前的chkconfig iptables on
systemctl enable iptables.service
#開啓服務
systemctl start iptables.service
#查看狀態
systemctl status iptables.service
簡易腳本
#!/bin/sh
iptables -P INPUT ACCEPT
iptables -F
iptables -X
iptables -Z
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
service iptables save
systemctl restart iptables.service
三、安裝httpd
1.安裝
yum install -y httpd
yum install -y httpd-devel
# 啓動和設置開機啓動
systemctl start httpd
systemctl enable httpd
# 查看狀態
systemctl status httpd
這個時候在你的瀏覽器上輸入你的服務器IP地址就能看到這個了
四、MySQL安裝MySQL
添加mysql源
1 |
|
安裝mysql
1 |
|
啓動mysql並設置爲開機自啓動服務
1 2 |
|
檢查mysql服務狀態
1 |
|
第一次啓動mysql,會在日誌文件中生成root用戶的一個隨機密碼,使用下面命令查看該密碼
1 |
|
修改root用戶密碼
1 2 3 4 |
|
查看當前已有用戶
select host,user,authentication_string from mysql.user;
新建用戶
格式:create user "username"@"host" identified by "password";
create user 'test'@'localhost' identified by '123';
create user 'test'@'192.168.7.22' identified by '123';
create user 'test'@'%' identified by '123';
/*host="localhost"爲本地登錄用戶,host="ip"爲ip地址登錄,host="%",爲外網ip登錄*/
刪除用戶
格式:drop user 'username'@'host';
授權給用戶
格式:grant privileges on databasename.tablename to 'username'@'host' IDENTIFIED BY 'PASSWORD';
1. GRANT命令說明:
priveleges(權限列表),可以是all priveleges, 表示所有權限,也可以是select、update等權限,多個權限的名詞,相互之間用逗號分開。
on用來指定權限針對哪些庫和表。
*.* 中前面的*號用來指定數據庫名,後面的*號用來指定表名。
to 表示將權限賦予某個用戶, 如 jack@'localhost' 表示jack用戶,@後面接限制的主機,可以是IP、IP段、域名以及%,%表示任何地方。注意:這裏%有的版本不包括本地,以前碰到過給某個用戶設置了%允許任何地方登錄,但是 在本地登錄不了,這個和版本有關係,遇到這個問題再加一個localhost的用戶就可以了。
identified by指定用戶的登錄密碼,該項可以省略。
WITH GRANT OPTION 這個選項表示該用戶可以將自己擁有的權限授權給別人。注意:經常有人在創建操作用戶的時候不指定WITH GRANT OPTION選項導致後來該用戶不能使用GRANT命令創建用戶或者給其它用戶授權。
備註:可以使用GRANT重複給用戶添加權限,權限疊加,比如你先給用戶添加一個select權限,然後又給用戶添加一個insert權限,那麼該用戶就同時擁有了select和insert權限。
2.授權原則說明:
權限控制主要是出於安全因素,因此需要遵循一下幾個經驗原則:
a、只授予能滿足需要的最小權限,防止用戶幹壞事。比如用戶只是需要查詢,那就只給select權限就可以了,不要給用戶賦予update、insert或者delete權限。
b、創建用戶的時候限制用戶的登錄主機,一般是限制成指定IP或者內網IP段。
c、初始化數據庫的時候刪除沒有密碼的用戶。安裝完數據庫的時候會自動創建一些用戶,這些用戶默認沒有密碼。
d、爲每個用戶設置滿足密碼複雜度的密碼。
e、定期清理不需要的用戶。回收權限或者刪除用戶。
/*授予用戶通過外網IP對於該數據庫的全部權限*/
grant all privileges on `test`.* to 'test'@'%' ;
/*授予用戶在本地服務器對該數據庫的全部權限*/
grant all privileges on `test`.* to 'test'@'localhost';
grant select on test.* to 'user1'@'localhost'; /*給予查詢權限*/
grant insert on test.* to 'user1'@'localhost'; /*添加插入權限*/
grant delete on test.* to 'user1'@'localhost'; /*添加刪除權限*/
grant update on test.* to 'user1'@'localhost'; /*添加權限*/
flush privileges; /*刷新權限*/
查看當前已有權限
show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
rows in set (0.00 sec)
其它
(7)刪除權限
revoke privileges on databasename.tablename from 'username'@'host';
revoke delete on test.* from 'jack'@'localhost';
(8)更改用戶名
mysql> rename user 'jack'@'%' to 'jim'@'%';
(9)修改密碼
1.用set password命令
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
Query OK, 0 rows affected (0.00 sec)
2.用mysqladmin [root@rhel5 ~]# mysqladmin -uroot -p123456 password 1234abcd
備註: 格式:mysqladmin -u用戶名 -p舊密碼 password 新密碼
3.用update直接編輯user表
(10)pycharm中python3.6+pymysql+mysql8.0.1連接報錯
pymysql.err.OperationalError: (1045, u"Access denied for user 'root'@'localhost' (using password: No)")
解決方法:
在cmd命令行連接mysql, 通過mysql -u root -p dong1990
然後輸入ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'dong1990';
數據庫操作
一、創建數據庫(默認字符集和排序規則)
(1)創建數據庫
CREATE DATABASE my_db1;
Query OK, 1 row affected (0.00 sec)
(2)因爲my_db1已經存在,再次創建會報錯
CREATE DATABASE my_db1;
ERROR 1007 (HY000): Can't create database 'my_db1'; database exists
(4)加上IF NOT EXISTS就算數據庫已經存在,把原來的覆蓋掉了
CREATE DATABASE IF NOT EXISTS my_db1;
Query OK, 1 row affected, 1 warning (0.00 sec)
二、創建數據庫包含字符集和排序規則
(1)查看mysql字符集
[sql] view plain copy
- mysql> SHOW CHARACTER SET;
- +----------+-----------------------------+---------------------+--------+
- | Charset | Description | Default collation | Maxlen |
- +----------+-----------------------------+---------------------+--------+
- | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
- | dec8 | DEC West European | dec8_swedish_ci | 1 |
- | cp850 | DOS West European | cp850_general_ci | 1 |
- | hp8 | HP West European | hp8_english_ci | 1 |
- | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
- | latin1 | cp1252 West European | latin1_swedish_ci | 1 |
- .......
(2)查看mysql排序規則
[sql] view plain copy
- mysql> SHOW COLLATION;
- +--------------------------+----------+-----+---------+----------+---------+
- | Collation | Charset | Id | Default | Compiled | Sortlen |
- +--------------------------+----------+-----+---------+----------+---------+
- | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 |
- | big5_bin | big5 | 84 | | Yes | 1 |
- | dec8_swedish_ci | dec8 | 3 | Yes | Yes | 1 |
- | dec8_bin | dec8 | 69 | | Yes | 1 |
- | cp850_general_ci | cp850 | 4 | Yes | Yes | 1 |
- | cp850_bin | cp850 | 80 | | Yes | 1 |
- | hp8_english_ci | hp8 | 6 | Yes | Yes | 1 |
- .................
(3)創建數據庫包括字符集和排序規則
[sql] view plain copy
- mysql> CREATE DATABASE IF NOT EXISTS hdu CHARACTER SET 'gbk' COLLATE 'gbk_chinese_ci';
- Query OK, 1 row affected (0.00 sec)
三、刪除數據庫
[sql] view plain copy
-
- mysql> DROP DATABASE my_db1;
- Query OK, 0 rows affected (0.00 sec)
五、安裝https證書
域名是得要有的
1.安裝相關軟件
# 擴展庫
yum install epel-release
# 更新
yum update
# 安裝
yum install httpd mod_ssl python-certbot-apache
添加虛擬主機 Apache vhost
我使用的域名是 deepppixel.com,域名要添加解析哦。
我創建的是基於域名的虛擬主機,就是多個域名指向同一個服務器地址。
還有兩種方式是:基於 IP 地址 和基於端口。
先創建保存虛擬主機配置的文件夾
// 虛擬主機的配置文件
[root@~]# mkdir -p /etc/httpd/conf/vhost
然後創建域名網站的內容文件夾
// 使用域名方便以後添加新的域名,不會產生衝突,不要隨意命名,你會後悔的
[root@~]# mkdir -p /var/www/deepppixel.com
添加域名的 log 日誌文件
[root@~]# touch /etc/httpd/logs/deepppixel.com-error_log
[root@~]# touch /etc/httpd/logs/deepppixel.com-access_log
把上面創建的虛擬主機路徑添加到 Apache 的配置文件中
[root@~]# vim /etc/httpd/conf/httpd.conf
......
# vhost
Include conf/vhost/*.conf
開始添加虛擬主機文件
[root@~]# vim /etc/httpd/conf/vhost/deepppixel.com.conf
<VirtualHost 私網 IP:80>
ServerName deepppixel.com
ServerAlias deepppixel.com *.deepppixel.com
DocumentRoot "/www/deepppixel.com"
ErrorLog "/logs/deepppixel.com-error_log"
CustomLog "/logs/deepppixel.com-access_log"
<Directory "/www/deepppixel.com">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
檢查 httpd.conf 是否有錯
[root@~]# httpd -t
// 沒有找到文件夾
AH00112: Warning: DocumentRoot [/www/deepppixel.com] does not exist
// CustomLog 後面需要跟 兩個或三個參數
AH00526: Syntax error on line 7 of /etc/httpd/conf/vhost/deepppixel.com.conf:
CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional "env=" or "expr=" clause (see docs)
出現了兩個錯誤,修改如下
<VirtualHost 私網 IP:80>
ServerName deepppixel.com
ServerAlias deepppixel.com *.deepppixel.com
DocumentRoot "/var/www/deepppixel.com"
ErrorLog "logs/deepppixel.com-error_log"
CustomLog "logs/deepppixel.com-access_log" "%h %l %u %t \"%r\" %>s %b"
<Directory "/www/deepppixel.com">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
重新檢查即可
[root@~]# httpd -t
Syntax OK
重啓 Apache 服務器
[root@~]# systemctl restart httpd.service
首先使用curl下載get-pip文件
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
使用Python來運行
python get-pip.py
安裝完畢
方案一、更改庫的默認字符集
創建庫的時候指定默認字符集:
create database 庫名 default charset=utf8;
1
或者修改現有庫的字符集:
alter database 庫名 character set utf8;
1
方案二、更改表的默認字符集,
創建表的時候指定默認字符集
create table 表名 (...) default charset=utf8;
1
或者修改現有表的字符集
alter table 表名 character set utf8;
1
方案三、修改配置文件(新創建的庫和表會自動設置中文字符集)
修改配置文件詳細步驟: (注意:爲了防止把配置文件改錯,修改之前先將其備份)
# 獲取用戶權限:
sudo -i
# 進入到mysql配置文件所在路徑:
cd /etc/mysql/mysql.conf.d/
1
2
3
4
# 備份(-p選項會把原文件的權限也一起復制)
cp -p mysql.cnf mysql.cnf.bak #用vi打開配置文件mysqld.cnf並進行修改:
1
2
輸入vi mysqld.cnf
找到 [mysqld],在 tmpdir =/tmp 後面按o鍵換行插入 character_set_server=utf8(如圖所示):
按esc鍵 退出插入模式,按shift + : 進入命令行,輸入wq保存並退出
輸入 /etc/init.d/mysql restart 重啓mysql服務
輸入exit 退出超級用戶模式