1. 程式人生 > >Linux之經典網際網路架構

Linux之經典網際網路架構


經典網際網路架構

netstat -tulnp |grep 80
ss -tulnp|grep 80


前期鋪墊:

1. Linux要能上網
2. 掌握Linux軟體包安裝方法
2.1 rpm包管理
2.1.1 光碟掛載
mount /dev/cdrom /mnt
cd /mnt
ls
2.1.2 安裝rpm包
rpm -ivh vsftpd-3.0.2-22.el7.x86_64.rpm
rpm -Uvh vsftpd-3.0.2-22.el7.x86_64.rpm
2.13 解除安裝rpm
rpm -e vsftpd-3.0.2-22.el7.x86_64

2.1.4 rpm包的查詢
[[email protected]

~]# rpm -q vsftpd
vsftpd-3.0.2-22.el7.x86_64
[[email protected] ~]# rpm -q vsftp
package vsftp is not installed
[[email protected] ~]#
[[email protected] ~]# rpm -qa |grep vsf
[[email protected] ~]# which vim
/usr/bin/vim
[[email protected] ~]# rpm -qf /usr/bin/vim

**2.1.5 額外補充

A ----> B ---> C

A BC B DF F AC

rpm -ivh a b c d e f


2.2 yum 使用
2.2.1 使用aliyun yum站點源
Base源
cd /etc/yum.repos.d/
mv *.repo /tmp
wget http://mirrors.aliyun.com/repo/Centos-7.repo

EPEL源
yum install -y epel-release

2.2.2 使用yum安裝軟體
yum install -y openssl openssl-devel

2.2.3 軟體包查詢及組安裝
yum list |grep vsftpd
yum grouplist
yum groupinstall "Development Tools"

2.2.4 解除安裝軟體包
yum remove

 

2.2.5 優化yum源
(1)本地映象yum源(光碟掛載到/mnt下)
[local]
name=localios
baseurl=file:///mnt
gpgcheck=0

yum clean all
yum install -y vsftpd

(2)區域網yum源(ftp)
1.安裝ftp軟體
yum install -y vsftpd
2.啟動ftp服務
[[email protected] ~]# systemctl start vsftpd
[[email protected] ~]# systemctl enable vsftpd

3.建立站點目錄,並將光碟軟體拷貝其中
[[email protected] ~]# cp -a /mnt/* /var/ftp/pub/centos7

4.生成ftp站點的yum源配置檔案
[ftp]
name=centos7
baseurl=ftp://10.0.0.100/pub/centos7
gpgcheck=0


5.安裝軟體測試
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ openssl-devel zlib zlib-devel -y


2.3 原始碼包應用

2.3.1 解壓
.zip
unzip xxx.zip
.tar
.tar.gz
.tgz
.tar.bz2
.tar.xz
tar xf xxx.tar
.iso
mount -o loop xxx.iso /test
.cpio
cpio -idcmv < xxx.cpio

2.3.2 原始碼包安裝
(1)python3.6原始碼包安裝
./configure ------> 定製功能
make
make install

mysql 原始碼包
cmake
make
make install

(2) redis 原始碼包安裝
make

修改環境變數
vim /etc/profile

新增以下一行:
export PATH=/opt/redis-3.2.10/src:$PATH

生效配置
source /etc/profile


(3) 讓python連結redis

unzip redis-py-master.zip
cd redis-py-master
python3 setup.py install


對redis的單例項進行連線操作
python3
>>>import redis
>>>r = redis.StrictRedis(host='localhost', port=6379)
>>>r.set('name', 'oldguo')
True
>>>r.get('name')
'oldguo'


3. 二進位制包(mysql5.7.20)

3.1 解壓及製作軟連線
tar xf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
ln -s mysql-5.7.20-linux-glibc2.12-x86_64 mysql

3.2 編輯環境變數
vim /etc/profile
新增以下一行:
export PATH=/opt/mysql/bin:$PATH

生效配置:
source /etc/profile

3.3 解除安裝自帶mariadb
yum remove mariadb-libs

3.4 生成配置檔案(/etc/my.cnf)
vim /etc/my.cnf
[mysqld]
basedir=/opt/mysql
datadir=/opt/mysql/data
socket=/tmp/mysql.sock
user=mysql
log_error=/var/log/mysql.log
log_bin=/opt/mysql/data/mysql-bin
server_id=100
[mysql]
socket=/tmp/mysql.sock

3.5 建立使用者和資料目錄,並授權
useradd mysql
mkdir /opt/mysql/data
chown -R mysql.mysql /opt/mysql


3.6 初始化資料

[[email protected] ~]# mysqld --initialize-insecure --basedir=/opt/mysql --datadir=/opt/mysql/data --user=mysql
2019-01-04T03:58:03.128958Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-01-04T03:58:03.142436Z 0 [ERROR] Could not open file '/var/log/mysql.log' for error logging: Permission denied
2019-01-04T03:58:03.142492Z 0 [ERROR] Aborting

報錯解決:
touch /var/log/mysql.log
chown -R mysql.mysql /var/log/mysql.log


再次初始化:
mysqld --initialize-insecure --basedir=/opt/mysql --datadir=/opt/mysql/data --user=mysql


3.7 啟動mysql
cd /opt/mysql/support-files
./mysql.server start

cp mysql.server /etc/init.d/mysqld

/etc/init.d/mysqld restart


擴充套件:使用systemctl 管理mysql
vim /etc/systemd/system/mysqld.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000


systemctl restart mysqld

netstat -tulnp |grep 3306


3.8 測試python3連結mysql

建立mysql連結使用者

grant all on *.* to [email protected]'10.0.0.%' identified by '123';
create database bbs charset utf8;

python程式碼測試:

pip3 install --upgrade pip
pip3 install pymysql

vim testmysql.py
#!/usr/bin/python3
import pymysql
db = pymysql.connect("10.0.0.100","root","123","bbs" )
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print ("Database version : %s " % data)
db.close()

python3 testmysql.py

 

4.經典網際網路架構專案(LNMP)

LNMPT = Linux Nginx MySQL PHP
LNMPJ = Linux Nginx MySQL PHP


LNMP架構環境部署
1) 使用官方Nginx yum源

[[email protected] ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

#安裝Nginx
[[email protected] ~]# yum install nginx -y
2) 啟動Nginx,並將Nginx加入開機自啟

systemctl start nginx
systemctl enable nginx
3) 使用第三方擴充套件源安裝php7.1

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

[[email protected] ~]# yum remove php-mysql-5.4 php php-fpm php-common
[[email protected] ~]# vim /etc/yum.repos.d/php.repo
[php]
name = php Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

[[email protected] ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
3) 配置php-fpm使用者與Nginx的執行使用者保持一致

[[email protected] ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[[email protected] ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
4) 啟動php-fpm,並將其加入開機自啟

[[email protected] ~]# systemctl start php-fpm
[[email protected] ~]# systemctl enable php-fpm
5) 安裝mysql資料庫

[[email protected] ~]# mysqladmin password '123'
[[email protected] ~]# mysql -uroot -p123
2.LNMP架構環境配置


vim /etc/nginx/conf.d/php.conf
server {
server_name 10.0.0.12;
listen 80;
root /code/wordpress;
index index.php index.html;

location ~ \.php$ {
root /code/bbs;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

 

 

 

 


LNMuWSGI+redis