Under Mac with apache set up a LAN server

One: As the system comes with Apache environment MacOX, so we configured with Apache on Mac systems.

  Mac System: 10.14.4

 

Two: Start Apache

Start at the terminal input: sudo apachectl start
verification in your browser and enter " HTTP: // localhost " It works show that the server has successfully started up!

In Mac under Apache's default root directory under "/ Library / WebServer / Documents /", you can also set up their own folder, only you need to modify it in the Apache configuration.

 

Three: Configure server

1, in a new folder in finder

 

 

2, a terminal is opened, switching the working directory: cd / etc / apache2

3, backup files, need only be performed once: sudo cp httpd.conf httpd.conf.bak (This step is just to back up what)

   If the operator error! You can use the command to restore the backup httpd.conf file: sudo cp httpd.conf.bak httpd.conf

4, with vim edit the httpd.conf file when the file httpd.conf Apache configuration file: sudo vim httpd.conf

   You will be asked to enter a password, then the following interface will appear:
 
 
5, followed by modifying the httpd.conf file - This file is the Apache configuration file.
   Hold down the shift key, and enter ":" No to enter the vim command mode (must be in the English state input), search / DocumentRoot,
     Find the figure corresponds to the position of the circle path to create a path previously NetTest folder. (NOTE: In press state i to enter English input mode to modify the path)
 

6, after finding Options FollowSymLinks: modify the Options Indexes FollowSymLinks, Indexes add a word between two words.

7、接下来查找php,:/php,定位到图中位置:

 

 

8、将这句代码前面的#去掉。最后 Esc退出, :wq保存并退出。

9、切换工作目录:cd /etc

10、拷贝php.ini文件:sudo cp php.ini.default php.ini

重新启动apache服务器

在终端输入:sudo apachectl -k restart

到这里就算是配置完了,这个时候如果在浏览器地址输入“http://127.0.0.1/”,看是否成功
 
可能报错:
 
(1) : Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message。
原因:ServerName未配置。
解决:打开配置文件 sudo vim /etc/apache2/apache2.conf        
   找到配置文件中这个地方: 改为localhost: 80
 
 
(2):访问页面出现 Forbidden
You don't have permission to access / on this server.
原因:这是由于配置中没有打开访问配置,把下图中两处改为All和granted
https://www.80note.com/2019/01/451.html 此处有详细说明
 
 
这样就可以将Test文件夹中的目录列出来了。同一工作组里的电脑可以通过本电脑的ip地址来访问本电脑上的文件。
 
 
四:penSSL证书生成及Mac上Apache服务器配置HTTPS
 
先在桌面创建个SSL文件夹,用来放生成的私钥证书文件
打开终端cd到SSL文件夹    cd desktop/SSL

1. 自签名证书

(1) 在SSL文件夹中生成私钥 生成rsa私钥,2048位强度,server.key是秘钥文件名
   openssl genrsa -out server.key 2048 
(2) 生成自签名证书
 openssl req -new -sha256 -x509 -days 365 -key server.key -out server.crt 
 
按顺序输入信息

Common Name应该与域名保持一致(如我的电脑搭建的服务器IP地址为192.168.1.112) 

2. 配置Apache服务器SSL

(1) 放入证书
 

将server.crt和server.key两个文件拷贝

放到/etc/apache2/目录         (Finder中点击前往文件夹/etc即可打开etc文件夹)

(2) 修改配置文件

若是配置文件无法修改   只需要把途中位置改掉就好

 

  • 编辑/etc/apache2/httpd.conf文件
    找到去掉下面前边的注释#号

    LoadModule ssl_module libexec/apache2/mod_ssl.so
    Include /private/etc/apache2/extra/httpd-vhosts.conf
    Include /private/etc/apache2/extra/httpd-ssl.conf
    LoadModule socache_shmcb_module libexec/apache2/mod_socache_shmcb.so

  • 打开/etc/apache2/extra/httpd-ssl.conf文件
    去掉以下两项注释并检查是否与之前安装私钥和证书的路径一致

    SSLCertificateFile "/private/etc/apache2/server.crt"
    SSLCertificateKeyFile "/private/etc/apache2/server.key"

  •   
  • 编辑/etc/apache2/extra/httpd-vhosts.conf文件
    在<VirtualHost *:80> .....</VirtualHost>后面添加一段如下内容:
<VirtualHost *:443>
    SSLEngine on
    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    SSLCertificateFile /private/etc/apache2/server.crt
    SSLCertificateKeyFile /private/etc/apache2/server.key
    ServerName 192.168.1.112 DocumentRoot "/Library/WebServer/Documents" </VirtualHost> 

注意ServerName填写域名与所需访问一致

(3) 重启服务器
sudo apachectl restart

访问https://192.168.1.112/
提示不安全什么的(因为自己的证书没添加到浏览器信任列表),继续访问

 

 HTTPS访问成功
至此一个Mac上的apache服务器就搭好了,局域网内的小伙伴可以通过你的本级IP访问到你所设置的文件夹了
引用:https://www.jianshu.com/p/b2a9655fe687
 
 

Guess you like

Origin www.cnblogs.com/MuniuTian/p/11184270.html