前言:上一篇寫(xiě)到Git倉(cāng)庫(kù)的搭建,有朋友說(shuō)服務(wù)器還沒(méi)搭建好,所以這里來(lái)一篇環(huán)境搭建的。
注:我搭建是Centos6版本以上的,Centos7以上有大的改變
一、配置防火墻,開(kāi)啟80端口、3306端口,刪除原有的iptables ,添加合適的配置
rm -rf /etc/sysconfig/iptablesvim /etc/sysconfig/iptables
添加一下內(nèi)容:
# Firewall configuration written by system-config-firewall# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT
:wq保存退出,重啟防火墻使配置生效
/etc/init.d/iptables restart? ? centos 7.0以上執(zhí)行? systemctl restart firewalld.service
關(guān)閉SELINUX
rm -rf? /etc/selinux/configvim /etc/selinux/config
然后添加一行內(nèi)容:
SELINUX=disabled
:wq 保存退出
進(jìn)行重啟系統(tǒng)
shutdown -r now
注意:配置好防火墻必須重啟系統(tǒng)
二、安裝第三方y(tǒng)um源
安裝下載工具
yum install wget
下載
wget http://www.atomicorp.com/installers/atomic
安裝
sh ./atomic
更新yum源
yum check-update
三、安裝Nginx
刪除系統(tǒng)自帶的軟件包
yum remove httpd* php*
安裝nginx
yum install -y nginx
設(shè)置nginx開(kāi)機(jī)啟動(dòng)
chkconfig nginx on? Centos 7.0 以上執(zhí)行 systemctlenablenginx.service
啟動(dòng)nginx
service nginx startCentos 7.0 以上 systemctl start? nginx.service
注:nginx啟動(dòng)可能失敗,需要修改配置文件報(bào)錯(cuò)如下
Starting nginx:nginx:[emerg] socket() [::]:80 failed (97:Address family not supported by protocol)? [FAILED]
處理方法
vim /etc/nginx/conf.d/default.conf
將
listen? ? ? 80 default_server;
listen? ? ? [::]:80 default_server;
改為:
listen? ? ? 80;#listen? ? ? [::]:80 default_server;
保存退出:wq,重啟成功
啟動(dòng)成功后,打開(kāi)瀏覽器,輸入ip地址,可看到nginx歡迎頁(yè)
四、安裝PHP
檢查當(dāng)前安裝的PHP包
yum list installed | grep php
如果有安裝的PHP包,先刪除他們,如:
yum remove php.x86_64 php-cli.x86_64 php-common.x86_64
配置安裝包源:
Centos 5.X
rpm -Uvh http://mirror.webtatic.com/yum/el5/latest.rpm
CentOs 6.x?
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
CentOs 7.X?
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm?
rpm -Uvh? https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
注:不同版本服務(wù)器需要配置對(duì)應(yīng)的安裝包,如果想刪除上面安裝的包重新安裝
rpm -qa | grep webstaticrpm-e[上面搜索到的包即可]
執(zhí)行安裝
yum -y install php56w.x86_64
yum -y --enablerepo=webtatic install php56w-devel
yum -y install php56w-gd.x86_64 php56w-ldap.x86_64? php56w-mbstring.x86_64 php56w-mcrypt.x86_64
yum -y install php56w-mysql.x86_64 php56w-pdo.x86_64 php56w-opcache.x86_64
注:每條命令看到Complete!才成功
安裝PHP FPM
yum -y install php56w-fpm
設(shè)置php-fpm開(kāi)機(jī)啟動(dòng)
chkconfig php-fpm onCentos7.0以上? systemctlenablephp-fpm.service
啟動(dòng)php-fpm
/etc/init.d/php-fpm startCentos 7.0 以上 systemctl start? php-fpm.service
注:如果想更換到php5.5或5.4版本,直接把上面的56w換成55w或者54w就可以了
五、?安裝 MySQL
安裝
yum install -y mysql mysql-server
啟動(dòng)MySQL
/etc/init.d/mysqld start
設(shè)為開(kāi)機(jī)啟動(dòng)
chkconfig mysqld on
拷貝配置文件(注意:如果/etc目錄下面默認(rèn)有一個(gè)my.cnf,直接覆蓋即可)
cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
為root用戶設(shè)置密碼
mysql_secure_installation
回車(chē),根據(jù)提示輸入Y,輸入2次密碼,回車(chē),根據(jù)提示一路輸入Y,最后出現(xiàn):
Thanksforusing MySQL!
MySql密碼設(shè)置完成,重新啟動(dòng) MySQL:
#重啟
/etc/init.d/mysqld restart
停止
/etc/init.d/mysqld stop
啟動(dòng)
/etc/init.d/mysqld start
六、配置nginx
我存放的nginx路徑地址
[root@iZ2zeftluibm3hesz36v3tZ conf.d]# pwd/etc/nginx/conf.d
[root@iZ2zeftluibm3hesz36v3tZ conf.d]# lltotal 20-rw-r--r-- 1 root root 1501 Feb? 9 10:54 admin.shop.conf-rw-r--r-- 1 root root? 434 Dec 25 20:39 default.conf.bak-rw-r--r-- 1 root root 1419 Feb? 8 17:19 web.shop.conf
因?yàn)閚ginx.conf里面
include /etc/nginx/conf.d/*.conf;
在/etc/nginx/conf.d下面新建一個(gè).conf的文件 把這個(gè)東西復(fù)制進(jìn)去?
?{? ? ? ? ? ? ??
? ? ? listen? 80;? ? ? ?
?? ? ? server_name? ? 解析域名;? ? ? ? ? ?
? ? ?index index.php index.html index.htm default.html default.htm default.php; ? ? ? ? ? ? ? ? ? ? ? ? ?client_header_buffer_size 16k;? ? ? ? ? ??
? large_client_header_buffers 4 32k;? ? ? ? ? ??
? client_max_body_size 300m;? ? ? ? ? ? ?
?client_body_buffer_size 128k;? ? ? ? ? ?
?? location / {? ? ? ? ? ? ? ? ? ? ??
? root? ? ? ? ? ? ? 項(xiàng)目路徑;? ? ? ? ? ? ? ? ? ? ? ??
index? ? ? ? ? ? ? index.php;? ? ? ? ? ? ? ? ? ??
? ? proxy_connect_timeout 3000;? ? ? ? ? ? ? ? ??
? ? ? proxy_send_timeout 3000;? ? ? ? ? ? ? ? ?
?? ? ? proxy_read_timeout 3000;? ? ? ? ? ? ? ? ? ? ??
? proxy_buffer_size 4k;? ? ? ? ? ? ? ? ? ? ?
?? proxy_buffers 4 32k;? ? ? ? ? ? ? ? ? ? ? ?
?proxy_busy_buffers_size 64k;
if(!-e$request_filename) {? ? ? ? ? ? ? ? ? ? ??
? rewrite ^/index.php(.*)$ /index.php?s=$1last;? ? ? ? ? ? ? ? ? ?
?? ? rewrite ^(.*)$ /index.php?s=$1last;break;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ?
?? ? }? ? ? ? ? ?
?? location ~ \.php$ {? ? ? ? ? ??
? ? ? ? ? ? root? ? ? ? ? ? ? 項(xiàng)目路徑;? ? ??
? ? ? ? ? ? fastcgi_pass? 127.0.0.1:9000;? ?
? ? ? ? ? ? ?fastcgi_index? index.php;? ? ? ?
? ? ? ? ? ? ?fastcgi_param? SCRIPT_FILENAME$document_root$fastcgi_script_name;? ? ? ? ? ? ? ? ? ? ? ? ????????????include? ? ? ? fastcgi_params;? ? ? ? ? ? ? ? ? ? ? ??
? ? ? }
}
:wq 保存退出
七、 配置php-fpm
vim /etc/php-fpm.d/www.conf
將用戶和用戶組設(shè)置為nginx,如:
修改用戶為nginx
user = nginx
修改組為nginx
group = nginx
八、開(kāi)始測(cè)試
cd/var/wwwvim index.php
添加以下代碼
phpinfo();
?>
:wq!
保存退出
? ? ? ? #設(shè)置權(quán)限
chown nginx.nginx /var/www -R
? ? ? ? #重啟nginx
service nginx restart
? ? ? ? #重啟php-fpm
service php-fpm restart
這時(shí)你的mysql數(shù)據(jù)庫(kù)不能遠(yuǎn)程連接的,此時(shí)你應(yīng)該授權(quán)一下
USE mysql;? GRANT ALL PRIVILEGES ON *.* TO'root'@'%'WITH GRANT OPTION;? FLUSH PRIVILEGES;
注意:GRANT ALL PRIVILEGES表示授權(quán)所有權(quán)限,上面*.*中第一個(gè)*是授權(quán)可以訪問(wèn)的數(shù)據(jù)庫(kù)名稱, root是用戶名, %表示授權(quán)所有的IP地址。
到這里就大致寫(xiě)完了,網(wǎng)上搭建服務(wù)器環(huán)境的很多,我也是綜合各個(gè)前人經(jīng)驗(yàn),加入一些自己的理解,希望對(duì)有需要的朋友能有一點(diǎn)小小的幫助。這里推薦我上一篇Git版本庫(kù)的搭建哦。
哈哈,知識(shí)在于分享,愿大家一塊成長(zhǎng)。
掘金地址:https://juejin.im/post/5a7d35625188257a5e5739a6