产品名:应用中心
产品想法:在手机上我们都有应用商店,应用商店收录了各种各样的app,当我们需要地图导航时只需要安装地图软件即可使用。在我们的开发与生产环境中,linux平台下安装软件需要有经验加持,才能避免各种坑。开发一款基于centos7的应用中心,在日常开发和生产环境中可以快捷方便的安装所需软件,并自动配置让软件相对运行到最优。
工具选择:ansible、nginx、python3、maraidb
- ansible:使用jinja2的模板,使用ansible调度功能
- nginx:做代理并提供高并发访问
- python3:高扩展性可以将功能模块化
- maraidb:使用centos7自带的mysql数据库
准备工作:
python3 编译安装
# 编译安装python3.6.4 # yum install libffi-devel -y cd /usr/bin ll python* mkdir /usr/local/python3 cd /usr/local/python3 wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz tar -zxf Python-3.7.4.tgz yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make cd Python-3.7.4 ./configure --prefix=/usr/local/python3 make && make install cd /usr/bin # 设置软连接 ln -s /usr/local/python3/bin/python3 /usr/bin/python3 # 查看安装的python版本,和之前的python2版本 cd python -V python3 -V # pip 安装 python3 -m pip install --upgrade pip python3 -m pip install django python3 -m pip install pyMySQL vim /usr/local/python3/lib/python3.7/site-packages/django/db/backends/mysql/base.py # 设置软连接 ln -s /usr/local/python3/bin/django-admin /usr/bin/django-admin
sqlit3编译安装
# 下载sqlit3 包 cd /usr/local/ wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz tar -zxvf sqlite-autoconf-3290000.tar.gz # 编译安装 cd sqlite-autoconf-3290000 ./configure --prefix=/usr/local make && make install mv /usr/local/bin/sqlite3 /usr/local/ rm -rf /usr/local/bin # 重新设置软连接 mv /usr/bin/sqlite3 /usr/bin/sqlite3_old ln -s /usr/local/sqlite3 /usr/bin/sqlite3 # 设置环境变量 export LD_LIBRARY_PATH="/usr/local/lib" sed -i "/LD_LIBRARY_PATH/d" /root/.bash_profile echo "export LD_LIBRARY_PATH=\"/usr/local/lib\"" >> /root/.bash_profile source /root/.bash_profile sqlite3 --version
nginx编译安装
# 安装编译环境 yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel # 下载nginx 包 mkdir /usr/local/nginx cd /usr/local/nginx wget -c https://nginx.org/download/nginx-1.16.0.tar.gz tar -zxvf nginx-1.16.0.tar.gz cd nginx-1.16.0 sed -i -e 's/1.16.0//' -e 's/nginx\//cgls/g' -e 's/"Nginx"/"cgls"/g' src/core/nginx.h # 编译安装 ./configure ./configure --prefix=/usr/local/nginx --with-http_ssl_module make && make install # 设置软连接 cd /usr/bin ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx # nginx设置为服务 cat << EOF > /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target EOF # 设置开机启动 systemctl enable nginx systemctl start nginx
ansible 安装
# 安装ansible yum -y install ansible
mariadb 安装
# 安装mariadb
yum -y install mariadb mariadb-server expect
systemctl enable mariadb
systemctl start mariadb
/usr/bin/expect <<EOF
spawn mysql_secure_installation
expect "Enter current password for root (enter for none):" {
send "\r";exp_continue
} "Set root password?" {
send "y\r";exp_continue
} "New password:" {
send "123qwe\r";exp_continue
} "Re-enter new password:" {
send "123qwe\r";exp_continue
} "Remove anonymous users?" {
send "y\r";exp_continue
} "Disallow root login remotely?" {
send "n\r";exp_continue
} "Remove test database and access to it?" {
send "y\r";exp_continue
} "Reload privilege tables now?" {
send "y\r";exp_continue
}
EOF
mysql -uroot -p123qwe -e "GRANT ALL PRIVILEGES ON root.* TO 'root'@'%' IDENTIFIED BY '123qwe';"
创建django项目
# 创建appcenter项目 django-admin startproject appcenter cd appcenter python3 manage.py startapp rpm # 创建库 mysql -uroot -p123qwe -e "CREATE DATABASE appcenter CHARACTER SET utf8;" # 更新my.cnf cat << EOF > /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 default-storage-engine = innodb innodb_file_per_table = on max_connections = 4096 collation-server = utf8_general_ci character-set-server = utf8 [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid [client] database = appcenter user = root password = 123qwe1 default-character-set = utf8 !includedir /etc/my.cnf.d EOF systemctl restart mariadb # 解决mysql在django中使用的bug sed -i "s/if version/#if version/g" /usr/local/python3/lib/python3.7/site-packages/django/db/backends/mysql/base.py sed -i "s/ raise/# raise/g" /usr/local/python3/lib/python3.7/site-packages/django/db/backends/mysql/base.py sed -i "s/query.decode/query.encode/g" /usr/local/python3/lib/python3.7/site-packages/django/db/backends/mysql/operations.py # pymysql替换MySQLdb sed -e "import pymysql\npymysql.install_as_MySQLdb()" >> appcenter/__init__.py # 创建超级用户 python3 manage.py createsuperuser # 启动服务 firewall-cmd --permanent --zone=public --add-port=8000/tcp firewall-cmd --reload python3 manage.py runserver 0.0.0.0:8000
nginx 发布django项目
# 安装uwsgi
python3 -m pip install uwsgi --upgrade
# 设置软连接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
# uwsgi启动
uwsgi --http :8000 --chdir /root/appcenter/ --module appcenter.wsgi
# 配置uwsgi文件启动项目
mkdir /root/appcenter/script
cat << EOF > /root/appcenter/uwsgi.ini
# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/root/appcenter/
# 指定项目的application
module=appcenter.wsgi
# 指定sock的文件路径
socket=/root/appcenter/script/uwsgi.sock
# 进程个数
workers=5
pidfile=/root/appcenter/script/uwsgi.pid
# 指定IP端口
http=:8000
# 指定静态文件
#static-map=/static=/root/appcenter/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/root/appcenter/script/uwsgi.log
EOF
# 执行命令,启动项目
uwsgi --ini /root/appcenter/uwsgi.ini
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload
# 配置nginx使用代理
vim /usr/local/nginx/conf/nginx.conf
upstream djangoserver {
server 172.16.2.243:8000;
}
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location / {
proxy_pass http://djangoserver;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
systemctl stop nginx
systemctl start nginx效果图

本文暂时没有评论,来添加一个吧(●'◡'●)