準備篇
1.配置防火牆,開啟80連接埠、3306連接埠
1 vim /etc/sysconfig/iptables
2 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #允許80連接埠通過防火牆
3 -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #允許3306連接埠通過防火牆
service iptables restart
#重啟防火牆,使配置生效
2.關閉SELINUX(這個和伺服器安全相關,先關閉。相關介紹:https://wiki.centos.org/zh/HowTos/SELinux)
vim /etc/selinux/config#SELINUX=enforcing #注釋掉#SELINUXTYPE=targeted #注釋掉SELINUX=disabled #增加:wq #儲存退出 shutdown -r now #重啟系統
3.安裝第三方源
1)安裝EPEL倉庫
yum install -y epel-release
如果以上命令不起作用:
CentOS/RHEL 7
rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
CentOS/RHEL 6
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
2)安裝IUS倉庫
CentOS 6
rpm -Uvh https://centos6.iuscommunity.org/ius-release.rpm
CentOS 7
rpm -Uvh https://centos7.iuscommunity.org/ius-release.rpm
錯誤處理:
1.安裝IUS倉庫出錯:curl: (35) SSL connect error。這個問題在RHEL 6.1 -> RHEL 6.4應該都會出現,原因是curl使用了REST API。解決:
yum update nss
2.結果問題1沒解決,出另一個問題:Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again。解決:
vim /etc/yum.repos.d/CentOS-Base.repo
把有mirrorlist的都注釋掉,有baseurl的都去掉注釋,然後再進行步驟1,再安裝IUS倉庫。修改如下:
[epel]name=Extra Packages for Enterprise Linux 6 - $basearchbaseurl=http://download.fedoraproject.org/pub/epel/6/$basearch#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearchfailovermethod=priorityenabled=1gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6[epel-debuginfo]name=Extra Packages for Enterprise Linux 6 - $basearch - Debugbaseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearchfailovermethod=priorityenabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6gpgcheck=1[epel-source]name=Extra Packages for Enterprise Linux 6 - $basearch - Sourcebaseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearchfailovermethod=priorityenabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6gpgcheck=1
查看倉庫列表,看看是否安裝成功了:
yum repolist
ius | 2.3 kB 00:00 ius/primary_db | 183 kB 00:00 repo id repo name statusbase CentOS-6 - Base 5,062epel Extra Packages for Enterprise Linux 6 - i386 9,992extras CentOS-6 - Extras 39ius IUS Community Packages for Enterprise Linux 6 - i386 329updates CentOS-6 - Updates 382repolist: 15,804
安裝倉庫成功。接下來安裝nginx,mysql,php。
安裝篇
查看即將要安裝的軟體的版本:
yum list nginx mysql php
結果:
[root@localhost ~]# yum list nginx mysql phpLoaded plugins: fastestmirror, securityLoading mirror speeds from cached hostfile* base: mirrors.163.com* extras: mirrors.163.com* ius: hkg.mirror.rackspace.com* updates: mirrors.163.comAvailable Packages mysql.i686 5.1.73-7.el6 base nginx.i686 1.10.1-1.el6 epel php.i686 5.3.3-48.el6_8 updates
mysql和php的版本都有些舊,他們都來源於mirrors.163.com這個源。而epel和ius源的軟體版本一般比較新。
使用以下命令列出所有mysql和php的版本:
yum list mysql* php*
這會列出一大堆像這樣的結果,接下來只要選擇需要安裝的軟體版本就好:
軟體名字 軟體版本 #軟體所在倉庫
php56u.i686 5.6.26-1.ius.centos6 ius mysql55.i686 5.5.52-1.ius.centos6 ius 安裝nginxyum remove httpd* php* #刪除系統內建的軟體包yum install nginx #安裝nginx 根據提示輸入y進行安裝chkconfig nginx on #設定nginx開機啟動service nginx start #啟動nginx安裝MySQLyum remove mysql* #刪除系統已經安裝的mysqlyum list mysql* #列出mysql所有版本,找到要安裝的版本的名字yum install mysql57u* #我安裝的是mysql5.7.15。輸入Y即可自動安裝,直到安裝完成service mysqld start #啟動MySQLchkconfig mysqld on #設為開機啟動
安裝PHP
yum install php56u-fpm php56u-mbstring php56u-bcmath php56u-mcrypt php56u-xmlrpc php56u-pdo php56u-xml php56u-xmlrpc php56u-mysqlnd php56u-gd php56u-opcache #安裝php5.6及擴充chkconfig php-fpm on #設定php-fpm開機啟動service php-fpm start #啟動php-fpm
配置篇
nginx的設定檔是/etc/nginx下的nginx.conf。在nginx.conf檔案的最後一行可以看到: include /etc/nginx/conf.d/*.conf; 它把 /etc/nginx/conf.d目錄下尾碼為.conf的檔案都包含進來了,所以只要在/etc/nginx/conf.d/這個目錄下配置一個.conf檔案就行了。以下是我的配置:
## The default server#server { listen 80; server_name n.com; root /usr/share/nginx/html; index index.php index.html; # Load configuration files for the default server block. location / { try_files $uri $uri/ /index.php?$args; } location ~ .*\.(php)?$ { expires -1s; try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { }}
重啟nginx:
service nginx restart
在/usr/share/nginx/html/這個目錄下建立一個index.php
cd /usr/share/nginx/html #進入html目錄vim index.php #建立index.php,加上phpinfo();
瀏覽器訪問:centos的ip地址/index.php。完成!