Linux下安装Apache、Nginx、PHP、MySQL

本人初学者,有些不到之处多包涵,也别纠结。

首先说遇到的几个问题:
    1、IP。我是为自己的虚拟机设置了静态IP,在浏览器访问静态IP连接虚拟机。注意CentOS7和之前的版本更改IP方式不同。
    2、版本。各类软件之间版本决定支持与否,编译的时候遇到错误提示一定要看看是不是说版本不对。这里很坑...
    3、防火墙。我对防火墙是不太懂的,虚拟机防火墙开启是无法访问端口的,需要设置开启80端口。


一、安装依赖及工具
wget (在linux下使用 ' wget 地址 ' 的方式下载资源)
yum -y install wget
编译工具
yum -y install gcc automake autoconf libtool make
yum -y install gcc gcc-c++
pcre
在https://sourceforge.net/projects/pcre/ 找到.tar.gz文件右键复制连接
cd /usr/local/src
wget 粘贴 (https://sourceforge.net/projects/pcre/files/pcre/8.42/pcre-8.42.tar.gz/download)
tar -zxvf 文件名
cd 文件
./configure
make
make install
zlib
在http://zlib.net/ 找.tar.gz 右键复制链接
cd /usr/local/src
wget 粘贴(http://zlib.net/zlib-1.2.11.tar.gz)
tar -zxvf 文件名
cd 文件
./configure
make
make install
ssl
cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
tar -zxvf openssl-1.0.1t.tar.gz
(找的帖子里没有进行./configure make make install)我也没进行此操作,没问题。暂不明情况。


二、Nginx
1.安装
网站http://www.nginx.cn/nginx-download
wget 粘贴(http://nginx.org/download/nginx-1.10.2.tar.gz)
tar -zxvf 文件
cd 文件
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-8.42 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.1t
(注意pcre,zlib,ssl的版本和路径,以上配置了nginx启动和config位置)
make
make install


2.运行
/usr/local/nginx/nginx (因为设定了--sbin-path,所以不是/usr/local/nginx/sbin/nginx)
查看80端口 netstat -ano|grep 80
查看nginx进程 ps -ef|grep nginx
访问虚拟机ip,不成功。关闭防火墙
查看防火墙状态 firewall-cmd --state 
永久单一开启端口 firewall-cmd --zone=public --add-port=80/tcp --permanent
关闭防火墙 systemctl stop firewalld.service 
开启防火墙  systemctl start firewalld.service 
开机禁止启动防火墙 systemctl disable firewalld.service
重启防火墙 firewall-cmd --reload
刷新出现welcome nginx,成功。
3.关闭
查看进程号
ps -ef|grep nginx
root     26398     1  0 16:33 ?        00:00:00 nginx: master process /usr/local/nginx/nginx
nobody   26399 26398  0 16:33 ?        00:00:00 nginx: worker process
root     26498  2515  0 16:41 pts/1    00:00:00 grep --color=auto nginx
其中 26398为主进程号
a.从容停止 kill -QUIT 进程号
b.快速停止 kill -TERM(INT) 进程号
c.强制停止 kill -9 进程号
4.重启
cd /usr/local/nginx
./nginx -s reload
5.支持php
安装fpm
yum install php-fpm
启动fpm
service php-fpm start
修改配置,见6.b
6.配置
a.显示目录
在nginx.conf的http下添加autoindex on;
删掉/usr/local/nginx/html下的index.html(即那个welcome nginx)
验证配置文件正确性 /usr/local/nginx/nginx -t
重启nginx
b.配置php支持
解锁nginx.conf以下被注释了的内容。略做更改。把root提到server下。
location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        重启nginx
        重启fpm service php-fpm restart


三、Apache
1.安装
yum -y install httpd
2.启动
systemctl start httpd.service
3.停止
systemctl stop httpd.service
4.配置
vim /etc/httpd/conf/httpd.conf
5.访问
在/var/www/html/下添加test.html
访问 ip/test.html
6.支持perl
yum -y install perl perl-CGI


四、PHP
yum install php php-devel
service httpd restart
yum install php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc
查看php版本 php -v

        


五、MySQL
yum install mariadb-server -y
systemctl start mariadb.service
systemctl enable mariadb.service
查看mysql版本 mysql -V


设置用户密码 mysqladmin -u root password "123" (第一次有效?不设置也可以直接下一步)
进入数据库 mysql -uroot -p123
数据库命令后边必须加分号才能回车执行。
显示数据库 show databases;
创建数据库 create database 数据库名;
使用数据库 use 数据库名
创建表(注意不能有tab键)
create table `user`(id int unsigned not null auto_increment comment 'ID', name varchar(4) not null default '新垣结衣' comment '索引名字(第一个字,倒序)',money decimal(8,2) not null default 0 comment '钱包',best_friend int unsigned comment '外键好朋友',create_time datetime not null default '2018-06-21' comment '创建时间',primary key(id),index i_name(name(1) desc))engine=innodb default charset = utf8;
显示表字段详情 show columns from 表名;
删除表 drop table 表名

猜你喜欢

转载自blog.csdn.net/z772532526/article/details/80776993