服务器架设篇-----CentOS架设WWW服务器-Nginx+Mysql+PHP
生活随笔
收集整理的這篇文章主要介紹了
服务器架设篇-----CentOS架设WWW服务器-Nginx+Mysql+PHP
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Nginx安裝配置
安裝nginx
系統(tǒng)環(huán)境:CentOS-6.3
軟件:nginx-1.7.9.tar.gz
安裝方式:源碼編譯安裝?
安裝位置:/usr/local/nginx?
安裝前提
在安裝nginx前,需要確保系統(tǒng)安裝了g++、gcc、openssl-devel、pcre-devel和zlib-devel軟件。安裝必須軟件:
[root@webserver /]#yum install gcc-c++ [root@webserver /]#yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel檢查系統(tǒng)安裝的Nginx:
[root@webserver /]# find -name nginx ./nginx ./nginx/sbin/nginx ./nginx-1.7.9/objs/nginx卸載原有的Nginx
[root@webserver /]# yum remove nginx安裝
將安裝包文件上傳到/usr/local/soft中執(zhí)行以下操作:
[cjky@webserver ~]# su root [root@webserver ~]# cd /usr/local [root@webserver local]# mkdir soft [root@webserver local]# cd soft [root@webserver soft]#wget http://nginx.org/download/nginx-1.7.9.tar.gz解壓縮
[root@webServer soft]# tar -zxv -f nginx-1.7.9.tar.gz [root@webServer soft]#ls nginx-1.7.9 nginx-1.7.9.tar.gz [root@webServer soft]# mv nginx-1.7.9 /usr/local/nginx [root@webServer soft]# rm -rf nginx-1.7.9.tar.gz編譯安裝
[root@webServer nginx]#./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf [root@webServer nginx]# make&&make install安裝完成后 目錄有如下文件:
[root@webServer nginx]# lsauto fastcgi.conf koi-win mime.types.default scgi_params CHANGES fastcgi.conf.default LICENSE nginx.conf scgi_params.default CHANGES.ru fastcgi_params logs nginx.conf.default src conf fastcgi_params.default Makefile objs uwsgi_params configure html man README uwsgi_params.default contrib koi-utf mime.types sbin win-utf
?配置
#修改防火墻配置: [root@webserver nginx]# vi + /etc/sysconfig/iptables #添加配置項(xiàng) -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #重啟防火墻 [root@webserver nginx]# service iptables restart啟動(dòng)nginx
#方法1 [root@webserver nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #方法2 [root@webserver nginx]# cd /usr/local/nginx/sbin [root@webserver sbin]# ./nginx停止nginx
#查詢nginx主進(jìn)程號(hào) [root@webserver nginx]# ps -ef | grep nginx #停止進(jìn)程 [root@webserver nginx]#kill -QUIT 主進(jìn)程號(hào) #快速停止 [root@webserver nginx]#kill -TERM 主進(jìn)程號(hào) #強(qiáng)制停止 [root@webserver nginx]# pkill -9 nginx重啟nginx
[root@webserver nginx]# /usr/local/nginx/sbin/nginx -s reload測(cè)試是否安裝成功
#測(cè)試端口 [root@webserver nginx]#netstat –na|grep 80 #瀏覽器中測(cè)試 http://127.0.0.1:80MySql安裝配置
1.通過(guò)yum安裝
#直接安裝yum源中的mysql [root@webserver ~]#yum -y install mysql-server2.修改mysql配置
#暫修改默認(rèn)編碼 [root@webserver ~]#vim /etc/my.cnf 添加default-character-set = utf8/var/lib/mysql #mysql數(shù)據(jù)庫(kù)的數(shù)據(jù)庫(kù)文件存放位置 /var/log/mysqld.log #mysql數(shù)據(jù)庫(kù)的日志輸出存放位置 netstat -anp #命令來(lái)查看3306端口是否在監(jiān)聽(tīng)3.設(shè)置mysql隨系統(tǒng)啟動(dòng)
#設(shè)置MySQL服務(wù)隨系統(tǒng)啟動(dòng)自啟動(dòng) [root@webserver ~]#chkconfig mysqld on [root@webserver ~]#chkconfig --list mysqld 確保2--5為on的狀態(tài)就行,然后啟動(dòng)MySQL服務(wù) [root@webserver ~]#/etc/rc.d/init.d/mysqld start4.設(shè)置root密碼和用戶操作
#為root用戶設(shè)置密碼 [root@webserver ~]/usr/bin/mysqladmin -u root password 'newPassword' #用root用戶登錄MySQL服務(wù)器 [root@webserver ~]#mysql -u root -p #查看用戶信息 mysql>select user,host,password from mysql.user;#建立對(duì)test數(shù)據(jù)庫(kù)有完全操作權(quán)限的名為userA的用戶 #格式:grant select on 數(shù)據(jù)庫(kù)名.* to 用戶名@登錄主機(jī) identified by "密匙"; mysql>grant all privileges on test.* to userA@localhost identified by '用戶密匙'; #創(chuàng)建一個(gè)可以從任何地方連接服務(wù)器的一個(gè)完全的超級(jí)用戶userM mysql>grant all privileges on *.* to userM@% identified by '密匙'; #刪除root的遠(yuǎn)程授權(quán) mysql>revoke all privileges on *.* from root@"%"; #刪除root的遠(yuǎn)程用戶 mysql>delete from user where user="root" and host="%"; #刷新 使以上操作生效 mysql>flush privileges;#修改userA的密碼為NewPassword mysql>set password for userA@localhost=password('NewPassword'); #刪除匿名用戶 mysql>delete from mysql.user where user='';5.數(shù)據(jù)庫(kù)常用sql
mysql>show databases; #查看系統(tǒng)中所有數(shù)據(jù)庫(kù) mysql>drop database test; #刪除名為test的數(shù)據(jù)庫(kù),不提醒 mysql>mysqladmin drop test; #刪除名為test的數(shù)據(jù)庫(kù)前,有提示mysql>show variables like 'port'; #mysql查看打開(kāi)的端口mysql>create database test; #建立名為test的數(shù)據(jù)庫(kù) mysql>use test; #連接到數(shù)據(jù)庫(kù) mysql>show tables; #查看數(shù)據(jù)庫(kù)中已存在的表 mysql>alter table test rename test_study; #重命名表test為test_study mysql>describe test; #表的詳細(xì)描述6.CentOS系統(tǒng)中mysqldump
#備份數(shù)據(jù)庫(kù) [root@webserver ~]mysqldump -h yourhost vi -uroot -p dbname <dbname_backup.sql #恢復(fù)數(shù)據(jù)庫(kù) [root@webserver ~]mysqldump -h yourhost -uroot -p dbname < dbname_backup.sql #如果只想Dump建表指令,則命令如下: [root@webserver ~]mysqladmin -u root -p -d databasename > a.sql #如果只想Dump插入數(shù)據(jù)的sql命令,而不需要建表命令,則命令如下: [root@webserver ~]mysqladmin -u root -p -t databasename > a.sql #那么只想要數(shù)據(jù),而不想要什么sql命令時(shí), [root@webserver ~]mysqldump -T./ phptest driver #其中,只有指定了-T參數(shù)才可以輸出純文本文件,表示輸出數(shù)據(jù)的目錄,./表示當(dāng)前目錄,即與mysqldump同一目錄。 #如果不指定driver 表,則將卸出整個(gè)數(shù)據(jù)庫(kù)的數(shù)據(jù)。每個(gè)表會(huì)生成兩個(gè)文件,一個(gè)為.sql文件,包含建表執(zhí)行。另一個(gè)為.txt文件,只包含數(shù)據(jù),且沒(méi)有sql指令。PHP安裝配置
[root@webserver soft]# groupadd www [root@webserver soft]# useradd -g www -s /sbin/nologin -M www # 安裝插件 [root@webserver soft]# yum install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel –y # 獲取需安裝的PHP版本 [root@webserver soft]# wget http://cn2.php.net/get/php-5.6.9.tar.gz #解壓 [root@webserver soft]# tar -zxvf php-5.6.9.tar.gz #編譯安裝 [root@webserver soft]# cd php-5.6.9 [root@webserver php-5.6.9]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-zip --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-readline --with-mhash --with-openssl --with-curl --with-gettext --with-iconv [root@webserver php-5.6.9]# make&&make install make installl無(wú)報(bào)錯(cuò),安裝成功 [root@webserver php-5.6.9]# cp php.ini-development /usr/local/php/etc/php.ini # 默認(rèn)的php.ini沒(méi)有自動(dòng)生成,需要手動(dòng)復(fù)制php配置文件到安裝目錄 [root@webserver php-5.6.9]# cd /usr/local/php/# 切換至php安裝目錄 [root@webserver php]# rm -rf /etc/php.ini # 刪除系統(tǒng)自帶配置文件 [root@webserver php]# ln -s /usr/local/php/etc/php.ini /etc/php.ini # 添加軟鏈接到 /etc目錄 [root@webserver php]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf # 拷貝模板文件為php-fpm配置文件 [root@webserver php]# vi /usr/local/php/etc/php-fpm.conf # 編輯fpm配置文件 user = www # 設(shè)置php-fpm運(yùn)行賬號(hào)為www group = www # 設(shè)置php-fpm運(yùn)行組為www pid = run/php-fpm.pid # 取消前面的分號(hào) :wq! # 保存退出[root@webserver php]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf # 添加軟連接到 /etc目錄 [root@webserver php]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm# 設(shè)置 php-fpm開(kāi)機(jī)啟動(dòng) [root@webserver php]# chmod +x /etc/init.d/php-fpm# 添加執(zhí)行權(quán)限 [root@webserver php]# chkconfig php-fpm on # 設(shè)置開(kāi)機(jī)啟動(dòng)[root@webserver php]# vi /usr/local/php/etc/php.ini # 編輯配置文件 找到:disable_functions = 修改為:disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname # 列出PHP可以禁用的函數(shù),如果某些程序需要用到這個(gè)函數(shù),可以刪除,取消禁用。 找到:;date.timezone = 修改為:date.timezone = PRC #設(shè)置時(shí)區(qū) 找到:expose_php = On 修改為:expose_php = Off #禁止顯示php版本的信息 找到:short_open_tag = Off 修改為:short_open_tag = ON #支持php短標(biāo)簽 找到:opcache.enable=0 修改為opcache.enable=1 #php支持opcode緩存 找到:;opcache.enable_cli=1 修改為:opcache.enable_cli=0 #php支持opcode緩存 在最后一行添加:zend_extension=opcache.so #開(kāi)啟opcode緩存功能 :wq! #保存退出[root@webserver php]# service php-fpm start# 啟動(dòng)服務(wù) [root@webserver php]# /usr/local/php/bin/php -v PHP 5.6.3 (cli) (built: Mar 22 2016 16:55:35) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies [root@webserver php]# php -v -bash: php: command not found [root@webserver php]# export PATH=$PATH:/usr/local/php/bin [root@webserver php]# php -v PHP 5.6.3 (cli) (built: Mar 22 2016 16:55:35) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies PHP搭建成功,接下來(lái)配置Nginx支持PHP [root@webserver php]# vi /usr/local/nginx/conf/nginx.conf# 配置修改 user www www; # 首行user去掉注釋,修改Nginx運(yùn)行組為www www;必須與/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否則php運(yùn)行出錯(cuò) index index.html index.htm index.php; # 添加index.php location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # 取消FastCGI server部分location的注釋,注意fastcgi_param行的參數(shù),改為$document_root$fastcgi_script_name,或者使用絕對(duì)路徑[root@webserver php]#/etc/init.d/nginx restart # 重啟nginx [root@webserver php]#service php-fpm start # 啟動(dòng)php-fpm [root@webserver php]#cd /usr/local/nginx/html/ # 進(jìn)入nginx默認(rèn)網(wǎng)站根目錄 [root@webserver html]#rm -rf /usr/local/nginx/html/* # 刪除默認(rèn)測(cè)試頁(yè) [root@webserver html]#vi index.php # 新建index.php文件 <?php phpinfo(); ?> :wq! # 保存退出[root@webserver html]#chown -R www.www /usr/local/nginx/html/ # 設(shè)置目錄所有者 [root@webserver html]#chmod -R 700 /usr/local/nginx/html/ # 設(shè)置目錄權(quán)限轉(zhuǎn)載于:https://www.cnblogs.com/cjky/p/4210607.html
總結(jié)
以上是生活随笔為你收集整理的服务器架设篇-----CentOS架设WWW服务器-Nginx+Mysql+PHP的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 设计模式 -- (14)中介者模式
- 下一篇: You must rebuild it