論壇搭建全記錄

搭了個(gè)論壇玩。 以下文字時(shí)寫給自己看的,首要目的是記錄,其次才是得瑟。會有些啰嗦,因?yàn)椴幌雽懩欠N從頭到位都很順利跟說明書似的帖子。

創(chuàng)新互聯(lián)建站專注于龍里網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供龍里營銷型網(wǎng)站建設(shè),龍里網(wǎng)站制作、龍里網(wǎng)頁設(shè)計(jì)、龍里網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造龍里網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供龍里網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。概況

基本思路是LNMP:

L: CentiOS7 為什么用7,沒什么特別的原因,喜歡7這個(gè)數(shù)字,CentOS7剛出來的時(shí)候就裝了個(gè),只是當(dāng)時(shí)電腦配置差,玩得不爽,耿耿于懷,這次順便了了這個(gè)結(jié),當(dāng)然坑也會有的。為什么不用ubuntu,就ubuntu默認(rèn)防火墻關(guān)閉,CentOS默認(rèn)防火墻打開這一點(diǎn)就讓我不打算用ubuntu了。

N: nginx 沒認(rèn)真筆記多nginx喝apache,只知道大家現(xiàn)在更偏向于用nginx跑python項(xiàng)目,nginx也有虛擬主機(jī),反向代理的玩法,可以很方便的在一臺服務(wù)器上搭建多個(gè)網(wǎng)站。

M: MySQL 被oracle搞去了,因?yàn)閾?dān)心被閉源,所以CentOS7用了由社區(qū)維護(hù)的MySQL的一個(gè)分支,MariaDB,但起起來

P: python 用的django框架,論壇直接用的開源軟件misago

搭建 CentOS7

系統(tǒng)安裝直接用的ECS,創(chuàng)建虛擬機(jī)的時(shí)候選的CentOS7的鏡像。 我覺得用root用戶直接搭建環(huán)境不好,于是創(chuàng)建了一個(gè)管理員賬號,以我自己命名,命令: adduser henry -c "Henry Chen"用戶henry在用sudo的時(shí)候不需要輸入密碼,命令:visudo,會打開一個(gè)文件,找到這兩行:

## Same thing without a password %wheel ALL=(ALL) NOPASSWD: ALL

去掉第二行前面的注釋(這里已經(jīng)去掉了),記下wheel這個(gè)組,只要在這個(gè)組里面的用戶,使用sudo都不用輸入密碼。 將henry加入到這個(gè)組中:usermod -a -G henry wheel。 安裝必要的軟件:

安裝必需的依賴包sudo yum install libjpeg-turbo-devel zlib-devel python-devel libfreehand-devel python-imgcreate

防火墻
這個(gè)鏡像用起來跟本地不一樣,本地安裝的CentOS7種,防火墻會阻止幾乎全部端口,而ECS中只要你用到了哪個(gè)端口,它會幫你自動打開這個(gè)端口,不用自己操作防火墻。本地做實(shí)驗(yàn)的時(shí)候需要操作防火墻,需要以下命令:

firewall-cmd —list-port firewall-cmd --zone=dmz --add-port=80/tcp firewall-cmd --zone=dmz --add-port=3306/tcp

若要永久生效方法在后面加上--permanent

這是我遇到的第一個(gè)坑,裝上mysql,添加好用戶和權(quán)限后卻不能登錄,google出來的方向都是讓我去改iptables,但CentOS7中將iptables換成了firewall,有網(wǎng)友建議卸載firewall,安裝iptables,但我覺得人家用了firewall總是有他的道理的,于是決定用firewall。

MySQL

安裝
因?yàn)橛X得數(shù)據(jù)庫能用就行,沒必要源碼編譯,于是想直接用源里面的,但CentOS7的源中并沒有自帶mysql,而是MariaDB,這是由社區(qū)維護(hù)的一個(gè)MySQL的分支,但據(jù)說啟動后查看進(jìn)程看到的是mariadb不是mysql,有點(diǎn)怪怪的,于是我去官方找了個(gè)源,然后安裝。

wget

在安裝完成后會看到replaced mariadb的信息,這個(gè)有意思。

初次安裝mysql,root用戶沒有密碼,為了安全起見,最好設(shè)置下密碼

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

新建用戶&權(quán)限管理

create user \'username\'@\'%\' identified by \'password\'; create user \'username\'@\'localhost\' identified by \'password\'; grant all privileges on database.* to username@\'%\'identified by \'password\'; grant all privileges on database.* to username@\'%\'identified by \'password\';

修改編碼配置
在/etc/my.cnf中加上如下配置:

[mysql] default-character-set =utf8

這里的字符編碼必須和/usr/share/mysql/charsets/Index.xml中一致。
創(chuàng)建數(shù)據(jù)庫的時(shí)候也需要加上字符編碼的選項(xiàng): create database xxx default character set utf8;
參考: centos7 mysql數(shù)據(jù)庫安裝和配置

虛擬環(huán)境 安裝步驟: 安裝virtualenv和virtualenvwrapper(源碼或者pip) mkdir $HOME/.virtualenvs 在~/.bashrc 中添加: export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh source ~/.bashrc 測試:mkvirtualenv bookbbs,沒問題就ok 參考:Python 虛擬環(huán)境:Virtualenv 此時(shí)就可先在虛擬環(huán)境中安裝好所需的依賴,然后把項(xiàng)目用sudo python manage.py runserver 0.0.0.0:80的形式跑起來了,我在做到這一步的時(shí)候是先把項(xiàng)目跑起來的,因?yàn)闆]在CentOS7上玩過,怕跑不起來,所以想先在這一步看看跑起來的效果如何。 uwsgi

因?yàn)槭撬许?xiàng)目都要用的,所以不該放在某個(gè)虛擬環(huán)境中,應(yīng)該在虛擬環(huán)境外安裝,如果不是root則需要sudo的權(quán)限。然后需要找一個(gè)uwsgi的配置文件,以及啟動腳本,可以不用啟動腳本,自己將啟動命令加到開機(jī)啟動的腳本中,但還是覺得啟動腳本的方式方便管理些。

安裝: 無論源碼還是pip都很順利,沒什么好說的 測試: 單獨(dú)測試是否安裝成功:

新建test.py文件,內(nèi)容如下: def application(env, start_response): start_response(\'200 OK\', [(\'Content-Type\',\'text/html\')]) return "Hello World"

然后在終端運(yùn)行: uwsgi --http :8001 --wsgi-file test.py

在瀏覽器內(nèi)輸入看是否有“Hello World”輸出,若沒有輸出,請檢查你的安裝過程。

項(xiàng)目配置文件: [uwsgi] socket = /path/to/socket #socket = 127.0.0.1:8989 #plugin = python wsgi-file = /path/to/project/wsgi.py processes = 2 master = true max-requests = 5000 daemonize = /path/to/log logfile-chmod = 644 pidfile = /path/to/pid limit-as = 1000 enable-threads = true vacuum = true log-maxsize = 40960000 threads = 2 virtualenv = /path/to/virtualenv uid = 1000 # 運(yùn)行的用戶

啟動腳本

沒在網(wǎng)上找到個(gè)滿意的,所以改造了下,這個(gè)腳步目前只能啟動uwsgi進(jìn)程,但是沒法殺進(jìn)程。啟動后,指定目錄下的項(xiàng)目配置文件如果發(fā)生改變,會自動重啟相應(yīng)進(jìn)程(uwsgi支持通過--emperor制定配置文件目錄,如果配置文件發(fā)生改變,會自動重啟相應(yīng)進(jìn)程). 以下是腳本: #!/bin/bash # chkconfig: - 90 15 # # wsgI init Script # # processname: uwsgi # description: Used to run python and wsgi web applciations. # author: Miguel Clara (miguelmclara@gmail.com) . /etc/rc.d/init.d/functions

# vars ########################### prog=/path/to/uwsgi (which uwsgi) desc=uWSGI daemoN_OPTS="--emperor /path/to/uwsgi/configs " # Change this if needed! lockfile=/path/to/uwsgi.lock start () { echo -n "Starting $DESC: " daemon $PROG $DAEMON_OPTS retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop () { echo -n "Stopping $DESC: " killproc $PROG retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } reload () { echo "Reloading $NAME" killproc $PROG -HUP retVAL=$? echo } restart () { stop start } rh_status () { status $PROG } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) if ! pidof $PROG >/dev/null; then rh_status_q && exit 0 $1 else echo -e "n$DESC is already started...n" fi ;; stop) if pidof $PROG >/dev/null; then rh_status_q || exit 0 $1 else echo -e "n$DESC can not be stoped because its not running...n" fi ;; restart|force-reload) $1 ;; reload) rh_status_q || exit 7 $1 ;; status) rh_status ;; *) echo "Usage: $0 {start|stop|restart|reload|force-reload|status}" >&2 exit 2 ;; esac exit 0 將腳本添加到系統(tǒng) cp this_script /etc/init.d/uwsgi sudo chkconfig —add uswgi sudo chkconfig uwsgi on 重啟服務(wù)器,查看進(jìn)程是否存在。重啟不僅是為了驗(yàn)證是否開機(jī)自啟動,如果不重啟可能會遇到很奇怪的問題,比如不知道什么東西啟動了進(jìn)程,殺業(yè)殺不完。 nginx

用的源碼編譯, nginx-1.8.0.tar.gz. 編譯&安裝: ./configure --prefix=/home/henry/softwares/nginx
--user=henry
--group=henry
--with-pcre=/home/henry/packages/pcre-8.37
--with-http_realip_module
--with-http_stub_status_module
--with-http_gzip_static_module
--with-http_stub_status_module
--with-http_ssl_module

make make install

init.d的腳本: #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: NGINX is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server # processname: nginx # config: /home/henry/softwares/nginx/conf/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /home/henry/softwares/nginx/logs/nginx.pid

# Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/home/henry/softwares/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/home/henry/softwares/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile="/home/henry/softwares/nginx/subsys/nginx.lock" make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" | sed \'s/[^*]*--user=([^ ]*).*/1/g\' -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep \'configure arguments:\'` for opt in $options; do if [ `echo $opt | grep \'.*-temp-path\'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac

添加腳本到系統(tǒng) sudo cp this_script /etc/init.d/nginx sudo chkconfig —add nginx sudo chkconfig nginx on

驗(yàn)證: 重啟服務(wù)器,訪問ip,看看是否能出現(xiàn)nginx成功的界面。

增加網(wǎng)站配置,采用虛擬主機(jī)的方式,在主配置文件中指定虛擬主機(jī)配置文件目錄,目錄中給每個(gè)網(wǎng)站寫一個(gè)配置文件:

主配置文件nginx.conf: user henry; 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; include conf.d/*.conf; }

在跟nginx.conf同級目錄下mkdir conf.d,用于存放vhost配置文件 vhost文件: 例如:bookbbs.conf: server { listen 80; server_name 192.168.30.154;

charset utf-8; access_log logs/bookbbs_access.log; location /static/ { alias /path/to/static/dir; } location /media/ { alias /path/to/media/dir; } location / { uwsgi_pass unix:///home/henry/softwares/nginx/logs/BookBBS.sock; include uwsgi_params; client_max_body_size 10m; } }

要注意的是,配置文件中該加”/“的地方通通都得加上,否則會失敗。

網(wǎng)站名稱:論壇搭建全記錄
鏈接URL:http://muchs.cn/article14/cjpcge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、移動網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)