一、安裝Nginx
1、切換安裝源
我們先把yum源換成國(guó)內(nèi)的阿里云鏡像源(當(dāng)然不換也可以),先備份一下原來(lái)的源鏡像文件,以免出錯(cuò)后可以恢復(fù):
[root@tuniao ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
下載新的CentOS-Base.repo 到/etc/yum.repos.d/,版本根據(jù)自己的系統(tǒng)版本選擇下載:
CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
更改/etc/yum.repos.d/CentOS-Media.repo使其為不生效:
enabled=0
運(yùn)行yum makecache生成緩存:
yum clean all
yum makecache
yum update
2、安裝Nginx
由于yum源中沒(méi)有我們想要的nginx,那么我們就需要?jiǎng)?chuàng)建一個(gè)“/etc/yum.repos.d/nginx.repo”的文件,其實(shí)就是新增一個(gè)yum源。
[root@tuniao yum.repos.d]# vim /etc/yum.repos.d/nginx.repo
把如下內(nèi)容復(fù)制進(jìn)去:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
然后保存退出,輸入 yum list nginx 查看
[root@tuniao ~]# yum list nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base | 3.6 kB 00:00:00
epel | 5.3 kB 00:00:00
extras | 3.4 kB 00:00:00
nginx | 2.9 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/3): epel/x86_64/updateinfo | 993 kB 00:00:00
(2/3): epel/x86_64/primary_db | 6.8 MB 00:00:00
(3/3): nginx/7/x86_64/primary_db | 46 kB 00:00:02
Available Packages
nginx.x86_64 1:1.16.0-1.el7.ngx nginx
[root@tuniao ~]# yum list |grep nginx
collectd-nginx.x86_64 5.8.1-1.el7 epel
munin-nginx.noarch 2.0.49-1.el7 epel
nextcloud-nginx.noarch 10.0.4-2.el7 epel
nginx.x86_64 1:1.16.0-1.el7.ngx nginx
nginx-all-modules.noarch 1:1.12.2-3.el7 epel
nginx-debug.x86_64 1:1.8.0-1.el7.ngx nginx
nginx-debuginfo.x86_64 1:1.16.0-1.el7.ngx nginx
nginx-filesystem.noarch 1:1.12.2-3.el7 epel
nginx-mod-http-geoip.x86_64 1:1.12.2-3.el7 epel
nginx-mod-http-image-filter.x86_64 1:1.12.2-3.el7 epel
nginx-mod-http-perl.x86_64 1:1.12.2-3.el7 epel
nginx-mod-http-xslt-filter.x86_64 1:1.12.2-3.el7 epel
nginx-mod-mail.x86_64 1:1.12.2-3.el7 epel
nginx-mod-stream.x86_64 1:1.12.2-3.el7 epel
nginx-module-geoip.x86_64 1:1.16.0-1.el7.ngx nginx
nginx-module-geoip-debuginfo.x86_64 1:1.16.0-1.el7.ngx nginx
nginx-module-image-filter.x86_64 1:1.16.0-1.el7.ngx nginx
nginx-module-image-filter-debuginfo.x86_64
1:1.16.0-1.el7.ngx nginx
nginx-module-njs.x86_64 1:1.16.0.0.3.2-1.el7.ngx nginx
nginx-module-njs-debuginfo.x86_64 1:1.16.0.0.3.2-1.el7.ngx nginx
nginx-module-perl.x86_64 1:1.16.0-1.el7.ngx nginx
nginx-module-perl-debuginfo.x86_64 1:1.16.0-1.el7.ngx nginx
nginx-module-xslt.x86_64 1:1.16.0-1.el7.ngx nginx
nginx-module-xslt-debuginfo.x86_64 1:1.16.0-1.el7.ngx nginx
nginx-nr-agent.noarch 2.0.0-12.el7.ngx nginx
owncloud-nginx.noarch 9.1.5-1.el7 epel
pcp-pmda-nginx.x86_64 4.1.0-5.el7_6 updates
python2-certbot-nginx.noarch 0.35.1-1.el7 epel
如果執(zhí)行命令是這樣的顯示效果,那么我們的nginx的yum源就配置成功啦!
然后要安裝我們的nginx就直接執(zhí)行
yum -y install nginx
啟動(dòng)Nginx并且設(shè)置為開(kāi)機(jī)啟動(dòng),可以使用curl 127.0.0.1查看是否啟動(dòng)成功
[root@tuniao ~]# systemctl start nginx
[root@tuniao ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2019-07-16 13:13:58 CST; 5s ago
Docs: http://nginx.org/en/docs/
Process: 5433 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 5434 (nginx)
CGroup: /system.slice/nginx.service
├─5434 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─5435 nginx: worker process
Jul 16 13:13:58 iZwz961b3fgv6g8a3oongyZ systemd[1]: Starting nginx - high performance web server...
Jul 16 13:13:58 iZwz961b3fgv6g8a3oongyZ systemd[1]: PID file /var/run/nginx.pid not readable (yet?) a...rt.
Jul 16 13:13:58 iZwz961b3fgv6g8a3oongyZ systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@tuniao ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@iZwz961b3fgv6g8a3oongyZ ~]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
3、配置防火墻和阿里云的安全組
[root@tuniao ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@tuniao ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success
[root@tuniao ~]# firewall-cmd --reload
二、安裝MySQL數(shù)據(jù)庫(kù)
1、安裝 MySQL 源
yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
2、安裝 MySQL
yum install mysql-community-server
3、配置并啟動(dòng) MySQL并設(shè)置為開(kāi)機(jī)啟動(dòng)
[root@tuniao ~]# systemctl start mysqld
[root@tuniao ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-07-16 16:16:12 CST; 4s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 5985 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 5912 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 5989 (mysqld)
CGroup: /system.slice/mysqld.service
└─5989 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Jul 16 16:16:07 iZwz961b3fgv6g8a3oongyZ systemd[1]: Starting MySQL Server...
Jul 16 16:16:12 iZwz961b3fgv6g8a3oongyZ systemd[1]: Started MySQL Server.
[root@tuniao ~]# systemctl enable mysqld
獲取密碼
grep 'temporary password' /var/log/mysqld.log
得到這行 A temporary password is generated for root@localhost: Jqqskhz1Wr(:冒號(hào)后面的就是密碼
進(jìn)入 MySQL 對(duì)數(shù)據(jù)庫(kù)的權(quán)限進(jìn)行一定的修改
mysql -uroot -p
#修改密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY ********** (密碼請(qǐng)用引號(hào)包起來(lái) 注意 MySQL 的密碼必須復(fù)雜 不復(fù)雜會(huì)報(bào)錯(cuò))
#開(kāi)放遠(yuǎn)程訪問(wèn)權(quán)限
use mysql;
update user set host = '%' where user = 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
//遠(yuǎn)程連接數(shù)據(jù)庫(kù)的時(shí)候需要輸入用戶名和密碼
用戶名:root
密碼:123456
指點(diǎn)ip:%代表所有Ip,此處也可以輸入Ip來(lái)指定Ip
輸入后使修改生效還需要下面的語(yǔ)句
mysql>FLUSH PRIVILEGES;
修改數(shù)據(jù)庫(kù)的默認(rèn)端口3306
往vim /etc/my.cnf文件中添加port=【端口號(hào)】
port=6603
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
重啟mysql服務(wù)器
[root@tuniao ~]# systemctl restart mysqld
防火墻打開(kāi)3306端口
firewall-cmd --zone=public --add-port=6603/tcp --permanent
firewall-cmd --reload
三、安裝php
由于 linux 的 yum 源不存在 php7.x,所以我們要更改 yum 源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum 安裝 php72w 和各種拓展,選自己需要的即可,由于apache和nginx的不一樣,所以這里重點(diǎn)需要安裝php-fpm
yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
啟動(dòng)php-fpm并設(shè)置開(kāi)機(jī)啟動(dòng)
systemctl start php-fpm
systemctl enable php-fpm
配置nginx的配置文件
修改/etc/nginx/nginx.conf的user為root

修改/etc/nginx/conf.d/default.conf里面的網(wǎng)站目錄 root的值
location / {
root /home/vsftpd/httpuser/www/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /home/vsftpd/httpuser/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /$document_root$fastcgi_script_name;
include fastcgi_params;
}
重啟nginx服務(wù)器即可
配置過(guò)程中遇到的問(wèn)題
1、配置完成服務(wù)器后,對(duì)服務(wù)器進(jìn)行訪問(wèn)的時(shí)候出現(xiàn)403 Forbidden
這個(gè)時(shí)候需要檢查網(wǎng)站的文件夾以及對(duì)應(yīng)的父級(jí)目錄是否具有其他用戶的可讀可寫權(quán)限
可以使用chmod -R命令對(duì)主目錄以及其子目錄和文件進(jìn)行權(quán)限設(shè)置
就好像我們這里配置的網(wǎng)站主目錄在vsftpd下的,由于vsftpd的文件對(duì)其他用戶沒(méi)有讀寫權(quán)限,所以需要使用
chmod -R 755 vsftpd/
進(jìn)行權(quán)限的修改
2、如果網(wǎng)站下有.htaccess文件需要使用,需要對(duì)nginx的服務(wù)器配置文件添加以下內(nèi)容
location ~ /\.ht {
deny all;
}